Moving
Ball
Instructions: 1. Click on the panel for a new
starting point.
Complete Source
Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MovingBall {
private static JFrame myFrame;
private static MovingBallPanel
myPanel;
private static MovingBallListener
listener;
private static Timer timer;
private static Ball ball;
private static int[] xCoord;
private static int[] yCoord;
private static int index;
public static class
MovingBallPanel extends
JPanel {
public void
paintComponent(Graphics g) {
super.paintComponent(g);
ball.draw(g);
for (int i = 0; i < index; i++) g.drawLine(xCoord[i],yCoord[i],xCoord[i+1],yCoord[i+1]);
}
}
public static class
MovingBallListener implements
MouseListener,
ActionListener {
public void
mousePressed(MouseEvent e) {
ball = new Ball(e.getX(),e.getY());
index = 0;
for (int i = 0; i < 20000; i++) {
xCoord[i] = 0;
yCoord[i] = 0;
}
xCoord[index] = e.getX() + 10;
yCoord[index] = e.getY() + 10;
myPanel.repaint();
}
public void
mouseReleased(MouseEvent e) {}
public void
mouseClicked(MouseEvent e) {}
public void
mouseEntered(MouseEvent e) {}
public void
mouseExited(MouseEvent e) {}
public void
actionPerformed(ActionEvent e) {
if (ball.x == 0 || ball.y == 0 || ball.x == 480 || ball.y == 380)
ball.direction();
ball.motion();
if (index < 19998) {
index++;
xCoord[index] = ball.x + 10;
yCoord[index] = ball.y + 10;
}
myPanel.repaint();
}
}
public static class
Ball {
int x, y,
direction;
public Ball(int
x, int y) {
this.x = x;
this.y = y;
if (x < 480 && y < 380) direction = 1;
else direction = 3;
}
public void
draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillOval(x,y,20,20);
}
public void
direction() {
if (x == 0 && y == 380) direction = 2;
else if (x == 480 && y == 380) direction = 3;
else if (x == 480 && y == 0) direction = 4;
else if (x == 0 && y == 0) direction = 1;
else if (x != 480 && y == 380 && direction == 1) direction = 2;
else if (x != 0 && y == 380 && direction == 4) direction = 3;
else if (x == 480 && y != 0 && direction == 2) direction = 3;
else if (x == 480 && y != 380 && direction == 1) direction = 4;
else if (x != 0 && y == 0 && direction == 3) direction = 4;
else if (x != 480 && y == 0 && direction == 2) direction = 1;
else if (x == 0 && y != 380 && direction == 4) direction = 1;
else if (x == 0 && y != 0 && direction == 3) direction = 2;
}
public void
motion() {
if (direction == 1) {
x++;
y++;
}
else if (direction == 2) {
x++;
y--;
}
else if (direction == 3) {
x--;
y--;
}
else if (direction == 4) {
x--;
y++;
}
}
}
public static void
main(String[] args) {
myFrame = new
JFrame("Moving Ball");
myPanel = new
MovingBallPanel();
listener = new
MovingBallListener();
timer = new
Timer(10,listener);
ball = new
Ball(0,100);
xCoord = new
int[20000];
yCoord = new
int[20000];
index = 0;
xCoord[index] =
10;
yCoord[index] =
110;
myPanel.addMouseListener(listener);
myPanel.setBackground(Color.BLACK);
myFrame.setContentPane(myPanel);
myFrame.setLocation(100,100);
myFrame.setSize(506,428);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setResizable(false);
myFrame.setVisible(true);
timer.start();
}
}
|