0

I want to appear two differenet Frames with different results in eachone. My code is :

JFrame frame = new JFrame("Before the outage in Maximization");
MyCanvas canvas = new MyCanvas(initials.vaccesspoint,initials.vTerminal);
frame.setSize(initials.frameSize, initials.frameSize);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(canvas);
frame.setVisible(true);
Graphics graph = canvas.getGraphics();
canvas.paintComponent(graph);

After this, I write some code so as to change the index of the last frame and then i run again this :

JFrame frame2 = new JFrame("Before the outage in Maximization");
MyCanvas canvas2 = new MyCanvas(initials.vaccesspoint,initials.vTerminal);
frame2.setSize(initials.frameSize, initials.frameSize);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(canvas2);
frame2.setVisible(true);
Graphics graph2 = canvas2.getGraphics();
canvas2.paintComponent(graph2);

Then they appear both of the frames but they have the same information. this is wrong. Any help please?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 06 '12 at 11:53
  • Are there any other operations on the canvas beside the constructors? can you provide the code for the constructor of canvas? – Sednus Sep 06 '12 at 11:54
  • @Sednus The first comment is noise. Please consider deleting it. – Andrew Thompson Sep 06 '12 at 12:04
  • @Sednus only the paintComponent function that draws my circles. The problem is that the programms repaints the circle in the first frame with the last colour that the circle in the second frame is painted. – Dimitris Kerkezos Sep 06 '12 at 21:17

1 Answers1

2

These two lines are identical

MyCanvas canvas = new MyCanvas(initials.vaccesspoint,initials.vTerminal);
MyCanvas canvas2 = new MyCanvas(initials.vaccesspoint,initials.vTerminal);

So we have to assume that somewhere, the data s different..

And these lines are not required

Graphics graph = canvas.getGraphics();
canvas.paintComponent(graph);

Graphics graph2 = canvas2.getGraphics();
canvas2.paintComponent(graph2);

In fact, I'd say they're are a bad idea. You don't control the paint process, Swing does.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • With this line i call the constructor of the class MyCanvas and update the vectors. So they are not identical. MyCanvas canvas = new MyCanvas(initials.vaccesspoint,initials.vTerminal); As far as concerned the othter lines, how can i draw a frame twice without calling the paintcomponent twice? – Dimitris Kerkezos Sep 06 '12 at 12:18
  • You don't control the paint process, calling setVisible(true) will cause them to request a repaint and the repaint manager will take care of it. As to the constuctors, the data your assign, as far as we can tell, is the same. You've not provided us with enough information to say otherwise – MadProgrammer Sep 06 '12 at 14:40
  • among these two parts of code there are several functions that change the datas. For example: I draw a blue circle in the first frame. Then i create a second frame to draw a red circle but then, in the first frame it apears this red circle (which means that it changes the draw). In the second frame there is the red circle correctly. – Dimitris Kerkezos Sep 06 '12 at 16:56