Java/Swing programming question

Miscellaneous Forums/General Discussion/Java/Swing programming question

'Allo, all. Apologies for posing this question here, but I couldn't think of any other place that might be able to help. I've been writing a game in Java for the last two months or so, and have gotten it to the stage where the engine is starting to think about working. Unfortunately, the graphics are currently borked. Here's the deal:

I'm running into problems when I try to add graphical overlay effects onto my tiles in-engine. The tiles are components that are based off of the JButton class, and I'd found that one could add graphical overlays to such tiles/buttons by adding JLabel components containing an alpha-mapped ImageIcon.

The graphics overlays seem to render fine at the beginning (the flame effect in the screens below is done using the same trick). I set up one specific overlay change to be tied to clicking the tile-buttons; once clicked, a simple green rectangle overlay is added to the tile in the results of the actionPerformed() call. This overlay (and other resulting changes to the GUI) don't actually get displayed on-screen until the frame containing the tile components gets resized, for some reason. Calling repaint() doesn't seem to do anything for any of the components (including on the frame itself). Any idea what I could do to get the frame to properly redraw/refresh?

For reference, here's a link to a screeny of before the resize forces a refresh: (edit: or a picture, apparently. go forums!)


and here's a screeny post-refresh:


Try calling the paint() method manually instead.

repaint() is only really used when performing animation, you may need to update the whole screen, since something has been in front of the app, or the dimensions have been changed!

Tell me if this works

[EDIT} - if not, I'll have another think... I've never used Java for yonks, but I remember that you can never be to sure that calling repaint() will result in the app being repainted. Something do do with it can't process them as quickly as they are being called, or another thread/task takes up most of it's time?

Try slowing down the program with the sleep() method, other than that.. visit http://forum.java.sun.com/index.jspa

- Dabz

Reading up on repaint(), it looks like it only marks an area for re-drawing the next time a call to paint() rolls around. Unfortunately, I've now tried calling paint() (using the getGraphics() method to supply the necessary Graphics object - is that the right way to do it?), and while it seems to make the tile in question flicker briefly, it doesn't actually draw the overlaid component that it's supposed to draw. (similarly, calling paint() on the frame itself does nothing).

As of right now, I don't even have any threads running at all (outside of the main program thread and whatever voodoo Java uses to keep track of its GUI components). Should I have a thread calling paint() on the frame on a regular interval? This is besides the point, though, because paint() doesn't seem to be what Java is doing when I move to resize the frame - everything updates correctly then, and I have no clue why.

You may have to make your own paint() method in a thread, and use that one... not the one of the superclass!

something like:-

(If any part is wrong, sorry) :)

import java.awt.*;

public class blah extends javax.swing.JApplet
   implements Runnable {

   Thread runner;
   int pause = 500

 public void init() {
 }

 public void paint(Graphics screen){
   super.paint(screen);
   graphics2d screen2d = (graphics2D) screen;
   other code  
}

 public void start()
  if runner == null {
 runner = new Thread)(this)
 runner.start();

 public void run() {
 Thread thisthread = thread.currentthread();
 while (runner == thisthread) {
  repaint()
  }
  }

 public void stop() {
 if runner != null {
 runner = null
}
}


I think that's roughly how it works when using threads! :/

Dabz

Alright. Attempting to muck with paint() now. Can I use the JButton-based tiles as ImageObservers when doing DrawImage?

That Bagels,to be honest, I'm not sure about, my JButton experience only extends to text buttons, something like:-


import javax.swing.*;
import java.awt*;

public class MyButton extends JFrame {
 public MyButton(){
  super("Button");
  setsize(..)
  setdefaultcloseoperation(..)
  setVisible(..)
  anythingelse()
 
  flowlayout = flow = new flowlayout();
  setlayout(...)  
  JButton clickhere = new JButton("Click Here")
  add(clickhere)
  setvisible(...)
  }

 public static void main(...blah) {
  MyButton mbutton = new MyButton
}


Visit Suns forums, for a more in depth answer... That's the first place I used to go when I played with Java! :)

Dabz

Update: A custom paint() jobby seems to work splendidly for this, and there's no thread necessary, it just draws it properly. (in fact, when I tried to use a thread to update, it threw strange exceptions). Now I just need to get the blasted side-panel to update, too. That might actually be easier to crack, since I got something reasonably similar to work in the map editor.

Thanks for all of your help, Dabz!

Anytime Bagels! :)

Dabz