0

I have such problem: I have prepared in JAVA program that is supposed to save some data from picture and save them into txt file. Then the program is supposed to change into black all pixels of the picture in each 25th row and actualize the picture on the display (already with black lines). But something is wrong and I do not have any clue what - the whole picture is removed from diplay and nothing is displayed. Here is the code:

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt)  

        ..........

      BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j=j+25) {
                    out.setRGB(i,j,0);  
                }
            }

            ImageIcon img = new ImageIcon(out);

            imagePanel.removeAll();
            imagePanel.setIcon(img);

        } catch (IOException e) { 
            System.out.print("ERROR");  
        }
    }
}                                          

public static BufferedImage loadImage(File file) {
    try {            
        BufferedImage out = ImageIO.read(file);
        return out;
    } catch (IOException e) {
        return null;
    }
}

I am using NetBeans and despite this everything works fine.

vonski
  • 7
  • 1
  • 6
  • Sorry. I have specified the problem now - see in the post. – vonski Feb 17 '16 at 14:19
  • 2
    You're creating a new empty BufferedImage `out` with the same size as `in` but you never copy the data in `in` to `out`. So you're drawing a black line on an empty image and then showing the result - which will then be empty. Make sure that you copy the image `in` first. – Erwin Bolwidt Feb 17 '16 at 14:28
  • What (type) is `imagePanel`? – Harald K Feb 17 '16 at 20:12

3 Answers3

2

you create an empty image and you start drawing in each 25th line , in fact you should start with original image and start drawing in it.

 // you create an empty image with same width and height of the original
    //BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());
      BufferedImage out = ImageIO.read(new File("path/to/Original/image")); 
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j=j+25) {
                out.setRGB(i,j,0);  
            }
        }
achabahe
  • 2,445
  • 1
  • 12
  • 21
  • I have made it like this but ImageIo is regarded as symbol - do not know why. What is more, the Buffered Image is not regarded as "function" (not highlighted). What am I missing? `BufferedImage out = ImageIo.read(new File(file.getAbsolutePath())); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j=j+25) { out.setRGB(i,j,0); } }` – vonski Feb 18 '16 at 07:45
  • it is ImageIO not ImageIo i have made a typo i will correct it this library is in the jdk the package name is javax.imageio this library is in jdk since 1.4 – achabahe Feb 18 '16 at 10:17
1

Just add imagePanel.revalidate(); after calling imagePanel.setIcon(img);. You have to tell Java that it has to evaluate your changes and repaint the components if neccessary.

Aron_dc
  • 883
  • 4
  • 14
  • Nothing is changing - the display is still gray although the program is going through this line. – vonski Feb 17 '16 at 14:35
-1

achabahe and Erwin Bolwidt were right - missing image. I have solved it in such really easy way:

 BufferdImage out = in;

I haven't flushed the memory so I could use it again. Maybe not the best solution but working.

Community
  • 1
  • 1
vonski
  • 7
  • 1
  • 6