如何解决处理:如何根据所单击的鼠标按钮为形状赋予值?
我正在尝试在Processing上创建一个程序,该程序基于单击的按钮(向左或向右按钮)绘制矩形或椭圆形,并且我在如何将值保存在变量currentShape中class mousepressed。我应该在按下鼠标时获取一个值并将其保存到currentShape中,然后使用mouseDragged中的值来拖动并弄乱形状大小。这是我的代码:
int startX;
int startY;
int currentColor;
float currentShape;
float[] firstcornerX = {};
float[] firstcornerY = {};
float[] secondcornerX = {};
float[] secondcornerY = {};
color[] colors = {};
float[] shapes = {};
void setup() {
size(500,500);
rectMode(CORNERS);
ellipseMode(CORNERS);
}
void draw() {}
void mousepressed() {
startX = mouseX;
startY = mouseY;
currentColor = color(random(255),random(255),random(255));
if (mouseButton == LEFT) {
ellipse(mouseX,mouseY,100,100);
} else if (mouseButton == RIGHT) {
rect(mouseX,100);
}
}
void mouseReleased() {
firstcornerX = append(firstcornerX,startX);
firstcornerY = append(firstcornerY,startY);
secondcornerX = append(secondcornerX,mouseX);
secondcornerY = append(secondcornerY,mouseY);
colors = append(colors,currentColor);
shapes = append(shapes,currentShape);
}
void mouseDragged() {
background(255);
for (int i = 0; i < firstcornerX.length; i++) {
fill(colors[i]);
rect(firstcornerX[i],firstcornerY[i],secondcornerX[i],secondcornerY[i]);
}
fill(currentColor);
rect(startX,startY,mouseX,mouseY);
}
解决方法
您确实要附加currentShape
,但是您没有在mousePressed()
中的椭圆和矩形之间更改形状类型,因此currentShape
在代码中将始终为0.0
。另外,您需要使用形状类型来检查将在屏幕上呈现的形状(在代码中您直接使用rect()
和ellipse()
的所有位置)
我个人会为形状类型(或enum
)使用一个整数和几个常数,但是float currentShape;
会做到。假设0.0代表椭圆,而1.0代表矩形。您可以存储这些常数,以便轻松记住哪个常数:
final float SHAPE_TYPE_ELLIPSE = 0.0;
final float SHAPE_TYPE_RECT = 1.0;
由于需要在draw()
中渲染形状,而且还需要在mouseDragged()
时渲染形状,因此可以将功能封装到可重用的功能中(而不是复制代码):
void drawShape(float x1,float y1,float x2,float y2,float shapeType){
if(shapeType == SHAPE_TYPE_ELLIPSE){
ellipse(x1,y1,x2,y2);
}
if(shapeType == SHAPE_TYPE_RECT){
rect(x1,y2);
}
}
本来可以达到if(shapeType == 0.0) ... else ...
的条件,但是上面的内容更易于阅读/理解,将来可以扩展以支持更多形状。
剩下3个要仔细检查:
- 基于鼠标按钮更新
mousePressed()
中的形状类型 - 在
mouseReleased()
中添加形状类型(您已经这样做了) - 在
drawShape()
和mouseDragged()
中相应地调用draw()
完整的代码清单:
int startX;
int startY;
int currentColor;
float currentShape;
// constants for the supported shape types
final float SHAPE_TYPE_ELLIPSE = 0.0;
final float SHAPE_TYPE_RECT = 1.0;
float [] firstcornerX = {};
float [] firstcornerY = {};
float [] secondcornerX = {};
float [] secondcornerY = {};
color [] colors = {};
float [] shapes = {};
void setup () {
size(500,500);
rectMode(CORNERS);
ellipseMode(CORNERS);
}
void draw() {
background (255);
for (int i=0; i < firstcornerX.length; i++) {
fill(colors[i]);
// draw the shape from memory
drawShape(firstcornerX[i],firstcornerY[i],secondcornerX[i],secondcornerY[i],shapes[i]);
}
}
void drawShape(float x1,y2);
}
}
void mousePressed () {
startX = mouseX;
startY = mouseY;
currentColor = color(random(255),random(255),random(255));
if(mouseButton == LEFT) {
currentShape = SHAPE_TYPE_ELLIPSE;
}else if(mouseButton == RIGHT) {
currentShape = SHAPE_TYPE_RECT;
}
}
void mouseReleased () {
firstcornerX = append(firstcornerX,startX);
firstcornerY = append(firstcornerY,startY);
secondcornerX = append(secondcornerX,mouseX);
secondcornerY = append(secondcornerY,mouseY);
colors = append(colors,currentColor);
shapes = append(shapes,currentShape);
}
void mouseDragged () {
fill(currentColor);
// preview the shape live
drawShape(startX,startY,mouseX,mouseY,currentShape);
}
我正在使用旧版本的Processing,并且在mouseDragged()
下出现了一些闪烁。或者,可以在mousePressed
中使用draw()
布尔值:
int startX;
int startY;
int currentColor;
float currentShape;
// constants for the supported shape types
final float SHAPE_TYPE_ELLIPSE = 0.0;
final float SHAPE_TYPE_RECT = 1.0;
float [] firstcornerX = {};
float [] firstcornerY = {};
float [] secondcornerX = {};
float [] secondcornerY = {};
color [] colors = {};
float [] shapes = {};
void setup () {
size(500,shapes[i]);
}
// preview the shape live if mouse is dragged:
if(mousePressed){
fill(currentColor);
drawShape(startX,currentShape);
}
}
void drawShape(float left,float top,float right,float bottom,float shapeType){
if(shapeType == SHAPE_TYPE_ELLIPSE){
ellipse(left,top,right,bottom);
}
if(shapeType == SHAPE_TYPE_RECT){
rect(left,bottom);
}
}
void mousePressed () {
startX = mouseX;
startY = mouseY;
currentColor = color(random(255),currentShape);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。