This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ImageIcon trouble

 
Josh Ziegler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing a quicky project for my high-school Programming class and so far i'm having trouble loading a image and returning it as a ImageIcon I need the image to be a Icon so I can use it on a button. Any help is appreciated. BTW this code was taken off this website to speed up development so....
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read me..
It should be very easy to replace the JLabel in the example given with a JButton using a bit of thought. Notice that java.net.URL&041;" target="_blank" rel="nofollow">ImageIO.read(url) returns an Image not an ImageIcon - using this image an ImageIcon can be instantiated and used to construct a JButton.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method has a return type of ImageIcon so java is expecting an ImageIcon in the return statement at the bottom of the method. But you are returning an image. The compiler will complain about this. The return type declared in the method signature must match the type returned by the method.

Your class will also need a main method. ImageIO is new in JRE1.4.
[ March 30, 2004: Message edited by: Craig Wood ]
 
Enrique Fernandez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are planning to load your image from a jpg or gif source here's a little example using a JDialog to display a JButton with the image:
public class ImageLoad extends JDialog{
private Image imagen;
public ImageLoad (Frame padre, String titulo, boolean modal) throws Exception {
super(padre, titulo,modal);
}
public void dibujaLog() {
// CHANGE THE PATH TO YOUR IMAGE!!!
imagen = Toolkit.getDefaultToolkit().getImage("your.jpg");
ImageIcon aceptIcon = new ImageIcon(imagen);
this.setLocationRelativeTo(null);
this.setSize(300,300);
JButton jb = new JButton(aceptIcon);
jb.setBounds(5,5,275,342);
getContentPane().setLayout(null);
getContentPane().add(jb);
show();
}
public static void main(String args[]) throws Exception{
ImageLoad log = new ImageLoad(null, "Access", true);
log.dibujaLog();
}
}
 
Josh Ziegler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help first off.
Craig -> Why do I need a main when I'm using the class to support my main project? Is it really neccessary? I'm going to be calling this classes loadImage method (or whatever its called) lots and it may be different every time (at least the file will be).
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggestion: change the line:
URL url = ShowingAnImage.class.getResource(fileName);
to:
URL url = ShowingAnImage.class.getClassLoader().getSystemResource(fileName);

Then regardless of what package ShowingAnImage belongs to, the file will be found. (It will still work without a package being specified)
For instance

The directory structure is
./images/Owls.jpg
./test/ShowingAnImage.class
Then execute java test.ShowingAnImage and the image file is found as we expect.
[ March 31, 2004: Message edited by: Carlos Failde ]
 
Eddie Felson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was really interested in this thread as I was having problems myself getting Images to display. I have tried this code out and it really worked for me. Which is great as I am a novice coder, just into my second year at University.
However the example deals with just one image and I am struggling to make the image loader class to be a generic class that would deal with any image I choose to display in my project.
Is this easy to do?
Are there any default generic imageLoad functions I could use?
Many thanks in advance, your code was really neat.
Any help you can give at all would be very much appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic