1

我有这段代码在网格中随机绘制六边形并从数组中随机着色。

我要做的是按一个CP5按钮来初始化绘图功能。如果我再次按下此按钮,它将用黑色填充所有绘制的六边形并重新初始化绘图功能。

目前这会初始化绘图:

void mousePressed() {

  if (active_counter == 0) {

    hexagons[int(wide/2)][int(tall/2)].active = true;

    active_counter++;
  }
}

所以我尝试了这种方法:

public void startDrawing(int theValue) {
    println("a button event from startDrawing: "+theValue);
    c1 = c2;
    c2 = color(0,0,0);

    if (active_counter == 40) {
      fill(color(0));
    }
}

这不起作用,因为“填充”没有连接到“六边形”,但我很难以正确的方式附加它。

这是完整的代码:

import controlP5.*;

ControlP5 cp5;

int myColor = color(255);

int c1,c2;

float n,n1;

// ------------- Hexagon grid -------------
Hexagon[] [] hexagons;

float rx = 51;
float ry = 56;
int wide;
int tall;

int hexagon;

int active_counter = 0;
boolean active;
int max_hexagons;

float Xoffset, Yoffset;
int randX;
int randY;
int randD;
int x;
int y;

int[] stepX = { -1, -1, 1, 0, 1, 1 };
int[][] stepY = {{ 0, 1, -1, 1, 0, 1 }, { 0, -1, -1, 1, 0, -1 } };

class Hexagon {
  float x;
  float y;
  int rand;
  color[] colors;
  boolean active;

  Hexagon (float ix, float iy) {
    x = ix;
    y = iy;
    active = false;

    // Array of colors
    colors = new color[10];
    colors[0] = color(#62563D); // Infantry green
    colors[1] = color(#2C2B2D); // Parisian night blue
    colors[2] = color(#3E2224); // Purple heart
    colors[3] = color(#A49F9B); // Wild dove grey
    colors[4] = color(#684F40); // Brown
    colors[5] = color(#5C573D); // Moss green
    colors[6] = color(#B9897F); // Pink
    colors[7] = color(#24283B); // Dark blue
    colors[8] = color(#1F1D20); // Black
    colors[9] = color(#C5A9A2); // Brazilian clay

    // Takes the colors array and output random colors
    rand = (int)random(colors.length);
  }
  // ------------- Nested draw -------------
  void draw() {
    if (active) {

      // Call the colors array in random order
      fill(colors[rand]);
    } else {
      noFill();
    }

    stroke(80);
    pushMatrix();
    translate(x, y);
    beginShape();
    vertex(-17, -28);
    vertex(17, -28);
    vertex(34, 0);
    vertex(17, 28);
    vertex(-17, 28);
    vertex(-34, 0);
    endShape(CLOSE);
    popMatrix();
  }
}

// ------------- setup -------------
void setup() {
  size(1000, 700);

  // ------------- CP5 control buttons -------------
  cp5 = new ControlP5(this);

  cp5.addButton("start drawing")
     .setValue(0)
     .setPosition(20,80)
     .setSize(200, 19)
     ;

  // ------------- Create hexagon grid -------------
  wide = int(width/rx) + 2;

  tall = int (height/ry) + 2;

  max_hexagons = 40;

  hexagons = new Hexagon [wide] [tall];

  for (int y = 0; y < tall; y++) {
    for (int x = 0; x < wide; x++) {

      if ( x % 2 == 0) {

        hexagons[x] [y] = new Hexagon(x*rx, y*ry);

      } else {

        hexagons[x] [y] = new Hexagon(x*rx, (y-.5)*ry);
      }
    }
  }

  float targetX = width/2;
  float targetY = height/2;

  Xoffset = hexagons [int(wide/2)] [int(tall/2)] .x - targetX;
  Yoffset = hexagons [int(wide/2)] [int(tall/2)] .y - targetY;
}

// ------------- draw -------------
void draw () {
  background(0);
  pushMatrix();
  translate(-Xoffset, -Yoffset);
  for (int y = 0; y < tall; y++) {
    for (int x = 0; x < wide; x++) {

      hexagons[x] [y] .draw();
    }
  }
  if (active_counter > 0 && active_counter < max_hexagons) {
    nextHex();
  }
  popMatrix();
  myColor = lerpColor(c1,c2,n);
    n += (1-n)* 0.1; 
} 
  public void controlEvent(ControlEvent theEvent) {
    println(theEvent.getController().getName());
    n = 0;
  }

  public void startDrawing(int theValue) {
    println("a button event from startDrawing: "+theValue);
    c1 = c2;
    c2 = color(0,0,0);

    if (active_counter == 40) {
      fill(color(0));
    }

  }

// ------------- Mouse interaction -------------
void mousePressed() {

  if (active_counter == 0) {

    hexagons[int(wide/2)][int(tall/2)].active = true;

    active_counter++;
  }
}

// ------------- Drawing next hexagon -------------
void nextHex() {
  int randX = int(random(wide));
  int randY = int(random(tall));
  while (!hexagons[randX][randY] .active) {
    randX = int(random(wide));
    randY = int(random(tall));
  } 
  int randD = int(random(6));
  if (
    randX + stepX[randD] >= 0 && 
    randX + stepX[randD] < wide &&
    randY + stepY[randX % 2][randD] >= 0 && 
    randY + stepY[randX % 2][randD] < tall
    ) {
    if (!hexagons[randX + stepX[randD]][randY + stepY[randX % 2][randD]] .active) {
      active_counter++;
    }
    hexagons[randX + stepX[randD]][randY + stepY[randX % 2][randD]] .active = true;
  }
}
4

0 回答 0