彩色棋盘实验报告
一. 思路:
- 初始化棋盘格子,初始化最上和最下两边的颜色参数。
- 监测鼠标点击的位置的颜色和状态(0,-1,1)。
- 定义ball和square,包含了颜色,半径,位置(x,y)属性(property),并且定义了绘画ball和square的方法(method)。
- 在格子边缘确定这个颜色覆盖在哪个格子上。计算出鼠标点击的位置和该位置所占百分比,从而确定格子的位置i,j。
- 每次draw()函数调用时,刷新格子的状态。
二. 实现过程:
- 使用setcolor()来设置初始的上下两边的颜色参数,setNumbers()刷新ifUsed[i][j]数组。
- drawcirle()和drawblocks()设置上下的颜色。drawgrid()设置格子。
- 监测点击所选择的位置是否有填充颜色。具体利用ifUsed[i][j]数组和color[15][15]数组来确定。
- drawMain()刷新颜色和棋盘。
- mousePressed(),mouseReleased(),keyPressed()来检测键盘和鼠标的状态。点击鼠标则拖动方块,按下键盘空格则刷新界面。
三. 代码:见工程文件。
//Dear Reader:CopyRight by qingling
class rec {
color reccolor;
int positionX;
int positionY;
float radius=height/num-3.8;
rec(color bc,
int X, int Y) {
reccolor=bc;
positionX=X;
positionY=Y;
}
void drawrec()
{
noStroke();
fill(reccolor);
rectMode(CENTER);
rect(mouseX, mouseY, height/num-3.8, height/num-3.8);
}
void drawgrid() //Draw the background
{
int i;
for (i=0; i<num; i++)
{
stroke(255, 0, 0);
line(0, height*i/num, width, height*i/num);
line(width*i/num, 0, width*i/num, height);
}
}
}
class ball {
color ballcolor;
int positionX;
int positionY;
float radius=height/num-3.8;
ball(color bc,
int X, int Y) {
ballcolor=bc;
positionX=X;
positionY=Y;
}
void drawball()
{
noStroke();
fill(ballcolor);
ellipse(mouseX, mouseY, height/num-3.8, height/num-3.8);
}
}
Start variable
ball b;
rec r;
int X;//position of XPressed
int Y;//position of YPressed
int x;//position of XReleased
int y;//position of XReleased
int icircle = 0;
int iblocks = 0;
int itemp, jtemp;//save the point's color and time.
int num = 15;//the number of rows and vols
int[][] ifUsed = new int[15][15];//1 for circle;-1 for rec;0 for nothing
color[] colorInit = new color[15];
color[][] colorBlock = new color[15][15]; //15 rows and 15 vols.
boolean ifReleased=false, ifRec=false, ifCircle=false,ifCopy=false;
void setup() {
size(650, 650);
setColor();
background(255);
setNumbers();
}
void draw() {
int i=0, j=0;
background(255);
drawblocks();
drawcirle();
if (mousePressed)
{
drawMain();
i=X/(width/num);//decide the location of blocks;
j=Y/(height/num);
if (Y<height/num && Y>0)
{
r=new rec(colorInit[i], mouseX, mouseY);
r.drawrec();
colorBlock[i][j]=colorInit[i];
ifUsed[i][j]=-1;
} else if (Y>height/num*(num-1) && Y<height)
{
b=new ball(colorInit[i], mouseX, mouseY);
b.drawball();
colorBlock[i][j]=colorInit[i];
ifUsed[i][j]=1;
} else
{
ifCopy=true;
if (ifUsed[i][j]==1)
{
b=new ball(colorBlock[i][j], mouseX, mouseY);
b.drawball();
ifUsed[i][j]=1;
}
else if (ifUsed[i][j]==-1)
{
r=new rec(colorBlock[i][j], mouseX, mouseY);
r.drawrec();
ifUsed[i][j]=-1;
}
}
itemp=i;
jtemp=j;
ifCircle=true;
ifRec=true;
}
if (ifReleased)
{
int px=x/(width/num);//decide the location of blocks;
int py=y/(height/num);
if (y<height/num*(num-1)&&y>height/num&&ifRec&&ifCircle&&!ifCopy&&x>0&&x<width)
{
ifUsed[px][py]=ifUsed[itemp][jtemp];
ifUsed[itemp][jtemp]=0;
colorBlock[px][py]=colorBlock[itemp][jtemp];
colorBlock[itemp][jtemp]=color(0, 0, 0);
}
if(ifCopy&&y<height/num*(num-1)&&y>height/num&&x>0&&x<width)
{
ifUsed[px][py]=ifUsed[itemp][jtemp];
colorBlock[px][py]=colorBlock[itemp][jtemp];
}
drawMain();
}
r=new rec(colorBlock[i][j], mouseX, mouseY);
r.drawgrid();//initial black grid
}
void mousePressed()
{
ifReleased=false;
X=mouseX;
Y=mouseY;
}
void mouseReleased()
{
ifReleased=true;
x=mouseX;
y=mouseY;
}
void keyPressed()
{
if (key==32)
{
setColor();
setNumbers();
}
}
void drawcirle() //Initial circle up
{
icircle=0;
for (; icircle<num; icircle++)
{
noStroke();
fill(colorInit[num-1-icircle]);
ellipse(width*(num-icircle)/num-width/num/2, height-height/num/2, height/num-3.8, height/num-3.8);
}
}
void drawblocks() //Initial blocks up
{
iblocks=0;
for (; iblocks<num; iblocks++)
{
noStroke();
rectMode(CENTER);
fill(colorInit[iblocks]);
rect(width*iblocks/num+width/num/2+1, height/num-height/num/2, height/num-3.8, height/num-3.8);
}
}
void setColor()
{
int i;
for (i=0; i<num; i++)
{
colorInit[i]=color(random(255), random(255), random(255));
}
}
void drawMain()//draw the lefted circles and rects
{
for (int i=0; i<num; i++)
{
for (int j=0; j<num; j++)
{
if (ifUsed[i][j]==1)
{
ifCircle=false;
noStroke();
fill(colorBlock[i][j]);
ellipse(width*i/num+width/num/2, height*j/num+height/num/2, height/num-3.8, height/num-3.8);
} else if (ifUsed[i][j]==-1)
{
ifRec=false;
noStroke();
fill(colorBlock[i][j]);
rectMode(CENTER);
rect( width*i/num+width/num/2+1, height*j/num+height/num/2+1, height/num-3.8, height/num-3.8);
}
}
}
}
void setNumbers()//reset ifUsed[k][j] and colorBlock[k][j] array
{
for (int k = 0; k<num; k++)
{
for (int j = 0; j<num; j++)
{
colorBlock[k][j] = color(0, 0, 0);
ifUsed[k][j] = 0;
}
}
}