Dzień dobry,
jak zrobić, żeby rysowało mi dwa obiekty klasy Ship, które są dodawane w klasie Game metodą add.
W tej chwili pokazuje mi tylko ten drugi statek.
public class Game extends JFrame {
final int width=800;
final int height=600;
//static int G=6;
public Game() {
super("Lunar Lander");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width,height);
add(new Ship(10,10,6));
add(new Ship(200,200,6)); //chce, aby rysowane byly dwa statki
//Container stage = new Stage();
//add(stage,BorderLayout.CENTER);
//pack();
setVisible(true);
}
}
public class Ship extends Canvas {
private int x,y; //pozycja statku
private String imageFileName;
private Image img;
private boolean left,right,up,down;
private int vHorizontally, vUpright;
public Ship(int x, int y, int gravity) {
//schowane, zeby bylo bardziej przejrzyscie
}
public void addNotify() {
super.addNotify();
img = Toolkit.getDefaultToolkit().getImage(imageFileName);
}
public void update(Graphics g) {
if (right) x=x+10;
if (left) x=x-10;
if (up) y=y-10;
if (down) y=y+10;
g.setColor(getBackground());
g.fillRect(x-25, y-25, 100, 100);
paint(g);
}
public void paint(Graphics g) {
// System.out.println("wywolano paint ship");
Dimension size = getSize();
//g.setColor(getBackground());
//g.fillRect(0, 0, size.width, size.height);
g.drawImage(img, x-15, y-15, 50, 50, this);
}
public Dimension getPreferredSize() {
return new Dimension(50,50);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}