【问题标题】:Array of Images-Getting the same errors图像数组 - 得到相同的错误
【发布时间】:2021-01-03 17:18:25
【问题描述】:

每当我写这段代码时:

PImage[] flowers=new PImage[3];
Bubble[] bubbles=new Bubble[5];

void setup() {
  size(640, 360);
  for(int i=0; i<flowers.length; i++){
    flowers[i]=loadImage(“flower”+i+".jpg");
  }
  for(int i=0; i<bubbles.length; i++){
    int index=int(random(0,flowers.length));
    bubbles[i]=new Bubble(flowers[index],100+i*100, 300, random(32, 72));
  }
}

void draw() {
  background(255);
  for (int i=0; i<bubbles.length; i++){
    bubbles[i].display();
    bubbles[i].ascend();
    bubbles[i].top();
  }
}

AND CLASS:
class Bubble {
  float x;
  float y;
  float diameter;
  PImage img;

  Bubble(PImage tempImg, float tempX, float tempY, float tempD) {
    x=tempX;
    y=tempY;
    diameter=tempD;
    img=tempImg;
  }

  void display() {
    stroke(0);
    fill(127);
    image(img,x, y, diameter, diameter);
  }

  void ascend() {
    y–;
    x=x+random(-2, 2);
  }

  void top() {
    if (y<diameter/2) {
      y=diameter/2;
    }
  }
}

我总是收到错误提示“文件“flower0.jpg”丢失或无法访问,请确保 URL 有效或文件已添加到您的草图中并且可读。 文件“flower1.jpg”丢失或无法访问,请确保 URL 有效或该文件已添加到您的草图并且可读。 文件“flower2.jpg”丢失或无法访问,请确保 URL 有效或该文件已添加到您的草图并且可读。 空指针异常 无法运行草图(目标 VM 无法初始化)。 有关更多信息,请阅读 revisions.txt 和 Help ?疑难解答。''

即使我已经仔细检查了我保存图像的方式和位置(名称等),但我仍然无法克服这一点。

【问题讨论】:

  • 与可执行文件运行的路径相关的图像在哪里。我不确定这是否会在您的可执行文件运行的路径中搜索图像,或者它是否会默认为“C:\”。最好有图片的完整路径,这样不会有歧义。
  • 路径类有很多方法来协助文件处理。 docs.microsoft.com/en-us/dotnet/api/system.io.path?view=net-5.0

标签: processing


【解决方案1】:

Processing 有一种非常简单的方法可以让您访问本地文件而无需过多担心路径。在草图的文件夹中,创建一个data 文件夹,然后将您的图像放入其中。

这是一个名为“hexa.png”的图像文件的示例:

您的代码需要 3 张图片,flower0.jpgflower1.jpgflower2.jpg。如果您像我刚刚向您展示的那样设置草图,那么事情应该会顺利进行(假设草图的其余部分处于工作状态)。

玩得开心!

【讨论】:

  • 我按照你说的做了,但我仍然遇到同样的错误。在我真正自己制作之前应该有一个数据文件夹吗?
  • 自己添加就可以了,应该会在data文件夹中找到图片。我告诉你要解决的问题是:这是一个错误(我将从答案中编辑它)。我必须指定“data/”将文件保存在那里,我没有注意到在那个草图中我正在保存而不是加载。使用flowers[i]=loadImage(“flower”+i+".jpg"); 再试一次,它会找到图像。
  • 原来文件扩展名被隐藏在win10中所以我不断添加.jpg两次!
【解决方案2】:

原来文件扩展名隐藏在win10中,所以我不断添加.jpg两次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    相关资源
    最近更新 更多