Java背景

时间:2013-11-06 20:51:08

标签: java processing

我是Java的新手,我和我的伙伴似乎都无法弄清楚如何为特定部分设置背景。我们需要将牙买加国旗的背景设定为(250,230,82),但仅限于该部分。目前,当我们在Processing 2.0上运行程序时,该行会混淆我们编写的整个流程。这是我们到目前为止所做的:

int x=0;
void setup() {
  size(320, 270);
  background(0);
  noStroke();
  frameRate(240);
}
void draw() {
  //germany, flag 1
  fill(0); //black first horizontal bar
  rect(0+x, 0, width+x, width); 
  fill(255, 0, 0); //red middle horizontal bar
  rect(0+x, 90, width+x, width);
  fill(240, 240, 0); //gold last horizontal bar
  rect(0+x, 180, width+x, width);

  //japan, flag 2
  fill(255); //white background
  rect(0+x-width, 0, width, height);
  fill(255, 0, 0); //red circle middle
  ellipse(width/2+x-width, height/2, 150, 150);

  //france, flag 3
  fill(0, 0, 255); //left blue vertical bar
  rect(0+x-2*width, 0, width/3, height);
  fill(255); //middle white vertical bar
  rect(width/3+x-2*width, 0, width/3, height);
  fill(255, 0, 0); //right red vertical bar
  rect(2.0/3.0*width+x-2*width, 0, width/3, height);

  //brazil, flag 4
  fill(0, 180, 0); //dark green background
  rect(0+x-3*width, 0, width, height);
  fill(230, 230, 0); //gold rhombus
  triangle(width/10+x-3*width, height/2, width/2+x-3*width, 30, width/2+x-3*width, 240);        //gold rhombus left side
  triangle(9.0/10.0*width+x-3*width, height/2, width/2+x-3*width, 30, width/2+x-3*width, 240); //gold rhombus right side
  fill(0, 0, 150); //dark blue circle
  ellipse(width/2+x-3*width, height/2, 120, 120);
  fill(255); //stars
  ellipse(170+x-3*width, 170, 3, 3); //stars 1
  ellipse(177+x-3*width, 185, 3, 3); //stars 2
  ellipse(151+x-3*width, 155, 3, 3); //stars 3
  ellipse(112+x-3*width, 145, 3, 3); //stars 4
  ellipse(132+x-3*width, 165, 3, 3); //stars 5
  ellipse(147+x-3*width, 148, 3, 3); //stars 6
  ellipse(157+x-3*width, 168, 3, 3); //stars 7
  ellipse(180+x-3*width, 188, 3, 3); //stars 8
  ellipse(190+x-3*width, 158, 3, 3); //stars 9

  //sweden, flag 5
  fill(0, 100, 190); //blueish teal background
  rect(0+x-4*width, 0, width, height); //the "-4*width" needs to be added after every
  fill(240, 210, 0); // gold nordic cross //^first x-place in order for the flags to follow one after
  rect(width/4+x-4*width, 0, 30, height); //another like a big continuous flag, so the subsequent 
  rect(0+x-4*width, height/2-15, width, 30); //ones will be "-5*width", "-6*width", etc
  //the "-15"^ is to center the horizontal line

  //norsefire, flag 6
  fill(0); //black background
  rect(0+x-5*width, 0, width, height);
  fill(255, 0, 0); //red double cross thing
  rect(0+x-5*width, height/2-15, width, 30);//horizontal line
  rect(width/8+x-5*width, 0, 30, height);//vertical line 1
  rect(3.0/8.0*width+x-5*width, 0, 30, height);//vertical line 2

  //chile, flag 7
  fill(0, 0, 250); //blue left horizontal bar
  rect(0+x-6*width, 0, 125, 145); 
  fill(250); //white right horizontal bar
  rect(125+x-6*width, 0, 320, 145);
  fill(250, 0, 0); //red last horizontal bar
  rect(0+x-6*width, 145, 320, 270);
  line(42+x-6*width, 53, 63, 30);
  line(82+x-6*width, 53, 63, 30);
  line(82+x-6*width, 53, 105, 58);
  line(15+x-6*width, 58, 42, 53);
  line(90+x-6*width, 75, 105, 58);
  line(35+x-6*width, 75, 15, 58);
  line(35+x-6*width, 75, 25, 95);
  line(25+x-6*width, 95, 53, 88);
  line(53+x-6*width, 88, 95, 95);
  line(95+x-6*width, 95, 90, 75); 

  //jamaica, flag 8
  //  background(250, 230, 82); // This line is messing everything up
  fill (0, 250, 0); //green triangle top
  triangle (10+x-7*width, 0, 160+x-7*width, 120, 310+x-7*width, 0);
  fill (0, 250, 0); //green triangle bot
  triangle (10+x-7*width, 270, 160+x-7*width, 150, 310+x-7*width, 270);
  fill (0); //black triangle left
  triangle (0+x-7*width, 10, 145+x-7*width, 135, 0+x-7*width, 260);
  fill (0); //black triangle right
  triangle (320+x-7*width, 10, 175+x-7*width, 135, 320+x-7*width, 260);

  //puerto rico, flag 9 
  fill(0, 0, 250); //blue triangle
  triangle(0+x-8*width, 0, 0+x-8*width, 270, 166+x-8*width, 135);
  fill(255, 0, 0); //red quad 1
  quad(0+x-8*width, 0, 54+x-8*width, 54, 320+x-8*width, 54, 320+x-8*width, 0);
  fill(250); //white quad 1
  quad(54+x-8*width, 54, 108+x-8*width, 108, 320+x-8*width, 108, 320+x-8*width, 54);
  fill(255, 0, 0); //red quad 2
  quad(108+x-8*width, 108, 135+x-8*width, 135, 320+x-8*width, 135, 320+x-8*width, 108);
  fill(255, 0, 0); //red quad 3
  quad(135+x-8*width, 135, 108+x-8*width, 162, 320+x-8*width, 162, 320+x-8*width, 135);
  fill(250); //white quad 2
  quad(108+x-8*width, 162, 320+x-8*width, 162, 320+x-8*width, 216, 54+x-8*width, 216);
  fill(250, 0, 0); //red quad 4
  quad(54+x-8*width, 216, 0+x-8*width, 270, 320+x-8*width, 270, 320+x-8*width, 216);

  //Kuwait flag, 10
  fill(80, 168, 62); //green quad
  quad(0+x-9*width, 0, 80+x-9*width, 90, 320+x-9*width, 90, 320+x-9*width, 0);
  fill(250); //white quad
  quad(80+x-9*width, 90, 80+x-9*width, 180, 320+x-9*width, 180, 320+x-9*width, 90);
  fill(250, 0, 0); //red quad
  quad(80+x-9*width, 180, 0+x-9*width, 270, 320+x-9*width, 270, 320+x-9*width, 180);
  fill(0); //black quad
  quad(0+x-9*width, 0, 0+x-9*width, 270, 80+x-9*width, 180, 80+x-9*width, 90);

  x=x+1; //scrolls flags to right

  if (x==9*width) { //resets flag scrolling, the number multiplied 
    x=0; //to the width is how many flag widths the first flag has moved
  }   // so the end result will be 9 widths bc we have 10 flags
}

4 个答案:

答案 0 :(得分:0)

在Processing中,background()方法会覆盖已使用背景颜色绘制的所有内容。但是,在调用之前,您似乎已经绘制了一些东西。尝试使用fill()设置颜色,然后quad()仅绘制该标记的背景。

答案 1 :(得分:0)

您必须在绘制线条之前使用笔触(颜色)设置线条颜色,然后使用noStroke()

设置线条关闭

答案 2 :(得分:0)

因为你正在绘制一面大墙旗并在画布(相机)之前移动这个墙,你不能仅为这个小的移动部分改变背景它会影响整个墙壁的旗帜。那么为什么你不要一直使用你的重叠矩形程序,这样你的牙买加国旗就可以了。

//jamaica, flag 8
fill(250,230, 82); // yellow background
rect(0+x-7*width,0,width, width);
fill (0, 250, 0); //green triangle top
triangle (10+x-7*width, 0, 160+x-7*width, 120, 310+x-7*width, 0);
fill (0, 250, 0); //green triangle bot
triangle (10+x-7*width, 270, 160+x-7*width, 150, 310+x-7*width, 270);
fill (0); //black triangle left
triangle (0+x-7*width, 10, 145+x-7*width, 135, 0+x-7*width, 260);
fill (0); //black triangle right
triangle (320+x-7*width, 10, 175+x-7*width, 135, 320+x-7*width, 260);

btw for height参数最好始终使用height而不是width

答案 3 :(得分:0)

它的旧帖子,但这可能会有所帮助

Image img = Toolkit.getDefaultToolkit().createImage("JamaicaFlag.jpeg");

此页面应该为您提供更多信息

Toolkit.createImage