问题
Yesterday I found the following code for creating a second window in Processing
import javax.swing.JFrame;
PFrame f;
secondApplet s;
void setup() {
size(600, 340);
}
void draw() {
background(255, 0, 0);
fill(255);
}
void mousePressed(){
PFrame f = new PFrame();
}
public class secondApplet extends PApplet {
public void setup() {
size(600, 900);
noLoop();
}
public void draw() {
fill(0);
ellipse(400, 60, 20, 20);
}
}
public class PFrame extends JFrame {
public PFrame() {
setBounds(0, 0, 600, 340);
s = new secondApplet();
add(s);
s.init();
println("birh");
show();
}
}
And edited...
void mousePressed(){
PFrame f = new PFrame();
}
Into:
if(mousePressed && mouseX > 1050 && mouseX < 1350 && mouseY > 700 && mouseY < > 750) {
f = new PFrame();
}
}
It worked lovely, but since I downloaded and installed Processing III, I've got the following errors:
- The function
add()
expects parameters likeadd(component)
. - The function
init()
does not exist. - The method
show()
from the type window is deprecated.
回答1:
First of all, that code is not very good. I'm surprised it worked in Processing 2, let alone Processing 3. Be very wary of code you just find randomly on the internet.
That being said, here's some code:
void setup() {
size(100, 100);
String[] args = {"TwoFrameTest"};
SecondApplet sa = new SecondApplet();
PApplet.runSketch(args, sa);
}
void draw() {
background(0);
ellipse(50, 50, 10, 10);
}
public class SecondApplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}
The above works for me, although the approach seems pretty hackish. If you really want to have two windows in your sketch, you might be better off creating a Java application that spawns two sketches.
回答2:
Processing 3 was changed so it is no longer dependent on AWT allowing for more flexibility, but breaking code that depends on it (like JFrames and such). The new way to do it is by using PSurfaces but documentation and examples are lacking at the present moment. This part of Processing 3 is under active development so you'll have to wait it out a bit.
来源:https://stackoverflow.com/questions/32243316/new-window-in-processing