Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. package lesson9.waitnotify;
  2.  
  3.  
  4. import java.awt.*;
  5.  
  6. public class Car {
  7.  
  8. private int x = 10;
  9. private int y = 160;
  10. private Color color;
  11. private int step = 0;
  12. private boolean isMoving = false;
  13.  
  14. public Car(){
  15.  
  16. color = Color.RED;
  17. }
  18.  
  19. public void draw(Graphics g) {
  20.  
  21. g.setColor(this.color);
  22. g.fillRect(x + step, y, 20, 10);
  23. }
  24.  
  25. public void move() {
  26.  
  27. new Thread(new Runnable() {
  28. @Override
  29. public void run() {
  30. isMoving = false;
  31. for (int i = step; i < 360; i++) {
  32. if (isMoving) {
  33. break;
  34. }
  35. step++;
  36. try {
  37. Thread.sleep(50);
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. }).start();
  44. }
  45.  
  46. public int getX() {
  47. return x + step;
  48. }
  49.  
  50. public int getY() {
  51. return y;
  52. }
  53.  
  54. public void stop() {
  55.  
  56. isMoving = true;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement