作者 主题: 我的一个 小画板  (阅读 2459 次)

yili5078

  • Newbie
  • *
  • 帖子: 1
我的一个 小画板
« 于: 六月 08, 2012, 08:14:41 下午 »
如下是我做的一个画板 但是我想怎么 把 这个颜色选择器 设置 c 键 呼出。 求大神帮忙

~~~
Square[] array = new Square[625];
color pickedColour;
ColorPicker offscreen;

ColorPicker cp;
void setup() {
  size(500, 500);
  background(0);
  stroke(0);
  int i = 0;
  for (int x = 1.5; x < width; x += 20) {
    for (int y = 1.5; y < height; y += 20) {
      array = new Square(x,y);
      i++;
    }
  }
  println(i);
  cp = new ColorPicker( 10, 10, 400, 400, 255 );
}

void draw() {
  for (int i = 0; i < 625; i++)
    array.paint();
    cp.render(key);
}

void mousePressed() {
  for (int i = 0; i < 625; i++)
    array.mousePressed();
}

class Square {
  int x, y;
  int r, g, b;
  int t= random(255);
 
  Square(int _x, int _y) {
    x = _x;
    y = _y;
    r = 0;
    g = 0;
    b = 0;
  }
 
  void paint() {
    fill(r, g, b,t);
    rect(x, y, 18, 18);
  }
 
  void mousePressed() {
    if ((mouseX >= x) && (mouseY >= y) && (mouseX < x+18) && (mouseY < y+18)) {
      if (mouseButton == LEFT) {
        //r = random(255);
        //g = random(255);
        //b = random(255);
        r = red(pickedColour);
        g = green(pickedColour);
        b = blue(pickedColour);
      }
      else if (mouseButton = RIGHT) {
        pickedColour = get(mouseX, mouseY);
      }
    }
  }
}
 
public class ColorPicker
{
  int x, y, w, h, c;
  PImage cpImage;
 
  public ColorPicker ( int x, int y, int w, int h, int c )
  {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.c = c;
   
    cpImage = new PImage( w, h );
     
    init();
  }
   
  private void init ()
  {
    // draw color.
    int cw = w - 60;
    for( int i=0; i<cw; i++ )
    {
      float nColorPercent = i / (float)cw;
      float rad = (-360 * nColorPercent) * (PI / 180);
      int nR = (int)(cos(rad) * 127 + 128) << 16;
      int nG = (int)(cos(rad + 2 * PI / 3) * 127 + 128) << 8;
      int nB = (int)(Math.cos(rad + 4 * PI / 3) * 127 + 128);
      int nColor = nR | nG | nB;
         
      setGradient( i, 0, 1, h/2, 0xFFFFFF, nColor );
      setGradient( i, (h/2), 1, h/2, nColor, 0x000000 );
    }
      
    // draw black/white.
    drawRect( cw, 0,   30, h/2, 0xFFFFFF );
    drawRect( cw, h/2, 30, h/2, 0 );
      
    // draw grey scale.
    for( int j=0; j<h; j++ )
    {
      int g = 255 - (int)(j/(float)(h-1) * 255 );
      drawRect( w-30, j, 30, 1, color( g, g, g ) );
    }
  }

  private void setGradient(int x, int y, float w, float h, int c1, int c2 )
  {
    float deltaR = red(c2) - red(c1);
    float deltaG = green(c2) - green(c1);
    float deltaB = blue(c2) - blue(c1);

    for (int j = y; j<(y+h); j++)
    {
      int c = color( red(c1)+(j-y)*(deltaR/h), green(c1)+(j-y)*(deltaG/h), blue(c1)+(j-y)*(deltaB/h) );
      cpImage.set( x, j, c );
    }
  }
   
  private void drawRect( int rx, int ry, int rw, int rh, int rc )
  {
    for(int i=rx; i<rx+rw; i++)
    {
      for(int j=ry; j<ry+rh; j++)
      {
        cpImage.set( i, j, rc );
      }
    }
  }
   
  public void render ()
  {
    image( cpImage, x, y );
    if( mousePressed &&
   mouseX >= x &&
   mouseX < x + w &&
   mouseY >= y &&
   mouseY < y + h )
    {
      c = get( mouseX, mouseY );
    }
    fill( c );
    rect( x, y+h+10, 20, 20 );
  }
}
~~~
notice: 鼠标右键 选择颜色 左键粘贴颜色

panhao1

  • Newbie
  • *
  • 帖子: 13
  • 华科学弟多苦逼 泪看学姐变人妻
    • ncf零社区
Re: 我的一个 小画板
« 回复 #1 于: 七月 16, 2012, 11:19:14 下午 »
for (int x = 1.5; x < width; x += 20) {
    for (int y = 1.5; y < height; y += 20) {
      array = new Square(x,y);

麻烦先改下逻辑错误和语法错误啦 ~
msn已经闲置
个人邮箱 4812ph@163.com
企业邮箱 panhao1@ncf-china.com