2016-10-04 14 views
1

두 사각형이 JFrame에 나타나지만 주 메소드 apperas에서 마지막으로 만들고 다른 하나는 그렇지 않습니다. 지금 약 3 시간 동안 이것을 알아 내려고 노력했고 내 컴퓨터 화면을 망치고 싶다. 어떤 도움이라도 굉장합니다. 고맙습니다.하나의 객체 만 화면에 렌더링합니다.

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); 

} 
} 

답변

2

정적 변수 속하는 오브젝트가 아닌 클래스. 적 위치에 정적 변수를 사용한다는 것은 Enemy 클래스의 인스턴스를 만들면 동일한 정적 x, y를 공유한다는 것을 의미합니다. 당신은 2 개의 정사각형을 가지고 있지만 그들은 항상 서로의 위에 있습니다.

public static int x, y;에서 public int x, y;으로 변경하면 문제를 해결할 수 있습니다.

+0

정말 고마워요 !! –