I am learning to use Java Swing and have made a simple animation that makes a small shape bounce around the predetermined borders of a panel. The program works well, but I am looking for feedback in terms of quality and any improvements or alternative methods that could be used in such an application.
import javax.swing.*;
import java.awt.*;
final public class Tester {
JFrame frame;
DrawPanel drawPanel;
private int oneX = 7;
private int oneY = 7;
boolean up = false;
boolean down = true;
boolean left = false;
boolean right = true;
public static void main(String[] args) {
new Tester().go();
}
private void go() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new DrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(300, 300);
frame.setLocation(375, 55);
moveIt();
}
class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.RED);
g.fillRect(3, 3, this.getWidth()-6, this.getHeight()-6);
g.setColor(Color.WHITE);
g.fillRect(6, 6, this.getWidth()-12, this.getHeight()-12);
g.setColor(Color.BLACK);
g.fillRect(oneX, oneY, 6, 6);
}
}
private void moveIt() {
while(true){
if(oneX >= 283){
right = false;
left = true;
}
if(oneX <= 7){
right = true;
left = false;
}
if(oneY >= 259){
up = true;
down = false;
}
if(oneY <= 7){
up = false;
down = true;
}
if(up){
oneY--;
}
if(down){
oneY++;
}
if(left){
oneX--;
}
if(right){
oneX++;
}
try{
Thread.sleep(10);
} catch (Exception exc){}
frame.repaint();
}
}
}