如何在Java中的各种变量中使用图片的文件名?

时间:2019-07-06 13:26:29

标签: java image loops arduino processing

我的代码加载并将图像绘制到屏幕上。每个图像都有一个四位数的名称,例如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);  
}

1 个答案:

答案 0 :(得分:2)

答案是“是的,只要您将此信息保存在某个地方(或在每次需要时提取它)。”但是,这可能会容易得多:您可以拥有一个设计的类的数组,而不是拥有一个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数组!当然,我设计得很快,因此您可以很好地利用所需的一切来丰富它。

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