I agree with everything that PhiLho has said and I am also concerned about the classes you have created.
For instance the two classes called Applet1 and Applet2 are the same effectivley the same the only difference being that value stored in a attribute. Same problem with the classes Frame1 and Frame2. This is poor OO design which will cause loads of problems - just think of the number of classes needed if you wanted 8 linked Windows.
Having said all that there is a solution to your problem.
The code below works I have tested it, but it uses classes from the G4P library. Download the G4P library from
here, extract the library from the zip file and locate the file called guicomponents.jar
Since you are using Proclipsing there will be a folder called lib inside the Eclipse Project, copy guicomponents.jar into this folder, if Eclipse is open then you need to refresh the project.
Now you have to modify the project's build path, right click on the project name and select
Build Path | Configure Build Path then in the dialog box select the
Libraries tab. Click on
Add External JARs then browse to the guicomponents.jar file and click OK
Now your project can make use of anything in the G4P library including its support of multiple Windows.
You have not posted your Circle class so I had to create my own which I called CircleQ so it would not conflict with your class. Add this class to the project.
- public class CircleQ {
- public int x,y,r,col;
- public CircleQ(int x, int y, int r, int col) {
- this.x = x;
- this.y = y;
- this.r = r;
- this.col = col;
- }
- }
The next class is the core of the solution and holds all the dtata to be shared between the Windows. The key here is that shared attributes should be public static
- public class SharedData {
- public static CircleQ cq;
- }
The final class is the main PApplet and when you run it it will display 4 Windows (with different background colours). Click on the one with the gray background to make it active. The
wasd keys will move the circle and the
zx keys decrease/increase the circles radius. Note that the changes are shown in all 4 Windows.
HTH
- import guicomponents.GWinApplet;
- import guicomponents.GWinData;
- import guicomponents.GWindow;
- import processing.core.PApplet;
- public class LinkedWindows extends PApplet {
- private GWindow[] window;
- public void setup(){
- size(200,200);
- SharedData.cq = new CircleQ(100,100,60,color(128,0,128));
- createWindows();
- }
- public void createWindows(){
- int col;
- window = new GWindow[3];
- for(int i = 0; i < 3; i++){
- col = (200 << (i * 8)) | 0xff000000;
- window[i] = new GWindow(this, "Window "+i, 130+i*210, 100+i*100, 200, 200, false, JAVA2D);
- window[i].setBackground(col);
- window[i].addDrawHandler(this, "windowDraw"+i);
- }
- }
- /**
- * Draw for the main window
- */
- public void draw(){
- pushMatrix();
- background(192);
- fill(0);
- text("Window 0", 20,20);
- if(SharedData.cq != null){
- fill(SharedData.cq.col);
- ellipseMode(CENTER);
- translate(SharedData.cq.x, SharedData.cq.y);
- ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
- }
- popMatrix();
- }
- public void keyTyped(){
- switch(key){
- case 'w':
- SharedData.cq.y -= 4;
- break;
- case 's':
- SharedData.cq.y += 4;
- break;
- case 'a':
- SharedData.cq.x -= 4;
- break;
- case 'd':
- SharedData.cq.x += 4;
- break;
- case 'z':
- SharedData.cq.r = max(SharedData.cq.r - 4, 40);
- break;
- case 'x':
- SharedData.cq.r = min(SharedData.cq.r + 4, 80);
- break;
- }
- }
- public void windowDraw0(GWinApplet appc, GWinData data){
- appc.pushMatrix();
- appc.fill(255);
- appc.text("Window 0", 20,20);
- if(SharedData.cq != null){
- appc.fill(SharedData.cq.col);
- appc.ellipseMode(CENTER);
- appc.translate(SharedData.cq.x, SharedData.cq.y);
- appc.ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
- }
- appc.popMatrix();
- }
- public void windowDraw1(GWinApplet appc, GWinData data){
- appc.pushMatrix();
- appc.fill(0);
- appc.text("Window 1", 20,20);
- if(SharedData.cq != null){
- appc.fill(SharedData.cq.col);
- appc.ellipseMode(CENTER);
- appc.translate(SharedData.cq.x, SharedData.cq.y);
- appc.ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
- }
- appc.popMatrix();
- }
-
- public void windowDraw2(GWinApplet appc, GWinData data){
- appc.pushMatrix();
- appc.fill(255,255,0);
- appc.text("Window 2", 20,20);
- if(SharedData.cq != null){
- appc.fill(SharedData.cq.col);
- appc.ellipseMode(CENTER);
- appc.translate(SharedData.cq.x, SharedData.cq.y);
- appc.ellipse(0, 0, SharedData.cq.r, SharedData.cq.r);
- }
- appc.popMatrix();
- }
- }