Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. package ball;
  2.  
  3. import java.applet.*;
  4. import java.awt.*;
  5.  
  6. public class Ball extends Applet implements Runnable
  7. {
  8.  
  9. int x_pos = 10;
  10. int y_pos = 100;
  11. int radius = 20;
  12. int x_speed = 1;
  13. int appletsize_x = 200;
  14. int appletsize_y = -300;
  15. int qq = 0;
  16.  
  17.  
  18. public void init() { }
  19.  
  20. public void start() {
  21. // define a new thread
  22. Thread th = new Thread (this);
  23. // start this thread
  24. th.start ();
  25. }
  26.  
  27. public void stop() { }
  28.  
  29. public void destroy() { }
  30.  
  31. public boolean mouseDown (Event e, int x, int y)
  32. {
  33.  
  34. x_speed = -2*(x_speed);
  35. qq++;
  36.  
  37.  
  38.  
  39. return true;
  40. }
  41. public void run () {
  42. // lower ThreadPriority
  43. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  44.  
  45. // run a long while (true) this means in our case "always"
  46. while (true)
  47. {
  48.  
  49. if (x_pos > appletsize_x - radius)
  50. {
  51.  
  52. // Change direction of ball movement
  53. x_speed = -1;
  54. qq = 0;
  55.  
  56. }
  57.  
  58. if (x_pos < radius) {
  59. x_speed = 1;
  60. qq = 0;
  61. }
  62. x_pos += x_speed;
  63.  
  64. // repaint the applet
  65. repaint();
  66.  
  67. try
  68. {
  69. // Stop thread for 20 milliseconds
  70. Thread.sleep (20);
  71. }
  72. catch (InterruptedException ex)
  73. {
  74. // do nothing
  75. }
  76.  
  77. // set ThreadPriority to maximum value
  78. Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  79. }
  80. }
  81.  
  82. public void paint (Graphics g) {
  83. // set color
  84. g.setColor (Color.red);
  85.  
  86. // paint a filled colored circle
  87. g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
  88.  
  89.  
  90.  
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement