简体   繁体   English

处理 Java - 绘制日食或加载图片的问题

[英]Processing Java - Problem with draw an eclipse or load pic

i'm testing a code in Processing with Java for my school.我正在为我的学校测试用 Java 处理的代码。

I try to create a game and i have a problem to draw an eclipse or to load a picture.我尝试创建一个游戏,但在绘制日食或加载图片时遇到问题。 I think the picture or the eclipse is drawing under my game board .. I don't know how to solved it.我想我的游戏板下有图画或日食..我不知道如何解决它。

I have a txt file for the game board ( by level).我有一个游戏板的 txt 文件(按级别)。 An example :一个例子 :

110000000 000000031 000000000 100000000 000000000 000000000 200000001 110000000 000000031 000000000 100000000 000000000 000000000 200000001

Please, can you help me thank you请帮帮我谢谢

int cols, rows, w, x, y,level;
String lines[];
PImage flag;

void setup() {
  size(460,360);
  cols = 9;
  rows = 7;
  w = 50 ;
  x= 0;
  y = 0;
  level = 1;
  lines = loadStrings("../../data/niveau"+level+".iwk");
  flag = loadImage("../../data/flag.png");
  ellipseMode(CORNER);
}

void draw() {
  String lines[]= loadStrings("../../data/niveau"+level+".iwk");
  loadCard(cols,rows,w,x,y,lines,flag);
}

void loadCard(int cols, int rows, int w, int x,int y,String lines[],PImage flag) {

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {

      if(lines[i].charAt(j) == '1'){
        fill(156,  158,  162);
      }
      else if(lines[i].charAt(j) == '2'){
        fill(225,  169,  26) ;
        ellipse(x,y,w/2,w/2);
      }
      else if(lines[i].charAt(j) == '3'){
        image(flag,x,y,w/2,w/2);

      }else {
        fill(23,  159,  215);
      }

      rect(x, y, w, w);
      x = x + w ;
    }
    y = y + w ;
    x = 0 ;
  }
}

It is indeed drawn under the game board.它确实是在游戏板下绘制的。

Check the loadCard function.检查 loadCard 功能。 First the ellipse / image is draw, then a rect is draw with the same x / y, ergo on top of it.首先绘制椭圆/图像,然后在其顶部绘制具有相同 x / y 的矩形。

A modified your code a little, it should show the ellipses / image A 稍微修改了您的代码,它应该显示椭圆/图像

void loadCard(int cols, int rows, int w, int x,int y,String lines[],PImage flag) {

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {

      //draw default so ellipse / img has a background
      fill(23,  159,  215);
      rect(x, y, w, w);

      //draw other cases on top of default    
      if(lines[i].charAt(j) == '1'){
        fill(156,  158,  162);
        rect(x, y, w, w);
      }
      else if(lines[i].charAt(j) == '2'){
        fill(225,  169,  26) ;
        ellipse(x,y,w/2,w/2);
      }
      else if(lines[i].charAt(j) == '3'){
        image(flag,x,y,w/2,w/2);
      }
      x = x + w ;
    }
    y = y + w ;
    x = 0 ;
  }
}

Since you're learning, allow me to give you 2 tips:既然你在学习,请允许我给你两个提示:

loadStrings() needs to happen only once per level. loadStrings() 每个级别只需要发生一次。 You should not put it in draw(), because draw() is called every frame.你不应该把它放在 draw() 中,因为 draw() 每一帧都会被调用。 It is already called in setup, that is fine for now.它已经在设置中调用,现在很好。 Eventually you can put it in a separate function, and call this function at the beginning of a new level.最终你可以把它放在一个单独的函数中,并在一个新的关卡开始时调用这个函数。

If you use a double for loop to draw a game board, you can use the iterators (int i and int j) as x/y variables.如果您使用双循环来绘制游戏板,您可以使用迭代器(int i 和 int j)作为 x/y 变量。 Instead of rect(x, y, w, w);而不是rect(x, y, w, w); you can use rect(i*w, j*w, w, w);你可以使用rect(i*w, j*w, w, w); . . This way you'll have fewer variables to manage.这样,您将需要管理的变量更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM