Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class MovingBall {
  5.  
  6. public static void main(String[] arg) {
  7.  
  8. JFrame frame = new JFrame();
  9. frame.setSize(300, 300);
  10.  
  11. final JPanel jpanel = new JPanel() {
  12.  
  13. int x = 20;
  14. int y = 20;
  15. int radius = 20;
  16.  
  17.  
  18. public void paint(Graphics g) {
  19. x++;
  20. y++;
  21. g.setColor(Color.red);
  22. g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
  23. }
  24. };
  25.  
  26. jpanel.setBackground(Color.black);
  27.  
  28. frame.add(jpanel);
  29. frame.setVisible(true);
  30.  
  31. Thread a = new Thread(new Runnable() {
  32.  
  33. public void run() {
  34. while (true) {
  35.  
  36. jpanel.repaint();
  37.  
  38. try {
  39. Thread.sleep(20);
  40. }
  41. catch (InterruptedException ex) {
  42. ex.printStackTrace();
  43. }
  44. }
  45. }
  46. });
  47.  
  48. a.start();
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement