-1

I have a problem with a Vector of BufferedImage objects. I'm trying to add BufferedImage objects into the Vector but the Vector is always empty. The ImageIo.read(input1) seems not working but I don't understand why.

Here's a part of my code :

private void drawPixel(int index,String name) throws IOException {
    File input1 = new File("pince.png");
    BufferedImage img = ImageIO.read(input1);
    Graphics g = this.imagePoints.getGraphics();
    imagePixelLabelBuffered.add(img);
    input1.delete();
    changeColorPixelLabel(imagePixelLabelBuffered.get(labelClassesCount-1),labelClassesCount-1);
    File output = new File("pince.png");
    //ImageIO.write(imagePixelLabelBuffered.get(labelClassesCount-1), "PNG", output);
    int x = (index % this.width);
    int y = (index / this.width);
    g.drawImage(imagePixelLabelBuffered.get(labelClassesCount-1),x-20, y-31,100,100, null);
    repaint();
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
kazrak
  • 3
  • 6

2 Answers2

0

My guess is that file "pince.png" is not in the current directory. I would recommend you to put your images in the classpath, and load them via Class.getResource() or getResourceAsStream().

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

You add the BufferedImage to the vector using imagePixelLabelBuffered.add(img) and you never modify the variable img, so your problem is not with the vector or the BufferedImage.

I do think your problem is in the line changeColorPixelLabel(imagePixelLabelBuffered.get(labelClassesCount-1),labelClassesCount-1) and more particularly with labelClassesCount-1. This variable is never updated, and it is simply useless. You can use the following line:

changeColorPixelLabel(img, imagePixelLabelBuffered.size()-1);
  • In Java, a List has a method size that returns the number of elements in the list.
  • Moreover, you never modify the variable img, so it's still a pointer on the BufferedImage you add to the Vector.
  • If you don't do parallelized (multi-threaded) operations, use an ArrayList instead of a Vector. A Vector is an ArrayList with synchronized operations, which is slower.

Same thing with the line g.drawImage(imagePixelLabelBuffered.get(labelClassesCount-1),x-20, y-31,100,100, null), you can use the variable img.

[EDIT] Here is a link for BufferedImage cloning.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • In fact I'm just trying to do a copy without reference to the original so that's why I created a Vector. I want to display all the copies of image and manipulate them differently, but every time I change the pixels color for the last one, it changes with the same color for the other images too. How is it possible to manipulate all the changes but for each copy of image, not for all of them in the same time ? Have to say I'm a beginner. – kazrak Jun 27 '17 at 09:35
  • When you add your image to the Vector, you add a pointer of the image into the Vector. Everything is pointer in Java. I've edited my answer with a link to a cloning method. But take a look to what I've written above, that's also an error in your program. – FiReTiTi Jun 27 '17 at 17:18