Dimitris Kerkezos
Dimitris Kerkezos

Reputation: 23

appearing on the same frame

I want two different images to appear in two different frames. The problem is that this code doesn't show this two images (circles) but only the last one. Any help would be appreciate! Thank you.

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

}
}

Upvotes: 2

Views: 76

Answers (1)

Reimeus
Reimeus

Reputation: 159764

You never call setVisible on frame2.

Also as paint is static:

static int paint = 0;

You will only ever see one color painted.

The solution is to make this into a member variable in MyCanvas, something like:

public void setColorFlag(int color)

or better still pass in the circle color(!).

Upvotes: 3

Related Questions