Reputation: 31
I'm trying to make it so that both squares appear on the JFrame but only the one that I make last in the main method apperas and the other one does not. Have been trying to figure this out for about 3 hours now and wanna smash my computer screen. Any help would be AWESOME. Thank you.
public class Main extends JFrame{
static Main main;
static Enemy square, square2;
Render render;
Main(){
render = new Render();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setResizable(false);
add(render);
}
public void render(Graphics2D g){
square.render(g);
square2.render(g);
}
public static void main(String [] args){
main = new Main();
square2 = new Square(300,50);
square = new Square(50,50);
}
}
.....
public class Render extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
Main.main.render((Graphics2D)g);
}
}
......
public class Enemy {
public static int x,y;
Enemy(int x, int y){
this.x = x;
this.y = y;
}
public void render(Graphics2D g){
}
}
.......
public class Square extends Enemy {
Square(int x, int y){
super(x,y);
}
public void render(Graphics2D g){
g.setColor(Color.red);
g.fillRect(x, y, 50, 50);
}
}
Upvotes: 1
Views: 91
Reputation: 1326
Static variables belongs to classes not objects. Using static variables for Enemy positions means that if you create any instances of Enemy class they will share the same static x, y. You have 2 squares but they are always on top of each other.
Changing public static int x, y;
to public int x, y;
should solve your problem.
Upvotes: 2