作者 主题: 添加控制按钮  (阅读 4247 次)

Homway

  • Newbie
  • *
  • 帖子: 12
添加控制按钮
« 于: 十二月 12, 2011, 08:20:10 下午 »
程序代码
color White_C = color(255, 255, 255),
Black_C = color(0, 0, 0);
final int Side = 60, // Side of cell
Rank = 5;  //Number of cells across = number of cells down
final int W = Side * Rank, // Width of canvas
H = W; // Height of canvas
int[][] Vals = new int[Rank][Rank];//To hold cell values
PFont Font; // Font for rendering cell values
PFont DFont; // Font for rendering direction values

void setup() {
  size(W, H);
  Font = loadFont("Arial-BoldMT-36.vlw");
  textFont(Font);
  DFont = loadFont("Century-18.vlw");
  textFont(CFont);
  Random_Populate_Array();
  Draw_Vals();
  Draw_Controls();
}

final int Hi = 4, // Max cell value
Lo = -2;// Min cell value

void Random_Populate_Array() {
  int i,
  j,
  k;
   for (i = 0; i < Rank; i++) {
    for (j = 0; j < Rank; j++) {
     for (k = 0; k < Rank*Rank; k++) {
        int r = int(random(Lo,Hi));
        Vals[i][j] = r;
     }
    }
   }
}
       
void Draw_Vals() {
  int i,
  j;
  int R;
  for(i = 0; i < Rank; i++) {
    for(j = 0; j < Rank; j++) {
      fill(White_C);
      rect(i * Side,j * Side, Side, Side);
      fill(Black_C);     
      textAlign(CENTER, CENTER);
      text(Vals[i][j], (i + 0.5) * Side,
      (j + 0.5) * Side);
    }
  }
}

String[] Legends = {"Up", "Right", "Down", "Left"};
void Draw_Controls() {
 
}



我想在输出方格的下方,添加一排控制按钮,来移动输出的随机数字,该在void Draw_Controls()里添加什么?
我已经参考了processing.org/learning/topics/button.html
但是我在添加了int rectX, rectY;boolean rectOver = false;
之后就无从下手了,不懂如何选定一个将要移动的数字,也不会在底部再添加一排按钮。。。
« 最后编辑时间: 十二月 12, 2011, 09:08:56 下午 作者 hongweilala »

vinjn

  • SuperManager
  • Hero Member
  • *****
  • 帖子: 586
Re: 添加控制按钮
« 回复 #1 于: 十二月 12, 2011, 10:29:48 下午 »
编程不是拼凑代码,不是复制黏贴
learning processing你看完了没?

Homway

  • Newbie
  • *
  • 帖子: 12
Re: 添加控制按钮
« 回复 #2 于: 十二月 12, 2011, 11:18:26 下午 »
编程不是拼凑代码,不是复制黏贴
learning processing你看完了没?
在看,主要是沒什麽思路

Homway

  • Newbie
  • *
  • 帖子: 12
Re: 添加控制按钮
« 回复 #3 于: 十二月 15, 2011, 05:02:02 上午 »
程序代码
color White_C = color(255, 255, 255),
      Black_C = color(0, 0, 0),
      Green_C = color(0, 255, 0),
      Yellow_C = color(255, 255, 0);
final int Side = 60, // Side of cell
          Rank = 5;  //Number of cells across = number of cells down
final int W = Side * Rank, // Width of canvas
          H = W + Side; // Height of canvas
int[][] Vals = new int[Rank][Rank];//To hold cell values
PFont Font; // Font for rendering cell values
PFont DFont; // Font for rendering direction values
String[] Legends = {"Up", "Right", "Down", "Left"};
// circle original position.
int Xposition = 30;
int Yposition = 270;

void setup() {
  size(W, H);
  Font = loadFont("Arial-BoldMT-36.vlw");
  DFont = loadFont("Century-18.vlw");
  textFont(Font);
  Random_Populate_Array();
  Draw_Squares();
  Draw_Vals();
  Draw_Controls();
}

void draw(){
}

final int Hi = 4, // Max cell value
          Lo = -2;// Min cell value

// This draws the squares the 'i' is the Columns and 'j' is the Rows.
void Draw_Squares() {
  for(int i = 0; i < Rank; i++) {
    for(int j = 0; j < Rank; j++) {
     fill(Yellow_C);
     stroke(Green_C);
     rect(i * Side,j * Side, Side, Side);
    }
  }


void Random_Populate_Array() {
   for (int i = 0; i < Rank; i++) {
   for (int j = 0; j < Rank; j++) {
   Vals[i][j] = int(random(Lo,Hi));
   }
  }
}

//This Draws the random values between -2 and 4       
void Draw_Vals() {
  for(int i = 0; i < Rank; i++) {
    for(int j = 0; j < Rank; j++) {
      fill(Black_C);
      textFont(Font);
      textAlign(CENTER, CENTER);
      text(Vals[i][j], (i + 0.5) * Side, (j + 0.5) * Side);
    }
  }
}

void Draw_Controls() {
  textFont(DFont);
  for(int i = 0; i < Rank; i++){
  for(int j = 5; j < Rank + 1; j++){
  fill (Green_C);
  stroke (Black_C);
  rect(i * Side, j * Side, Side, Side);
  if(i > 0 && i < 5){
          fill(Black_C);
          textAlign(CENTER, CENTER);
          text(Legends[i-1], (i + 0.5) * Side, (j + 0.5) * Side);
         
      }
  else{ 
        fill(Yellow_C);
        rect(i * Side, j * Side, Side, Side);
       
// draw circle.       
 if (Yposition == 270 && Xposition == 30)
{
stroke(Green_C);
noFill();
ellipse(Side/2,4.5*Side,Side,Side); 
} else{
  }
      }
    }
  }
}

void Update_Position(String ChangePosition) {
  Draw_Squares();
  Draw_Vals();
  Draw_Controls();
 
 if (ChangePosition == "UP") 
    {
      if (Yposition - Side > 0){
     
      Yposition = Yposition - 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      } else{
        stroke(Green_C);
        noFill();
        ellipse(Xposition,Yposition,Side,Side);
        }
  }
   if (ChangePosition == "Right")
    {
     if (Xposition + Side < 300){
     
      Xposition = Xposition + 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      } else{
        stroke(Green_C);
        noFill();
        ellipse(Xposition,Yposition,Side,Side);
        }
  }
   if (ChangePosition == "Down") 
    {
       if (Yposition + Side < 300){
     
      Yposition = Yposition + 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      } else{
         stroke(Green_C);
         noFill();
         ellipse(Xposition,Yposition,Side,Side);
        }
  }
   if (ChangePosition == "Left")
    {
        if (Xposition - Side > 0){
     
      Xposition = Xposition - 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      }  else{
        stroke(Green_C);
        noFill();
        ellipse(Xposition,Yposition,Side,Side);
        }
  }
}

void mousePressed() { 
  if( mouseY > 300 && mouseY < 360 ){
    if ( mouseX > 60 && mouseX < 120 ){Update_Position("UP");}
    if ( mouseX > 120 && mouseX < 180 ){Update_Position("Right");}
    if ( mouseX > 180 && mouseX < 240 ){Update_Position("Down");}
    if ( mouseX > 240 && mouseX < 300 ){Update_Position("Left");}
  }
}


第一張图是我的输出结果,我想在这个基础上给这个程序添加去除、记录数字并计算的功能
如下面所示,图一的圆圈在数字2处,左下角的计算框里数字为0(未记录数字)。图二圆圈向上移动到了4,原来方框里的2被移除,并记录在了计算框里。图三计算框里的6=图1的2加上图2的4. 请问我该怎麼做?


Homway

  • Newbie
  • *
  • 帖子: 12
Re: 添加控制按钮
« 回复 #4 于: 十二月 15, 2011, 11:28:02 下午 »
程序代码
color White_C = color(255, 255, 255),
      Black_C = color(0, 0, 0),
      Green_C = color(0, 255, 0),
      Yellow_C = color(255, 255, 0);
final int Side = 60, // Side of cell
          Rank = 5;  //Number of cells across = number of cells down
final int W = Side * Rank, // Width of canvas
          H = W + Side; // Height of canvas
int[][] Vals = new int[Rank][Rank];//To hold cell values
PFont Font; // Font for rendering cell values
PFont DFont; // Font for rendering direction values
String[] Legends = {"Up", "Right", "Down", "Left"};
// circle original position.
int Xposition = 30;
int Yposition = 270;

int p,q;
int sum; //hold value of Running_Total in bottom left cell

void setup() {
  size(W, H);
  Font = loadFont("Arial-BoldMT-36.vlw");
  DFont = loadFont("Century-18.vlw");
  textFont(Font);
  text(sum,Side/2,(Side/2)+5*Side);
  Random_Populate_Array();
  Draw_Squares();
  Draw_Vals();
  Draw_Controls();
}

void draw(){
}

final int Hi = 4, // Max cell value
          Lo = -2;// Min cell value

// This draws the squares the 'i' is the Columns and 'j' is the Rows.
void Draw_Squares() {
  for(int i = 0; i < Rank; i++) {
    for(int j = 0; j < Rank; j++) {
     fill(Yellow_C);
     stroke(Green_C);
     rect(i * Side,j * Side, Side, Side);
    }
  }


void Random_Populate_Array() {
   for (int i = 0; i < Rank; i++) {
   for (int j = 0; j < Rank; j++) {
   Vals[i][j] = int(random(Lo,Hi));
   }
  }
}

//This Draws the random values between -2 and 4       
void Draw_Vals() {
  for(int i = 0; i < Rank; i++) {
    for(int j = 0; j < Rank; j++) {
      fill(Black_C);
      textFont(Font);
      textAlign(CENTER, CENTER);
      text(Vals[i][j], (i + 0.5) * Side, (j + 0.5) * Side);
    }
  }
}

void Draw_Controls() {
  textFont(DFont);
  for(int i = 0; i < Rank; i++){
  for(int j = 5; j < Rank + 1; j++){
  fill (Green_C);
  stroke (Black_C);
  rect(i * Side, j * Side, Side, Side);
  if(i > 0 && i < 5){
          fill(Black_C);
          textAlign(CENTER, CENTER);
          text(Legends[i-1], (i + 0.5) * Side, (j + 0.5) * Side);
         
      }
  else{ 
        fill(Yellow_C);
        rect(i * Side, j * Side, Side, Side);
       
// draw circle.       
 if (Yposition == 270 && Xposition == 30)
{
stroke(Green_C);
noFill();
ellipse(Side/2,4.5*Side,Side,Side); 
} else{
  }
      }
    }
  }
}

void Update_Position(String ChangePosition) {
  Draw_Squares();
  Draw_Vals();
  Draw_Controls();
  Running_Total();
 
 if (ChangePosition == "UP") 
    {
      if (Yposition - Side > 0){
     
      Yposition = Yposition - 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      } else{
        stroke(Green_C);
        noFill();
        ellipse(Xposition,Yposition,Side,Side);
        }
  }
   if (ChangePosition == "Right")
    {
     if (Xposition + Side < 300){
     
      Xposition = Xposition + 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      } else{
        stroke(Green_C);
        noFill();
        ellipse(Xposition,Yposition,Side,Side);
        }
  }
   if (ChangePosition == "Down") 
    {
       if (Yposition + Side < 300){
     
      Yposition = Yposition + 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      } else{
         stroke(Green_C);
         noFill();
         ellipse(Xposition,Yposition,Side,Side);
        }
  }
   if (ChangePosition == "Left")
    {
        if (Xposition - Side > 0){
     
      Xposition = Xposition - 60 ;
      stroke(Green_C);
      noFill();
      ellipse(Xposition,Yposition,Side,Side);
      }  else{
        stroke(Green_C);
        noFill();
        ellipse(Xposition,Yposition,Side,Side);
        }
       
    }
}

void mousePressed() { 
  if( mouseY > 300 && mouseY < 360 ){
    if ( mouseX > 60 && mouseX < 120 ){Update_Position("UP");}
    if ( mouseX > 120 && mouseX < 180 ){Update_Position("Right");}
    if ( mouseX > 180 && mouseX < 240 ){Update_Position("Down");}
    if ( mouseX > 240 && mouseX < 300 ){Update_Position("Left");}
  }
}
 

void Running_Total(){               
p=(((2*Yposition)-Side)/(2*Side));
q=(((2*Xposition)-Side)/(2*Side));
sum = sum +(Vals[q][p]);
fill(255,255,0);
   rect(0,5*Side,Side,Side);
   fill(0,0,0);
   textFont(DFont);
   text(sum,Side/2,(Side/2)+5*Side);
   pushStyle();
   fill(255,255,0);
   stroke(255,255,0);
   rectMode(CENTER);
   rect(Xposition,Yposition,Side-5,Side-5);
   popStyle();
   Vals[q][p]=10;
   

   noFill();
   print(sum);
   println();
   
}

我修改好了,但是有个问题就是,每个被移除的数字框都会留下一个10,要怎麼改进?

Homway

  • Newbie
  • *
  • 帖子: 12
Re: 添加控制按钮
« 回复 #5 于: 十二月 16, 2011, 12:19:30 上午 »

上面的問題解決,刪除了Vals[q][p]=10;

但是去掉之后,那个格子里的数字又会重新生成,这怎麼搞,我需要移动过的格子是空的

Tags: