imageIcons... JPanels.... JLabels... help?

Hello all.

I am a noobie at this stuff (just to let y'all kno) and I'm having a hard time drawing/painting an ImageIcon on a JPanel. I'm not really sure, but I think it has something to do with the fact that the Graphics class is abstract (so obviously I can't make an instance of it) and therefore I can't pass a graphics object in a parameter without getting a NullPointerException or compiler error saying "g" has not been initialised.

here's part of the code:

ImageIcon image = new ImageIcon("C:\\marble.JPEG", "a marble");

JLabel background = new JLabel(image);

container.add(background, BorderLayout.CENTER);

Any help would be greatly appreciated. :-)

[706 byte] By [lunelotea] at [2007-10-2 21:15:44]
# 1

don't know what you're trying to do with the Graphics object

this is all you need

import java.awt.*;

import javax.swing.*;

class Testing extends JFrame

{

public Testing()

{

setLocation(400,300);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel p = new JPanel();

JLabel lbl = new JLabel(new ImageIcon("C:\\Test.jpg","a marble"));

p.add(lbl);

getContentPane().add(p);

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-14 0:23:38 > top of Java-index,Desktop,Core GUI APIs...
# 2
yay! it works... but: question? what is this doing?: getContentPane().add(p);pack();
lunelotea at 2007-7-14 0:23:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

> getContentPane().add(p);

if you are using java 1.5+, all you need is

add(p);

but for java versions < 1.5, the getContentPane() is required:

getContentPane().setLayout(...);

getContentPane().add(...);

> pack();

[url]http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Window.html#pack()[/url]

Michael_Dunna at 2007-7-14 0:23:38 > top of Java-index,Desktop,Core GUI APIs...
# 4

Not to be too terribly annoying, but if I wanted it to print the picture after a button click (so in the actionPerformed() method... check that) how would that work?.. I tried putting the code in it's own little method and then calling that up when the button is clicked, but there's no picture anymore.... :-(

lunelotea at 2007-7-14 0:23:38 > top of Java-index,Desktop,Core GUI APIs...
# 5
I don't do anything with printing, but I just used tom jacobs' StandardPrint class http://forum.java.sun.com/thread.jspa?forumID=57&threadID=711572and it worked fine - image printed out OK(had to modify the print code to remove the references to SpecialPrint
Michael_Dunna at 2007-7-14 0:23:38 > top of Java-index,Desktop,Core GUI APIs...
# 6

mmm.. k that went way over my head.... maybe *print* isn't the right word.... like *draw* perhaps? basically i want to get the picture to show up when a certain button is clicked (ok... maybe further explanation would help. i'm making a mancala programme and when the "play" button is clicked i would like the board picture to show up...)

thank you so much tho... actually getting the picture to show up after being very frustrated for a week was a nice change... even if it's not perfectly working;-)

lunelotea at 2007-7-14 0:23:38 > top of Java-index,Desktop,Core GUI APIs...
# 7

the method you need is

setIcon()

here's the earlier code, modified for the image to show after clicking the button

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Testing extends JFrame

{

public Testing()

{

setSize(600,400);//needs a size, otherwise frame will be very small (label is empty)

setLocation(300,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel p = new JPanel();

final JLabel lbl = new JLabel();

p.add(lbl);

getContentPane().add(p,BorderLayout.CENTER);

JButton btn = new JButton("Show Image");

getContentPane().add(btn,BorderLayout.SOUTH);

btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

lbl.setIcon(new ImageIcon("C:\\Test.jpg","a marble"));

pack();//will change the size of the frame, comment out and experiment

}});

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-14 0:23:39 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thank you!!! It makes sense now! :-)
lunelotea at 2007-7-14 0:23:39 > top of Java-index,Desktop,Core GUI APIs...