Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- public class MovingBall {
- public static void main(String[] arg) {
- JFrame frame = new JFrame();
- frame.setSize(300, 300);
- final JPanel jpanel = new JPanel() {
- int x = 20;
- int y = 20;
- int radius = 20;
- public void paint(Graphics g) {
- x++;
- y++;
- g.setColor(Color.red);
- g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
- }
- };
- jpanel.setBackground(Color.black);
- frame.add(jpanel);
- frame.setVisible(true);
- Thread a = new Thread(new Runnable() {
- public void run() {
- while (true) {
- jpanel.repaint();
- try {
- Thread.sleep(20);
- }
- catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- }
- }
- });
- a.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement