Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lesson9.waitnotify;
- import java.awt.*;
- public class Car {
- private int x = 10;
- private int y = 160;
- private Color color;
- private int step = 0;
- private boolean isMoving = false;
- public Car(){
- color = Color.RED;
- }
- public void draw(Graphics g) {
- g.setColor(this.color);
- g.fillRect(x + step, y, 20, 10);
- }
- public void move() {
- new Thread(new Runnable() {
- @Override
- public void run() {
- isMoving = false;
- for (int i = step; i < 360; i++) {
- if (isMoving) {
- break;
- }
- step++;
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- public int getX() {
- return x + step;
- }
- public int getY() {
- return y;
- }
- public void stop() {
- isMoving = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement