Java 处理Rect移动时的抖动和闪烁
所以我有一个草图,它包含数百个不同大小的矩形,保存在一个数组列表中,当我把鼠标放在它们上面时,它们会被推开,但当我移开,让它们回到原来的位置时,它们会回到原来的位置,并且会结巴,它们永远不会回到静止状态。这让我很沮丧 救命啊 编辑: 这个问题发生在很多方块被移动到一起时,我会尽可能少地编写代码 当该类使用图像初始化时,它会根据图像的颜色放置正方形Java 处理Rect移动时的抖动和闪烁,java,processing,double-buffering,Java,Processing,Double Buffering,所以我有一个草图,它包含数百个不同大小的矩形,保存在一个数组列表中,当我把鼠标放在它们上面时,它们会被推开,但当我移开,让它们回到原来的位置时,它们会回到原来的位置,并且会结巴,它们永远不会回到静止状态。这让我很沮丧 救命啊 编辑: 这个问题发生在很多方块被移动到一起时,我会尽可能少地编写代码 当该类使用图像初始化时,它会根据图像的颜色放置正方形 class Picture extends PApplet { int w, h; PApplet parent; PImage imag
class Picture extends PApplet {
int w, h;
PApplet parent;
PImage image;
PImage originalImage;
boolean started = false;
Frame frame;
int resolution = 18;
ArrayList<Square> sqrs = new ArrayList<Square>();
ArrayList<GravityPoint> points = new ArrayList<GravityPoint>();
boolean original = false;
Picture(PApplet _parent, PImage _img) {
super();
parent = _parent;
setImage(_img);
runSketch(new String[]{"Picture"}, this);
started = true;
frame = ((SmoothCanvas)getSurface().getNative()).getFrame();
}
void settings() {
size(w, h);
}
void setup() {
background(0);
sqrs.clear();
points.clear();
for (int i = 0; i < image.width; i += resolution)
for (int j = 0; j < image.height; j += resolution) {
Square sq = new Square(new PVector(i, j), (int) random(3, 15), getGraphics());
sq.setColor(image.get(i, j));
sqrs.add(sq);
points.add(new GravityPoint(sq.pos.copy()));
sqrs.get(sqrs.size() - 1).point = points.get(points.size() - 1);
}
}
void draw() {
background(0);
background(0);
if (original) image(originalImage, 0, 0);
else {
for (Square sq : sqrs) {
sq.update();
sq.show(null);
}
}
}
void mouseMoved() {
int radius = 100;
for (Square sq : sqrs) {
PVector mousePos = new PVector(mouseX, mouseY);
float distance = sq.pos.dist(mousePos);
if (distance <= radius) {
PVector force = mousePos.copy().sub(sq.pos.copy()).mult(-1 * map(distance, 0, radius, 5, 1));
sq.applyForce(force);
}
}
}
void mousePressed() {
if (mouseButton == RIGHT) frame.dispose();
if (mouseButton == LEFT) original = !original;
}
void keyPressed() {
int temp = resolution;
if (keyCode == DOWN) resolution++;
else if (keyCode == UP) resolution--;
else return;
resolution = constrain(resolution, 1, 18);
if (temp == resolution) return;
image = originalImage;
originalImage = originalImage.copy();
setup();
}
void setImage(PImage image_) {
image = image_;
int dw = parent.displayWidth;
int dh = parent.displayHeight;
if (image.width > dw) image.resize(dw, 0);
if (image.height > dh) image.resize(0, dh);
w = image.width;
h = image.height;
if (started) surface.setSize(w, h);
originalImage = image.copy();
}
}
这是广场班
class Square {
PVector pos = new PVector(0, 0);
PVector vel = new PVector(0, 0);
PVector acc = new PVector(0, 0);
PVector grv = new PVector(0, 0);
int size = 5;
GravityPoint point;
color col;
color coll;
PGraphics graphics;
Square(PVector p, int s, PGraphics gra) {
graphics = gra;
pos = p.copy();
size = s;
}
void show(PGraphics gr) {
PGraphics graphics;
if (gr != null)
graphics = gr;
else
graphics = this.graphics;
graphics.beginDraw();
graphics.pushStyle();
smooth();
graphics.rectMode(CENTER);
if (col == coll) graphics.colorMode(HSB, 255, 255, 255);
else graphics.colorMode(RGB, 255, 255, 255);
color c = color(map(pos.x, 30, width-30, 100, 255), 255, 255);
graphics.stroke(col == coll ? c : col);
graphics.strokeWeight(0.5);
graphics.noFill();
graphics.rect(pos.x, pos.y, size, size);
//graphics.shape(shape);
graphics.popStyle();
graphics.endDraw();
}
Square setColor(color _col) {
col = _col;
return this;
}
void update() {
if (point != null) applyForce(point.getVector(this).mult(4));
vel.add(acc);
vel.add(grv);
vel.limit(5);
pos.add(vel);
if (point == null) vel.mult(0);
acc.mult(0);
if (point != null && pos.dist(point.pos) < size/2) {
vel.mult(0);
acc.mult(0);
}
}
void applyForce(PVector force) {
acc.add(force);
}
}
这个类负责将矩形拉回到合适的位置
class GravityPoint{
PVector pos;
GravityPoint(PVector p){
pos = p.copy();
}
PVector getVector(Square sq){
return pos.copy().sub(sq.pos);
}
Square closest(ArrayList<Square> sqrs){
if(sqrs.size() <= 0) return null;
float distance = Float.MAX_VALUE;
Square closest = null;
for(Square sq : sqrs)
if(sq.point == null && sq.pos.dist(pos) < distance){
distance = sq.pos.dist(pos);
closest = sq;
}
return closest;
}
}
编辑2:
我认为这段代码复制了我的主程序中发生的相同问题,不是100%确定,如果您尝试帮助我完成我的主草图,我将不胜感激 好的,我修好了
问题是方块从未失去速度,它们一直围绕着重力点跳跃,从未失去速度
我修正了我的代码,使它们离重力点越近,速度就越慢。请发布一条消息。请注意,这不应是整个草图。创建一个单独的小草图,只显示两个显示该行为的硬编码矩形。@KevinWorkman很抱歉,这么多代码,我真的无法切掉这么多,因为我不知道到底是什么导致了问题。我在第二次编辑中发布了一小段代码,我认为它复制了相同的效果,但我不确定它是否具有完全相同的效果。
PVector pos;
PVector vel;
PVector acc;
PVector point;
void setup() {
size(500, 500);
pos = new PVector(width/2, height/2);
point = new PVector(width/2, height/2);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
}
void draw() {
background(255);
PVector mouse = new PVector(mouseX, mouseY);
if (mouse.dist(pos) <= 50)
acc.add(mouse.copy().sub(pos).mult(-1));
acc.add(point.copy().sub(pos).mult(3));
vel.add(acc);
vel.limit(4);
pos.add(vel);
acc.mult(0);
rectMode(CENTER);
rect(pos.x, pos.y, 30, 30);
}