gpt4 book ai didi

java - 处理 - 有面向对象的编程问题

转载 作者:行者123 更新时间:2023-11-30 11:19:31 25 4
gpt4 key购买 nike

基本上,我的目标是创建一个类似于 MS Paint 软件的程序,我可以在其中通过鼠标拖动来绘制形状并更改其颜色。这是我的一个很长的脚本。我对 OOP 还很陌生,所以我在尝试使所有功能协同工作时遇到了困难。

主要

Button button1, button2, button3, button4, button5, button6, button7;
Rect rect;
Circle circle;
int mode = 1;

void setup() {
size(900,600);
smooth();
rect = new Rect(0,0,0,0, new PImage());
circle = new Circle(0,0,0,0, new PImage());
color gray = color(234);
color black = color(0);
color white = color(255);
color red = color(255,0,0);
color green = color(0,255,0);
color blue = color(0,0,255);

button1 = new Button(10, 60, 20, white, gray, black); //draw rectangle function
button2 = new Button(10, 100, 20, white, gray, black); //draw circle function
button3 = new Button(10, 140, 20, red, gray, black); //option of color red
button4 = new Button(10, 160, 20, green, gray, black); //option of color green
button5 = new Button(10, 180, 20, blue, gray, black); //option of color blue
button6 = new Button(10, 220, 20, black, gray, black); //fill entire shape
button7 = new Button(10, 240, 20, white, gray, black); //fill nothing
}

void draw() {
button1.setp();
button2.setp();
}

void mousePressed() {
if (button1.press()) { mode = 1; }
if (button2.press()) { mode = 2; }
if (button3.press()) { mode = 3; }
if (button4.press()) { mode = 4; }
if (button5.press()) { mode = 5; }
if (button6.press()) { mode = 6; }
if (button7.press()) { mode = 7; }
}

void manageButtons() {
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();

button1.display();
button2.display();
button3.display();
button4.display();
button5.display();
button6.display();
button7.display();
}

void mouseReleased() {
button1.release();
button2.release();
button3.release();
button4.release();
button5.release();
button6.release();
button7.release();
}

void mouseDragged() {
//rect.drag();
}

按钮类

class Button {
int x, y; // the x- and y-coordinate
int size; // dimension (width & height)
color baseGray; // Default gray value
color overGray; // Value when the mouse is over
color pressGray; // Value when the mouse is pressed
boolean over = false; // true when the mouse is over
boolean pressed = false;// true when pressed

Button(int xp, int yp, int s, color b, color o, color p) {
x = xp;
y = yp;
size = s;
baseGray = b;
overGray = o;
pressGray = p;
}

void setp() {
background(255);
manageButtons();
//stroke();
if (mode == 1) {
rect.drawing();
} else if (mode == 2) {
circle.drawing();
}
}

void update() {
if ((mouseX >= x) && (mouseX <= x + size) && (mouseY >= y) && (mouseY <= y + size)) {
over = true;
} else {
over = false;
}
}

boolean press() {
if (over) {
pressed = true;
return true;
} else {
return false;
}
}

void release() {
pressed = false;
rect.release();
circle.release();
}

void display() {
if (pressed) {
fill(pressGray);
} else if (over) {
fill(overGray);
} else {
fill(baseGray);
}

stroke(0);
rect(x, y, size, size);
}
}

圈类

class Circle {
int x, y;
int xp, yp;
PImage a;

Circle(int dragx, int dragy, int movex, int movey, PImage image) {
x = dragx;
y = dragy;
xp = movex;
yp = movey;
a = image;
}

void display() {
smooth();
background(255);
a = get();
stroke(0);
fill(255); //255,255,10);
}

void drawing() {
image(a, 0, 0); //background(a);
float sizex = xp - x;
float sizey = yp - y;
if (mousePressed && mouseButton == LEFT) {
ellipse(x, y, sizex, sizey);
}
}

void press() {
x = mouseX;
y = mouseY;
}

void release() {
xp = mouseX;
yp = mouseY;
noLoop();
a = get();
loop();
}

void drag() {
xp = 80 + mouseX;
yp = 80 + mouseY;
}
}

矩形类

class Rect {
int x, y;
int xp, yp;
PImage a;

Rect(int dragx, int dragy, int movex, int movey, PImage image) {
x = dragx;
y = dragy;
xp = movex;
yp = movey;
a = image;
}

void display() {
smooth();
background(255);
a = get();
stroke(0);
fill(255); //255,255,10);
}

void drawing() {
image(a, 0, 0); //background(a);
float sizex = xp - x;
float sizey = yp - y;
if (mousePressed && mouseButton == LEFT) {
rect(x, y, sizex, sizey);
}
}

void press() {
x = mouseX;
y = mouseY;
}

void release() {
xp = mouseX;
yp = mouseY;
noLoop();
a = get();
loop();
}

void drag() {
xp = mouseX;
yp = mouseY;
}
}

使用上述处理脚本 (java),我在让矩形和圆形类正确与我创建的按钮一起工作时遇到问题。按钮 1 应该绘制矩形,按钮 2 应该绘制圆形(到目前为止,只有这些功能应该起作用。我还需要为它们应用颜色)。

我知道矩形和圆形类可以正确地工作,因为我在将所有内容组合在一起之前分别对它们进行了测试。按钮(功能)工作但不正确(我应该使用鼠标将形状拖动到任何需要的位置,而这个程序只允许它们出现我放置它们的地方并且只能从角落,就好像我只是从 x 和 y 位置 (0,0) 拉伸(stretch)形状一样。我唯一的问题是我似乎无法正确地将按钮连接到形状函数,并使它们一起工作。

最佳答案

如果不看 main 方法,很难评估代码的正确性。您的“主”类的设置方式使得 mousePressed() 方法似乎旨在轮询按钮并根据按钮的状态设置绘图模式。

但是,如果不查看 main 方法,我不可能知道您是否正确轮询了按钮。

如果您希望使用面向对象的方法,您将需要使用 Observer pattern .本质上,您的按钮都将引用具有 buttonClicked(Button btn) 方法的“主”对象。单击按钮时,它会运行“主”对象的 buttonClicked(Button btn) 方法。提供的参数将是对被单击按钮的引用,以便主程序可以选择要使用的适当模式。演示代码如下:

在主类中:

//Give the button a reference to the main object
button1 = new button(this);

//receive notifications from the button
public buttonClicked(Button btn) {
if(btn.equals(button1))
mode = 1;
if(...)
...
}

关于java - 处理 - 有面向对象的编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23189470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com