1

Im quiet new in java graphics programming, but i didnt thougt i would have problems right at the beginning:

I have this simple loop - which moves and resizes the cube, but its very slow and "unclean". I can see the pixels changing, when you know what i mean. What could i do better here? And why its so slow? Thank you all!

So, heres the code:

package game;

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

public class Main extends JPanel implements Runnable{

    Box b = new Box(0, 0, 20, 20);
    Thread t = new Thread(this);

    public Main(){
        JFrame f = new JFrame();
        f.setSize(800, 600);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);
        f.setVisible(true);

        setBounds(0, 0, f.getWidth(), f.getHeight());
        setBackground(Color.BLUE);
        f.add(this);

        t.start();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        b.paint(g);
    }

    public void run(){
        while(b.x < 100){
            b.x++;
            b.y++;
            b.width++;
            b.height++;
            repaint();
            try{Thread.sleep(10);}catch(Exception e){}
        }
    }

    public static void main(String[] args) {
        new Main();
    }

    public class Box {

        public int x;
        public int y;
        public int width;
        public int height;
        public boolean used;

        public Box(int x, int y, int width, int height){
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public void paint(Graphics g){
            g.setColor(Color.GREEN);
            g.fillRect(x, y, width, height);
        }

    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
GFP
  • 260
  • 3
  • 10
  • 1
    do you require that `Thread.sleep` – Abubakkar Jul 03 '13 at 13:50
  • what do you mean?.. could you explain a bit? – GFP Jul 03 '13 at 13:52
  • 3
    use `Swing Timer` instead or `Thread` and dealyed by `Thread.sleep(int)`, because `Thread.sleep(int)` caused lock of EDT, btw (maybe one milion times) asked similair questions here :-) – mKorbel Jul 03 '13 at 13:55
  • See this question about enabling Double Buffering (I have a feeling that's your "uncleaneliness"): http://stackoverflow.com/questions/5924697/java-double-buffering – anthonyvd Jul 03 '13 at 13:57
  • @pwny Swing JComponents are DoubleBuffered by default – mKorbel Jul 03 '13 at 13:58
  • @mKorbel I wasn't sure about it. Thanks for the info (the more you know!) – anthonyvd Jul 03 '13 at 14:00
  • yes that is right... but all tutorials or videos i saw said i should use thread.sleep instead of timers. – GFP Jul 03 '13 at 14:03
  • Timer is better, Thread.sleep should be better be in a separate Thread, which again has its details to worry about. In demos it is often a matter of verbosity. Maybe `panel.repaint(50L);` too. – Joop Eggen Jul 03 '13 at 14:30
  • i dont know you actually see, but Thread.sleep is in an own thread... and i dont know how to set up an gameloop with an timer... – GFP Jul 03 '13 at 14:38
  • 1
    @trashgod OP is not sleeping on the EDT, so that's not the issue here. – Harald K Jul 03 '13 at 15:00
  • 1
    @haraldK: Thanks, you are correct; GFP: Please cite the tutorial(s) to which you refer. – trashgod Jul 03 '13 at 15:07
  • for example this one: http://www.java-gaming.org/index.php?topic=24220.0 – GFP Jul 03 '13 at 22:00

1 Answers1

4

I'd use a Swing Timer as mentioned in the comments, otherwise there's nothing obviously wrong with your code.

However, to get perfectly smooth graphics on screen, you need to have "vertical sync" (that is, the entire painting must be done between screen refresh). I suggest you have a look at How to use BufferStrategy in Java for a start.

EDIT

Out of curiosity, I did some experiments using the ideas found in the blog post above, and while the animation became quite smooth, I did not achieve full vsync using Java 6 on OS X. I still get some "tearing". This was quite a disappointment.

Harald K
  • 26,314
  • 7
  • 65
  • 111