Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';不要用鼠标拖动不同的图像_Java_Processing - Fatal编程技术网

Java Can';不要用鼠标拖动不同的图像

Java Can';不要用鼠标拖动不同的图像,java,processing,Java,Processing,我正在做一个项目,必须为画线的循环编写不同的代码,这样当单击时,循环将切换到下一个。如代码中所示,每个方法都应该在计数到达它们时出现,然后在计数增加时消失。所需的输出是在播放时,它应该显示第一个方法hbars,单击时,它将转到vbars并再次单击到geoLines,依此类推,直到用户定义完成。现在,如果我播放它,它将在一秒钟内通过所有的方法,而无需我点击。抱歉,代码太多了,没有完整的代码,我无法真正解释问题 int count=0; void setup() { size(800, 600

我正在做一个项目,必须为画线的循环编写不同的代码,这样当单击时,循环将切换到下一个。如代码中所示,每个方法都应该在计数到达它们时出现,然后在计数增加时消失。所需的输出是在播放时,它应该显示第一个方法hbars,单击时,它将转到vbars并再次单击到geoLines,依此类推,直到用户定义完成。现在,如果我播放它,它将在一秒钟内通过所有的方法,而无需我点击。抱歉,代码太多了,没有完整的代码,我无法真正解释问题

int count=0;

void setup()
{
  size(800, 600);
}

void draw()
{
  mouseClicked();
}
void hbars()
{
  background(0);
  fill(255);
  for (int y=0; y <height; y+=height/10)
  {
    rect(0, y, width, height/20);
  }
}

void vbars()
{
  background(0);
  fill(255);
  for (int x = 0; x<width; x+=width/8)
  {
    rect(x, 0, width/16, height);
  }
}

void diagonalLines()
{
  background(0);
  fill(255);
  for (int i=0; i<=width*2; i+=width/100)
  {
    line(0, i, i, 0);
  }
}

void geoLines()
{
  background(0);
  int hx = 0;
  int hy = 0;
  int vx = width;
  int vy = height;
  for (int x =0; x < 10; x++)
  {
    line(0, hy, hx, height);
    line(vx, height, 0, vy);
    hy+=height/20;
    hx+=width/20;
    vx-=width/20;
    vy-=height/20;
  }
}

void circles()
{
  background(0);
  fill(255);
  for (int i=0; i<100; i++)
  {
    noFill();
    circle(width/2, height/2, i*10);
  }
}
void fadeRightToLeft(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeLeftToRight(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeDown()
{
  background(0);
  for (int i=0; i <40; i++) {
    if (i%2==1) {
      fadeRightToLeft(i*40);
    } else
    {
      fadeLeftToRight(i*40);
    }
  }
}
void checkerboard()
{
  background(0);
  int count = 0;
  int row = 0;
  for (int r = 0; r < 8; r++)
  {
    for (int c = 0; c < 8; c++)
    {
      if (count % 2 == 0)
      {
        if (row % 2 == 0)
        {
          fill(255);
        } else fill(0);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      } else
      {
        if (row % 2 == 0)
        {
          fill(0);
        } else fill(255);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      }
    }
    row++;
  }
}

void userDefined()
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    stroke(3*i, 160*i, 15*i);
    noFill();
    circle(0+(i*7.5), 0+(i*7.5), i*12);
    circle(0+(i*7.5), 600+(i*7.5), i*40);
  }
}

void mouseClicked()
{
  
  count++;
  
  if (count==1)
    hbars();
  if (count==2)
    vbars();
  if (count==3)
    diagonalLines();
  if (count==4)
    geoLines();
  if (count==5)
    circles();
  if (count==6)
    fadeRightToLeft(0);
  if (count==7)
    fadeLeftToRight(0);
  if (count==8)
    fadeDown();
  if (count==9)
    checkerboard();
  if (count==10)
    userDefined();
}
int count=0;
无效设置()
{
尺寸(800600);
}
作废提款()
{
鼠标点击();
}
void hbars()
{
背景(0);
填充(255);

对于(int y=0;y您几乎拥有了它,您只需避免将对
mouseClicked()
的调用置于
draw()
循环中

为什么它不能正常工作?
mouseClicked()
方法的工作方式类似于事件,这意味着当特定的事件发生时(在本例中是鼠标单击),它将被动态调用。您不需要调用它-一般来说,您不应该通过编程方式调用事件处理程序

draw()
循环每秒运行约60次。由于它调用该方法切换到下一个背景,因此在几分之一秒后,它就没有背景了

我修复了您的代码并修改了一些详细信息(我对此进行了评论,以便您知道原因)。给您:

int count=0;

void setup()
{
  size(800, 600);
  
  // I think that we can call the first background before a user input, but it's your call
  NextBackground();
}

void draw()
{
  // mouseClicked(); // mouseClicked is called as an event, do not call it programatically (also yyou don't need to)
  // Processing wants it's draw() loop so we'll let it here even if it's empty.
}
void hbars()
{
  background(0);
  fill(255);
  for (int y=0; y <height; y+=height/10)
  {
    rect(0, y, width, height/20);
  }
}

void vbars()
{
  background(0);
  fill(255);
  for (int x = 0; x<width; x+=width/8)
  {
    rect(x, 0, width/16, height);
  }
}

void diagonalLines()
{
  background(0);
  fill(255);
  for (int i=0; i<=width*2; i+=width/100)
  {
    line(0, i, i, 0);
  }
}

void geoLines()
{
  background(0);
  int hx = 0;
  int hy = 0;
  int vx = width;
  int vy = height;
  for (int x =0; x < 10; x++)
  {
    line(0, hy, hx, height);
    line(vx, height, 0, vy);
    hy+=height/20;
    hx+=width/20;
    vx-=width/20;
    vy-=height/20;
  }
}

void circles()
{
  background(0);
  fill(255);
  for (int i=0; i<100; i++)
  {
    noFill();
    circle(width/2, height/2, i*10);
  }
}
void fadeRightToLeft(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeLeftToRight(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeDown()
{
  background(0);
  for (int i=0; i <40; i++) {
    if (i%2==1) {
      fadeRightToLeft(i*40);
    } else
    {
      fadeLeftToRight(i*40);
    }
  }
}
void checkerboard()
{
  background(0);
  int count = 0;
  int row = 0;
  for (int r = 0; r < 8; r++)
  {
    for (int c = 0; c < 8; c++)
    {
      if (count % 2 == 0)
      {
        if (row % 2 == 0)
        {
          fill(255);
        } else fill(0);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      } else
      {
        if (row % 2 == 0)
        {
          fill(0);
        } else fill(255);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      }
    }
    row++;
  }
}

void userDefined()
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    stroke(3*i, 160*i, 15*i);
    noFill();
    circle(0+(i*7.5), 0+(i*7.5), i*12);
    circle(0+(i*7.5), 600+(i*7.5), i*40);
  }
}

void mouseClicked()
{
  // I moved this code into it's own method as the mouseClicked method may serve other purposes
  NextBackground();
}

void NextBackground() {
  count++;
  if (count > 10) {
    count = 1;
    // this way you loop in your backgrounds
  }

  // a bunch of "if" will get the job done, but you should use a switch for that part:
  switch (count) {
  case 1:
    hbars();
    break;
  case 2:
    vbars();
    break;
  case 3:
    diagonalLines();
    break;
  case 4:
    geoLines();
    break;
  case 5:
    circles();
    break;
  case 6:
    fadeRightToLeft(0);
    break;
  case 7:
    fadeLeftToRight(0);
    break;
  case 8:
    fadeDown();
    break;
  case 9:
    checkerboard();
    break;
  case 10:
    userDefined();
    break;
  default:
    // this one will be used for any other value, which should never happen, but I'm putting it so you know about it
    hbars();
  }
}
int count=0;
无效设置()
{
尺寸(800600);
//我认为我们可以在用户输入之前调用第一个后台,但这是您的决定
NextBackground();
}
作废提款()
{
//mouseClicked();//mouseClicked作为事件调用,不要以编程方式调用它(也不需要)
//处理需要它的draw()循环,所以即使它是空的,我们也将它放在这里。
}
void hbars()
{
背景(0);
填充(255);

对于(int y=0;y不清楚您在问什么。您能显示所需的输出和当前获得的输出吗?播放所需的输出时,它应显示第一个方法hbars,单击时,它将转到vbars并再次单击geoLines等,直到用户定义,到达该位置后即完成。现在,如果播放它,我将t将在一秒钟内完成所有方法,无需单击。谢谢您的帮助。