0

so this is basically how my code is working

class Main extends JFrame implements Runnable {

   public Main() {
      //init everything
   }

   public void start() {
      running = true;
      Thread thread = new Thread(this);
      thread.start();
   }

   public void run() {
      while(running) {
         render();
      }
   }

   public void render() {
      Image dbImage  = createImage(width, height);
      Graphics dbg = dbImage.getGraphics();
      draw(dbg);
      Graphics g = getGraphics();
      g.drawImage(dbImage, 0, 0, this);
      g.dispose();
   }

   public void draw(Graphics g) {
      for(int y=0; y < map.length; y++) {
         for(int x=0; x < map.length; x++) {
            g.drawImage(map.map[x + y* map.MAP_DIM], x*MAP_DIM, y*MAP_DIM, this);
         }
      }
   }

   public static void main(String... args) {
      Main main = new Main();
      main.start();
   }
}

but nothing gets drawn, all i see is gray. anyone know what the problem could be? i tried doing repaint() at the end of the draw() method, but still nothing.

user1459976
  • 207
  • 6
  • 14
  • "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). Also, Swing GUI objects should be constructed and manipulated _only_ on the [event dispatch thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jan 04 '13 at 02:14
  • 1
    Read the tutorials before doing this sort of coding, else you'll risk making all the wrong assumptions and creating non-functioning code like you've posted above. – Hovercraft Full Of Eels Jan 04 '13 at 02:16
  • ya ill take a look at the tutorials – user1459976 Jan 04 '13 at 03:17
  • 1
    Take a look at [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) and [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for some more useful tips. – MadProgrammer Jan 04 '13 at 03:30

1 Answers1

1

You are not supposed to manage drawing by yourself with Swing in Java.

You must let the EDT thread call the appropriate repaint methods by its own thread. This is done by invoking JComponent's paint(Graphics g). Now you shouldn't override that method but paintComponent(Graphics g) instead.

So you should move your draw method from the thread to the appropriate draw method:

public void run() {
  while (running)
    repaint();
}

public void paintComponent(Graphics g) {
  draw(g);
}

Mind that you should call the repaint at a fixed frame rate and use double buffering to avoid flickering.

An even simpler solution would be to embed something already prepared for this kind of work, like a Processing frame, which works very well.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • `use double buffering to avoid flickering` ... Swing components are double buffered by default ... not that one should avoid the subject as the default mechanism may not always meet ones needs though ;) +1 from me – MadProgrammer Jan 04 '13 at 03:31