Processing 停止背景刷新?
我试图让我的gif做一些类似的事情 我已经能够画出这条线,让“行星”绕轨道运行,但我无法像gif一样,找出如何保持这条线连接两个圆 以下是基本代码:Processing 停止背景刷新?,processing,Processing,我试图让我的gif做一些类似的事情 我已经能够画出这条线,让“行星”绕轨道运行,但我无法像gif一样,找出如何保持这条线连接两个圆 以下是基本代码: int x = 500; int y = 500; int radius = y/2; int cX = x/2; int cY = y/2; String text1; int lg_xBall; int lg_yBall; int sm_xBall; int sm_yBall; void setup() { size(x, y);
int x = 500;
int y = 500;
int radius = y/2;
int cX = x/2;
int cY = y/2;
String text1;
int lg_xBall;
int lg_yBall;
int sm_xBall;
int sm_yBall;
void setup() {
size(x, y);
smooth();
colorMode(RGB);
}
void draw() {
background(0);
stroke(255);
float t = millis()/1000.0f;
drawSmBallOrbit(100);
drawLgBallOrbit(100);
moveSmBall(t);
moveLgBall(t);
sun();
// showMouse();
connectingLines();
}
void drawCircle() { // This will draw a simple circle
stroke(1);
// x1=a+r*cos t, y1=b+r*sin t
ellipse(x/2, y/2, x/2, y/2);
}
void drawLines() { // This will draw lines from the center of the circle.
stroke(1);
line(x/2, y/2, radius/2, radius); // line from 6 to center
line(x/2, y/2, x/2, y/4); // line from 12 to center
for (int i = 0; i <= 5; i+=2.5) {
float x1 = x/2+radius/2*cos(i);
float y1 = y/2+radius/2*sin(i);
line(x/2, y/2, x1, y1);
}
}
void moveSmBall(float ky) { // This will create, and move, a small 'planet'
pushStyle();
stroke(100);
sm_xBall = (int)(cX+radius*cos(ky));
sm_yBall = (int)(cY+radius*sin(ky));
fill(190, 0, 0);
// background(0);
ellipse(sm_xBall, sm_yBall, 10, 10);
popStyle();
}
void drawSmBallOrbit(float opacity) {
pushStyle();
stroke(255, opacity);
strokeWeight(1);
noFill();
ellipse(x/2, y/2, cX+radius, cY+radius);
popStyle();
}
void moveLgBall(float kx) {
kx = kx/.7;
pushStyle();
lg_xBall = (int)(cX+radius*cos(kx)*.6);
lg_yBall = (int)(cY+radius*sin(kx)*.6);
fill(0, 0, 230);
ellipse(lg_xBall, lg_yBall, 30, 30);
popStyle();
}
void drawLgBallOrbit(float opacity) {
pushStyle();
stroke(255, opacity);
strokeWeight(1);
noFill();
ellipse(x/2, y/2, (cX+radius)*.6, (cY+radius)*.6);
popStyle();
}
void sun() {
pushStyle();
fill(250, 250, 0);
ellipse(cX, cY, 40, 40);
popStyle();
}
void connectingLines() {
line(sm_xBall, sm_yBall, lg_xBall, lg_yBall);
}
void showMouse() {
text("X: " + mouseX, x/2, y/2-30);
text("Y: " + mouseY, x/2, y/2-50);
}
intx=500;
int y=500;
int半径=y/2;
int cX=x/2;
int-cY=y/2;
字符串text1;
int lg_xBall;
int lg_yBall;
int sm_xBall;
int sm_yBall;
无效设置(){
尺寸(x,y);
光滑的();
彩色模式(RGB);
}
作废提款(){
背景(0);
中风(255);
浮点数t=millis()/1000.0f;
拉丝姆巴洛比(100);
抽签(100);
moveSmBall(t);
移动球(t);
sun();
//showMouse();
连接线();
}
void drawCircle(){//这将绘制一个简单的圆
中风(1);
//x1=a+r*cos t,y1=b+r*sin t
椭圆(x/2,y/2,x/2,y/2);
}
void drawLines(){//这将从圆的中心绘制线。
中风(1);
直线(x/2,y/2,半径/2,半径);//从6到中心的直线
直线(x/2,y/2,x/2,y/4);//从12到中心的直线
对于(int i=0;i而言,问题在于您在每一帧中调用background()
,这将清除您已经绘制的所有内容
因此,您需要停止调用background()
,或者需要每帧重新绘制旧的行
如果您只需将对background()
的调用从draw()
函数移到setup()
函数中,您已经完成了大约50%:
void setup() {
size(x, y);
smooth();
colorMode(RGB);
background(0);
}
void draw() {
// background(0);
stroke(255);
float t = millis()/1000.0f;
drawSmBallOrbit(100);
drawLgBallOrbit(100);
moveSmBall(t);
moveLgBall(t);
sun();
// showMouse();
connectingLines();
}
但是,原始动画不显示椭圆的先前位置。因此,您需要通过调用background()
函数清除先前的帧,然后重新绘制先前的行位置。您可以通过使用保存这些先前位置的ArrayList来完成此操作
下面是一个使用ArrayList重新绘制鼠标所在位置的简单示例:
ArrayList<PVector> points = new ArrayList<PVector>();
void setup() {
size(500, 500);
}
void draw() {
background(0);
stroke(255);
points.add(new PVector(mouseX, mouseY));
for(PVector p : points){
ellipse(p.x, p.y, 10, 10);
}
}
ArrayList points=new ArrayList();
无效设置(){
大小(500500);
}
作废提款(){
背景(0);
中风(255);
添加(新的PVector(mouseX,mouseY));
对于(PVector p:点){
椭圆(p.x,p.y,10,10);
}
}
你需要做一些非常类似的事情,但是你必须一次跟踪两个点,而不是一个点,因为你跟踪的是两个椭圆,而不仅仅是鼠标位置。问题是你在每一帧中调用background()
,这将清除你已经绘制的所有内容
因此,您需要停止调用background()
,或者需要每帧重新绘制旧的行
如果您只需将对background()
的调用从draw()
函数移到setup()
函数中,您已经完成了大约50%:
void setup() {
size(x, y);
smooth();
colorMode(RGB);
background(0);
}
void draw() {
// background(0);
stroke(255);
float t = millis()/1000.0f;
drawSmBallOrbit(100);
drawLgBallOrbit(100);
moveSmBall(t);
moveLgBall(t);
sun();
// showMouse();
connectingLines();
}
但是,原始动画不显示椭圆的先前位置。因此,您需要通过调用background()
函数清除先前的帧,然后重新绘制先前的行位置。您可以通过使用保存这些先前位置的ArrayList来完成此操作
下面是一个使用ArrayList重新绘制鼠标所在位置的简单示例:
ArrayList<PVector> points = new ArrayList<PVector>();
void setup() {
size(500, 500);
}
void draw() {
background(0);
stroke(255);
points.add(new PVector(mouseX, mouseY));
for(PVector p : points){
ellipse(p.x, p.y, 10, 10);
}
}
ArrayList points=new ArrayList();
无效设置(){
大小(500500);
}
作废提款(){
背景(0);
中风(255);
添加(新的PVector(mouseX,mouseY));
对于(PVector p:点){
椭圆(p.x,p.y,10,10);
}
}
你需要做一些非常类似的事情,但你必须一次跟踪两个点而不是一个点,因为你跟踪的是两个椭圆,而不仅仅是鼠标位置。你现在得到了什么-只有一条线连接你的行星?然后你必须将每个新的x/y对保存到一个数组中,并绘制它。@Jongware-没错,我只得到连接行星的单线。嗯,这是个好主意。关于在何处/如何实现这一点,有什么提示吗?我是否需要创建一个全局数组,将值从moveLgBall/moveSmBall subs传递到该数组,然后在“连接线”中传递,使用数组创建线?我在可视化如何在循环中执行此操作时遇到困难。我不知道足够的处理方法来建议正确的语法。我想你可以试试你的想法,看看你能走多远。@Jongware-我来试一试。我必须解决如何将变量传递到数组中,并画一条线,而不是那一行可以“删除”当背景刷新时…嗯。你现在得到了什么?只有一条线连接你的行星?然后你必须将每个新的x/y对保存到一个数组中,并绘制它。@Jongware-没错,我只得到连接行星的一条线。嗯,这是个好主意。关于在哪里/如何实现这一点,有什么提示吗?我需要创建一个数组吗全局数组,将值从moveLgBall/moveSmBall子数组传递到该数组,然后在“连接线”中,使用数组创建线?我在可视化如何在循环中执行此操作时遇到困难。我不知道足够的处理方法来建议正确的语法。我想你可以试试你的想法,看看你能走多远。@Jongware-我来试一试。我必须解决如何将变量传递到数组中,并画一条线,而不是当背景刷新时,该行将被“删除”…嗯。谢谢!!很抱歉我的延迟响应。我会查看该行并让你知道我能做/不能做什么。谢谢!!很抱歉我的延迟响应。我会查看该行并让你知道我能做/不能做什么。