Explain java animated with swing.

361    Asked by Amitjaisawal in Java , Asked on Oct 13, 2022

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();
        }
    }
}
Answered by Andrew Jenkins

Regarding Java animated, your code has a lot of magic numbers in it.


if(oneX >= 283){
   right = false;
   left = true;
}

What is special about 283? What does it actually mean? If you define a constant that happens to have the value of 283, it will be much easier to update your code if the value needs to change.

try{
    Thread.sleep(10);
} catch (Exception exc){}

Catching Exceptions is bad. You always want to catch the most specific exception you can to do what you need. In this case, InterruptedException would be the correct exception to catch. This is not a great example because there is only one exception that can be thrown. However, in the future, it will prevent you from accidentally handling unexpected exceptions the wrong way.

Additionally, you are not doing anything with the exception. In general, this is a bad practice. Exceptions are thrown for a reason and should not be ignored without some thought. If you know you don't care what the exception is caused by, add a comment to explain your reasoning to anyone who might see the code later.


Your Answer

Interviews

Parent Categories