1

I am trying to display a string and a BufferedImage onto a JFrame that both come as output from a method. I am not able to separate the String from the image, therefore I need to add both to the JFrame.

Here is the code I have do far and nothing is displaying. Thank you very much for your help in advance.

String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
    image = ImageIO.read(new File(path));
} catch (IOException ex) {
    Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
f.setSize(400,400);
f.setVisible(true);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    I don't think I understand. Does the image contain the `String` or do you need to display it with the image? You've not added anything to your frame, but instead showed an option pane – MadProgrammer Mar 13 '13 at 03:15
  • 2
    A method cannot "return a String and a BufferedImage". Methods can return one and only one thing. What exactly is not working? What errors are you seeing? Please clarify your question. Greatly. – Hovercraft Full Of Eels Mar 13 '13 at 03:15
  • MadProgrammer - I need to display the string with the image on a JFrame – Sandra Leon Mar 13 '13 at 03:32
  • Here is an example of what the getUserInfo() method returns: Name: Kim Kardashian BufferedImage@9b878c: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@b97081 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 150 height = 150 #numDataElements 3 dataOff[0] = 2 Bio: Twitter.com/KimKardashian Website: http://kimkardashian.com – Sandra Leon Mar 13 '13 at 03:35
  • The code that I have right now only creates and shows a JFrame with nothing in it. – Sandra Leon Mar 13 '13 at 03:36
  • That is just a `toString()` result, you can't obtain an object from this string. Instead, the method should return the actual `BufferedImage`. – Mordechai Mar 13 '13 at 03:37

3 Answers3

3

You can render the string over the image using drawString(), as shown here.

Alternatively, you can use label.setText() and rely on the label's horizontal & vertical alignment for positioning, as illustrated here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

Basically, you can use the label's setText method to supply a text value for the label.

You also need to "add" the label to the frame, or it won't display anything.

String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
    image = ImageIO.read(new File(path));
} catch (IOException ex) {
    Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel("This is some text", icon, JLabel.CENTER);
f.add(label);
f.setVisible(true);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Hovercraft Full Of Eels actually answered my question. I wanted to return both a String and BufferedImage but now I know that is not possible. Thank you all for your help :)