使对象移动 - 面向对象 - JAVA

时间:2016-10-16 17:19:05

标签: java object

尝试让对象移动到屏幕左侧,但我无法做到。 使用这两个类(获得第三个单独的类只是为了启动程序)。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import gu2.PaintWindow_GU2;
import paintpanel.Text;

public class Race {
//Instansvariabler
private PaintWindow_GU2 window;
private Car car1;
private Car car2;
private ImageIcon image = new ImageIcon("images/RacingTrack.png");
Random rand = new Random();
private int x;
private int x2;
Font font = new Font("SansSerif",Font.PLAIN,24);


//Konstruktor som använder sig av variablerna window, car1 och car2
public Race(PaintWindow_GU2 window,Car car1, Car car2){
this.window = window;
this.car1 = car1;
this.car2 = car2;


}

//Action-metosd för att starta racet
public void action(){
window.addIcon(image, 50, 50, true);
window.addIcon(car1.getImage(), car1.getX(), car1.getY(), true);
window.addIcon(car2.getImage(), 600, 205, true);
while(true){    
car1.moveTo(600-5, 165);    
window.setIconXY(car1.getImage(), car1.getX(), car1.getY());






}
}
}
import javax.swing.ImageIcon;

public class Car {
    private ImageIcon image;
    private int x;
    private int y;

    public Car(ImageIcon image) {
        this.image = image;
    }

    public ImageIcon getImage() {
        return image;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void moveTo(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

所以我在动作方法中缺少一些代码,但我不确定如何让对象向左移动。 任何有助于让对象向左移动的帮助都将非常感激。

0 个答案:

没有答案