I am writing a java animation program that is just not working
I am writing a java animation program that creates a frame and positions a ball at the top. When you click the start button the ball is supposed to move diagonally to the bottom. However it does not move. Can anybody tell me what I am doing wrong?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BallControl2 implements ActionListener {
JButton start;
JButton left;
JButton right;
JButton up;
JButton down;
Display currentDisplay;//instance of class that overides paintComponent
int motionMarker = 0;
int x = 0;// x coordinate for animated ball
int y = 0;// y coordinate for animated ball
//Constructor
public BallControl2() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(800, 800);
frame.setLocationRelativeTo(null);
start = new JButton("Start");
start.addActionListener(this);
left = new JButton("Left");
right = new JButton("Right");
up = new JButton("Up");
down = new JButton("down");
BorderLayout border = new BorderLayout();
frame.setLayout(border);
FlowLayout flo = new FlowLayout();
JPanel controlPanel = new JPanel();
controlPanel.setLayout(flo);
controlPanel.add(start);
controlPanel.add(left);
controlPanel.add(right);
controlPanel.add(up);
controlPanel.add(down);
frame.add(controlPanel, BorderLayout.SOUTH);
currentDisplay = new Display();
frame.add(currentDisplay, BorderLayout.CENTER);
frame.setVisible(true);
}//End of constructor
public static void main(String[] args) {
try {//sleep to allow the ball to animate
Thread.sleep(100);
} catch (Exception ex) {
}
BallControl2 test = new BallControl2();
}
@Override
public void actionPerformed(ActionEvent evt) {
String t = evt.getActionCommand();
if (t.equals("Start")) {
motionMarker = 1;// ignore this for now
animator();
}
}
public class Display extends JPanel {// inner class that overrides paintComponent
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
g2.setColor(Color.BLACK);
g2.fillOval(x, y, 40, 40);
}
}
public void animator(){
while(true){
x++;
y++;
currentDisplay.repaint();
System.out.println(x);
}
}
}