【问题标题】:How can I use the filename of an image in various variables in Java?如何在 Java 的各种变量中使用图像的文件名?
【发布时间】:2019-07-06 13:26:29
【问题描述】:

我的代码加载并将图像绘制到屏幕上。每个图像都有一个四位数的名称,例如0102.png。前两位 (01) 定义图像编号,后两位 (02) 定义图像的显示持续时间。

如何获取图像的文件名并在循环和其他变量中使用它们?

import processing.serial.*;

Serial myPort; 
int n, dataIn;
int maxImages = 5;
int imageIndex = 0; 
PrintWriter output;
int i, j, k;
int a = 20;
String s, p;

void setup()
{
    size(100, 100);

    output = createWriter("a.txt");
    myPort = new Serial(this, "COM6", 38400);

    PImage[] images = new PImage[maxImages];     

    for (j = 0; j < images.length; j++) 
    {
        images[j] = loadImage(j + ".png");
        frameRate(1);
        images[j].loadPixels();
        imageIndex = (imageIndex + 1) % images.length;     
    }
}

void draw()
{
    function();
    //image(images[j], 0, 0);  
}

【问题讨论】:

  • 的循环时间,你能解释一下这是什么意思吗?
  • 循环时间意味着我想循环运行图像n次。
  • 抱歉,仍然不确定您的意思。我有图像“1.jpg”,我想循环运行 2 次 - 这是什么意思?你要画2次吗?
  • @vs97 Asker 的“循环”可能意味着“显示持续时间”。也许它是显示下一张图片之前的计时器值?...不过,我只是在猜测。

标签: java image loops arduino processing


【解决方案1】:

答案是“可以,只要您将这些信息保存在某个地方(或在每次需要时提取它)”。不过,这可能会容易得多:您可以拥有一个包含您需要的所有信息的类的数组,而不是一个 PImage 数组,有点像这样:

class MyImage {
  protected PImage _image;
  protected PVector _size, _location;
  protected int _imageNumber;
  protected int _loopingTime;

  public MyImage(String imagePath, int imageNumber, int loopingTime, PVector location) {
    _image = loadImage(imagePath);
    _size = new PVector(_image.width, _image.height);
    _location = location;
    _imageNumber = imageNumber;
    _loopingTime = loopingTime;
  }

  public void SetImageLocation(int xx, int yy) {SetImageLocation(new PVector(xx, yy));}
  public void SetImageLocation(PVector v) {_location = v;}
  public int GetImageNumber() {return _imageNumber;}
  public int GetLoopingTime() {return _loopingTime;}
  public PVector GetSize() {return _size;}
  public PVector GetLocation() {return _location;}

  public void DrawImage(){
    image(_image, _location.x, _location.y, _size.x, _size.y);
  }
}

稍后在您的代码中,您可以初始化此类的数组而不是 PImage 数组!当然我设计得很快,所以你最好用你需要的一切来丰富它。

如果您有任何问题,我会留下来。玩得开心!

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多