绘制正方形时按键问题

时间:2019-06-30 01:49:06

标签: java processing

我正在尝试在按下鼠标键时在画布上添加一个正方形,我希望在释放鼠标键时将其保留在画布上,但是在释放键时它会消失。谁能帮助我,我在做什么错了?

int squareSize = 6;
final float DIFF_SIZE = 1;
final int MIN_COLOUR = 1;
final int MAX_COLOUR = 10;
final float INIT_RED = 100, INIT_GREEN = 50, INIT_BLUE = 80;
final float FINAL_RED = 255, FINAL_GREEN = 200, FINAL_BLUE = 250;
final float MAX_SIZE = 40;
final float X_SPACING = 10;
final float Y_SPACING = 10;
float squareX, squareY;
void setup() {
    size(600, 600);
}

void draw() {
    squareX = mouseX - squareSize / 2;
    squareY = mouseY - squareSize / 2;
    background(255);

    drawRowsOfBlocks();

}

void drawOneBlock() {

    rect(squareX, squareY, squareSize, squareSize);
    for (int i = MIN_COLOUR; mousePressed && i <= MAX_COLOUR / 10; i++)

    {
        float redValue = INIT_RED + (i - MIN_COLOUR) / (MAX_COLOUR - MIN_COLOUR)(FINAL_RED - INIT_RED);
        float greenValue = INIT_GREEN + (i - MIN_COLOUR) / (MAX_COLOUR - MIN_COLOUR)(FINAL_GREEN - INIT_GREEN);
        float blueValue = INIT_BLUE + (i - MIN_COLOUR) / (MAX_COLOUR - MIN_COLOUR) * (FINAL_BLUE - INIT_BLUE);
        fill(redValue, greenValue, blueValue);
        rect(squareX, squareY, squareSize, squareSize);
        squareSize += DIFF_SIZE;
    }
    if (squareSize > MAX_SIZE) {
        squareSize = 6;
    }
}

void drawRowsOfBlocks() {
    drawOneBlock();

    for (int i = 1; keyPressed && i <= 2; i++) {
        drawOneBlock();

        float squareY2;
        squareY2 = squareY + squareSize + Y_SPACING;
        squareY = squareY2;
    }
}

1 个答案:

答案 0 :(得分:2)

创建一个可以处理矩形的类。调用需要一种方法来绘制矩形(Draw())和一种方法来更新矩形的位置和大小(Update()):

final int DIFF_SIZE = 1;
final int MIN_SIZE = 6;
final int MAX_SIZE = 40;

final float INIT_RED = 100, INIT_GREEN = 50, INIT_BLUE = 80;
final float FINAL_RED = 255, FINAL_GREEN = 200, FINAL_BLUE = 250;

class Rectangle {

    int pos_x, pos_y, size;
    color col;

    Rectangle(int px, int py, int s, color c) {
        col = c;
        pos_x = px; pos_y = py;
        size = s;
    }

    void Update(int px, int py, int inc_size) {
        pos_x = px; pos_y = py;

        size += inc_size;
        if (size > MAX_SIZE)
            size = MIN_SIZE;

        float w = float(size - MIN_SIZE) / float(MAX_SIZE - MIN_SIZE);
        float redValue   = INIT_RED   + w * (FINAL_RED - INIT_RED);
        float greenValue = INIT_GREEN + w * (FINAL_GREEN - INIT_GREEN);
        float blueValue  = INIT_BLUE  + w * (FINAL_BLUE - INIT_BLUE);
        col = color(int(redValue), int(greenValue), int(blueValue));
    }

    void Draw() {
        fill(col);
        rect(pos_x-size/2, pos_y-size/2, size, size);
    }
}

使用ArrayList存储所有绘制的矩形:

ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();

添加一个全局布尔状态(drawingRect),该状态指示当前是否按下了鼠标按钮。设置状态,并在发生mousePressed()事件时在列表的末尾添加新的矩形。在mouseReleased()事件发生时保持状态

boolean drawingRect = false;

void mousePressed() {
    drawingRect = true;

    color col = color(int(INIT_RED), int(INIT_GREEN), int(INIT_BLUE));
    rectangles.add(new Rectangle(mouseX, mouseY, MIN_SIZE, col));
}

void mouseReleased() {
    drawingRect = false;
}

只要状态为.Update()表示已按下鼠标按钮,请使用方法drawingRect来更改列表中最后一个矩形的位置和大小。
连续不断地绘制所有te矩形:

void setup() {
    size(600, 600);
}

void draw() {

    if (drawingRect && rectangles.size() > 0) {
        Rectangle lastRect = rectangles.get(rectangles.size()-1);
        lastRect.Update(mouseX, mouseY, DIFF_SIZE);
    }

    background(255);

    for (int i = 0; i < rectangles.size(); i++) {
        Rectangle rect = rectangles.get(i);
        rect.Draw();
    }
}