多窗口范例(二),做一个划线生成图像的应用,最后结果:
子窗口划线,主窗口复制多个画布叠加并添加了旋转动画。
范例程序
主程序:
package syf.demo.multiwindow2;
import processing.core.PApplet;
import processing.core.PGraphics;
public class TwoWindowApp extends PApplet {
ChildWindow childwindow;
Canvas canvas;
PGraphics pg;
@Override
public void settings() {
size(800,800);
}
@Override
public void setup() {
pg = createGraphics(200,200);
pg.beginDraw();
pg.clear();
pg.background(0,0);
pg.endDraw();
canvas = new Canvas(this,pg);
childwindow = new ChildWindow(this,pg);
background(0);
}
@Override
public void draw() {
//background(20); 这里就暂时不刷新
canvas.update();
canvas.draw();
}
public static void main(String[] args) {
String[] appletArgs = new String[] {"syf.demo.multiwindow2.TwoWindowApp"};
PApplet.main(appletArgs);
}
}
子窗口类:
package syf.demo.multiwindow2;
import javafx.stage.Screen;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PGraphics;
import java.awt.*;
public class ChildWindow extends PApplet{
PGraphics pg;
PApplet app;
int preposx;
int preposy;
Dimension screen;
ChildWindow(PApplet _app,PGraphics _pg){
super();
pg = _pg;
app = _app;
PApplet.runSketch(new String[]{this.getClass().getName()},this);
Toolkit toolkit = Toolkit.getDefaultToolkit();
screen = toolkit.getScreenSize();
}
@Override
public void settings() {
size(pg.width, pg.height);
}
@Override
public void setup() {
background(0);
//让其窗口依附在主窗口左边
surface.setLocation(screen.width/2-app.width/2-this.width,screen.height/2-app.height/2);
pg.colorMode(PConstants.HSB,360,100,100);
pg.strokeWeight(8);
}
@Override
public void draw() {
pg.beginDraw();
pg.background(0,10); //背景刷新,加点透明度
//pg.stroke(250);
pg.stroke((frameCount*2)%360,100,100,150);//变颜色,颜色模式 HSB
pg.line(mouseX,mouseY,preposx,preposy);
pg.endDraw();
set(0,0,pg);
preposx = mouseX;
preposy = mouseY;
}
}
画布类:
package syf.demo.multiwindow2;
import processing.core.PApplet;
import processing.core.PGraphics;
public class Canvas {
private PApplet app;
PGraphics pg;
float rotvalue;
Canvas(PApplet _app, PGraphics _pg){
app = _app;
pg = _pg;
}
void update(){
rotvalue += 0.06; //让它自转
}
void draw(){
for (int i = 0; i < 10; i++) {
app.push();
app.translate(app.width/2,app.height/2);
app.rotate(360/10*i+rotvalue); //中心旋转扩散呈现
//app.set(0,0, pg); set 位置不受translate影响
app.image(pg, 0, 0);
app.pop();
}
}
}
总结
可以看到有了子窗口能清楚的看到用户的绘制过程和结果,在日后开发和维护都能有作用,说白了,是应用更自然更有范儿了