0

I have the following code :

public class TryToMakeSomethingHappen extends JPanel  {

private JFrame f;
private JPanel p;
public TryToMakeSomethingHappen() {
    f=new JFrame("Title");
    f.setSize(600, 400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    p=new JPanel(new BorderLayout());

    ImageIcon ic=new ImageIcon("/home/michael/Pictures/begin1.jpg");

    int imageHeight = ic.getIconHeight();
    int imageWidth  = ic.getIconWidth();

    BufferedImage  bimg = new BufferedImage(imageWidth ,imageHeight, BufferedImage.TYPE_INT_ARGB);

    int pixels[][]=new int[imageWidth][imageHeight];
    for (int i=0;i<imageWidth;i++)
        for(int j=0; j<imageHeight;j++)
            pixels[i][j]=bimg.getRGB(i, j);
    BufferedImage bimg2=new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);

    for(int y=0;y<imageHeight;y++)
        for(int x=0;x<imageWidth;x++){
            bimg2.setRGB(x, y,pixels[x][y]);
        }
       File file= new File("/home/michael/Pictures/pic2.png");
        JLabel label=new JLabel("",ic,JLabel.CENTER);
        JLabel label1;
    try {
        ImageIO.write(bimg2,"PNG",file);
        ImageIcon ic2=new ImageIcon("/home/michael/Pictures/pic2.png");
        label1=new JLabel("",ic2,JLabel.CENTER);
        p.add(label1,BorderLayout.EAST);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    p.add(label,BorderLayout.WEST);

    f.setVisible(true);
    f.add(p);
}

public static void main(String[]args){  
    TryToMakeSomethingHappen t = new TryToMakeSomethingHappen();
}
}

f is JFrame, bimg2 is BufferedImage and ,p is JPanel. (privates of my class).

The WEST picture is showing while EAST picture is not. Why is that? When i check pic2 in the folder, it is empty. I use Linux (Ubuntu).

How do I make bimg2 to show on the EAST side of the panel?

MKB
  • 7,587
  • 9
  • 45
  • 71
michael kova
  • 105
  • 2
  • 8
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get images for an example, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Dec 31 '14 at 10:31
  • as i said, the west picture is showing and the east is not. I want to fix the error. – michael kova Dec 31 '14 at 10:33
  • Andrew, I did not find that link helpful. – michael kova Dec 31 '14 at 10:35
  • Sorry for not using proper cases, fixed that. – michael kova Dec 31 '14 at 10:36
  • *"Andrew, I did not find that link helpful."* In that case, you are entitled to a full refund. BTW - which of the 3 links are you referring to? – Andrew Thompson Dec 31 '14 at 10:37
  • How do I make bimg2 to show on the EAST side of the panel ? – michael kova Dec 31 '14 at 10:37
  • I refered to the Q&A one, I am sorry I did not meet the format, you can read the "try - catch" I think the error is there. – michael kova Dec 31 '14 at 10:39
  • *"you can read the.."* I 'read' code after it is compiled and formatted in my IDE. But code snippets won't compile, so I recommended you post something not only what will compile but (with hot-linked images) will also run to display the problem to anybody who tries the code.. – Andrew Thompson Dec 31 '14 at 10:42
  • I will send you the whole code, but the thing is I have in my computer the picture "begin1" and you do not. I'll edit the post to have the whole code now.When compiling notice only the picture on the left is showing, and the right is not. – michael kova Dec 31 '14 at 10:45
  • *"I will send you the whole code,"* Don't send anything to me. And nobody has been talking about the 'whole code'. *"..but the thing is I have in my computer the picture "begin1" and you do not.."* WTF do you think I mentioned 'hot-link' **twice** already! Do you even understand what it means? – Andrew Thompson Dec 31 '14 at 10:48
  • I wrote the whole code now, without the imports(just takes a lot of place). – michael kova Dec 31 '14 at 10:48
  • No, what is hot-link? When I said Ill send you I meant I edited it. The principle is the same for any given picture, not for this particular one. Sorry for the mistakes, English is not my native.. – michael kova Dec 31 '14 at 10:49
  • *"No, what is hot-link?"* I'm going to pause here for a moment, to point out that if there is something you do not understand, *ask*, but always search first, and be specific about what you do not understand. It should not take me mentioning it 3 times, to find out that you do not understand. *Is that clear?* – Andrew Thompson Dec 31 '14 at 10:54
  • Okay I found that out, it does not matter for the specific picture. You can replace the address I wrote with any other given address that refers to a picture. – michael kova Dec 31 '14 at 11:07
  • *"You can replace the address I wrote with any other given address that refers to a picture."* If you couldn't be bothered doing it, why should I or anyone else? You really don't seem to have the grasp of 'making it as easy as possible for other people to help'. – Andrew Thompson Dec 31 '14 at 11:33

1 Answers1

1

What is the point of the posted code?

When i check pic2 in the folder, it is empty.

Well take a look at the code step by step:

BufferedImage  bimg = new BufferedImage(imageWidth ,imageHeight, BufferedImage.TYPE_INT_ARGB);

First you create an blank BufferedImage (in which case I believe the pixels will all be black).

pixels[i][j]=bimg.getRGB(i, j);

Then you copy the unchanged pixels to an Array.

BufferedImage bimg2=new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);

Then you create another blank image.

 bimg2.setRGB(x, y,pixels[x][y]);

Then you set the pixels of the BufferedImage to the unchanged (ie. black) pixels from the Array

ImageIO.write(bimg2,"PNG",file);

Then you write the BufferedImage to a file.

The BufferedImage still contains the default pixels from when you created the BufferedImage. What do you expect to see?

camickr
  • 321,443
  • 19
  • 166
  • 288