import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
@SuppressWarnings("serial")
public class MainAnimation extends JFrame {
public static void main(String[] args) {
new MainAnimation();
}
private MainAnimation() {
this.setSize(600, 610);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Animator animator = new Animator();
Animator animator2 = new Animator();
this.add(animator);
this.add(animator2);
this.setVisible(true);
while (true) {
try {Thread.sleep(20);} catch (InterruptedException e) {}
animator.repaint();
animator2.repaint();
this.repaint();
}
}
private class Animator extends JPanel {
int x = (int) ((Math.random() * 560) + 1);
int y = (int) ((Math.random() * 560) + 1);
ImageIcon imageIcon = new ImageIcon("ball.png");
Image image = imageIcon.getImage();
boolean plus = true;
boolean yplus = true;
public void paint(Graphics g) {
if (plus) {
x += 10;
} else {
x -= 10;
}
if (x >= 560) {
plus = false;
}
if (x <= 0) {
plus = true;
}
if (yplus) {
y += 10;
} else {
y -= 10;
}
if (y >= 560) {
yplus = false;
}
if (y <= 0) {
yplus = true;
}
g.drawImage(image, y, x, 20, 20, this);
}
}
}
It only animates one ball. (Not two, like I want it to.)
---------- Post added at 10:57 AM ---------- Previous post was at 10:57 AM ----------
And yes I know x and y are messed up. XD
---------- Post added at 10:58 AM ---------- Previous post was at 10:57 AM ----------
y represents the x location and x represents the y location. :P


Sign In
Create Account


Back to top










