2012-08-26 2 views
2

두 개의 다른 이미지가 두 개의 다른 프레임에 나타나길 원합니다. 문제는이 코드가이 두 이미지 (원)를 표시하지 않지만 마지막 이미지 만 표시한다는 것입니다. 어떤 도움을 주셔서 감사합니다! 고맙습니다.이 표시됩니다.

public class MyCanvas extends JPanel { 


private static final long serialVersionUID = 1L; 
static int paint=0; 

public MyCanvas(){    
} 

public void paintComponent(Graphics graphics){ 


    System.out.println("mpika!!!"); 
    // super.paintComponent(graphics); 
     if(paint==0){ 
      graphics.setColor(Color.blue); 
      graphics.drawOval(250,250,250,250);       
     } 
     else{  
      graphics.setColor(Color.red);  
      graphics.drawOval(150,150,150,150);       
     } 
} 


public static void other(){ 
    JFrame frame2 = new JFrame(); 
    MyCanvas canvas2 = new MyCanvas(); 
    frame2.setSize(700, 700); 
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame2.add(canvas2); 
    frame2.setVisible(true); 
    Graphics graph2 = canvas2.getGraphics(); 
    canvas2.paintComponent(graph2); 

} 
public static void main(String[] args){ 
    double t; 
    JFrame frame = new JFrame(); 
    MyCanvas canvas = new MyCanvas(); 
    frame.setSize(700, 700); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(canvas); 
    frame.setVisible(true); 
    Scanner input = new Scanner(System.in); 
    Graphics graph = canvas.getGraphics(); 
    canvas.paintComponent(graph); 
//  t = input.nextInt(); 

    paint=1; 
    other(); 

} 
} 

답변

3

당신은 frame2setVisible를 호출하지 않습니다. paint 로서도

정적이다

정적 INT 페인트 = 0;

단 하나의 색만 칠해집니다.

이 솔루션은 같은, MyCanvas에서 멤버 변수에이를 수 있도록하는 것입니다 (!)

public void setColorFlag(int color) 

또는 더 나은 아직이 원 색상으로 전달합니다.

+0

귀하의 답변은 매우 도움이되었습니다. 고마워요! –

관련 문제