Hey, All!
I'm writing a rather complex GUI program using Java 6 and NetBeans IDE
5.5. My project is coming along sharply, but I'm getting hung up on the
simplest of concepts.
I've read the tutorials, looked at the source code to them, searched the
net, read tons of source code and can't seem to make it work in my
project. What I'm talking about is simply displaying a picture on an
element of my forms.
For example, displaying a graphic on my About screen that is related to
the program. Or, showing screenshots that are previews of the various
themes for the Metal look and feel that I have already made.
I've got these images stored under my project source tree in a folder
called resources, but as far as I can tell, the only way these images get
compiled into the resources folder in the JAR are if they are actually
loaded at design time in the IDE.
I don't mind if I have to add these images to the JAR after I've compiled,
if I'm able to load them at runtime. I've tried loading them using a
path-like string, but that doesn't work. I've tried loading them using
getResource() and that doesn't seem to work as well.
I've been pouring over the JavaDocs for the APIs and I'm just getting more
and more confused. I've seen constructors that require an Icon object,
but when I look up that Icon object, it seems to want an Image object for
its constructor. And when I looked up Image, it didn't want a string
either. I've tried building the path using an URL object and can't seem
to get any of this to work.
If someone could please post a code snippet that shows how to load an
image file from the resources folder of the JAR file into a LayeredPane
object (or another object that may work better), I would greatly appreciate
it. It's driving me crazy that the simplest thing (nice-to-have, but *not*
needed) is bogging me down.
Thank you all for your time in reading this very long-winded message. I
hope to hear from some of you in the near future. Have a great day.
Cheers,
Sean
|
|
0
|
|
|
|
Reply
|
Sean
|
2/19/2007 1:13:03 AM |
|
On Feb 19, 12:13 pm, "Sean C." <BrknSh...@gmail.com> wrote:
....
(images)
> ...I've tried loading them using a
> path-like string, but that doesn't work. I've tried loading them using
> getResource() and that doesn't seem to work as well.
I see there being two basic parts to this
problem.
1) getting the path to the images
2) loading and displaying them
If part 1) does not work, part 2)
is moot.
So, I suggest you write up some test code
that focuses on getting a valid URL to
the image (or one of them).
If you cannot get that working, post
your short code (that focuses only on
finding the image) to the group and we
might be able to assist further.
For part 2), there are a number of ways
you might go about loading and displaying
the image, but one way is this..
JLabel imageLabel = new JLabel(
new ImageIcon( theImageURL ) );
parent.add( imageLabel );
Andrew T.
|
|
0
|
|
|
|
Reply
|
Andrew
|
2/19/2007 2:01:16 AM
|
|
Sean C. wrote:
> Hey, All!
>
> I'm writing a rather complex GUI program using Java 6 and NetBeans IDE
> 5.5. My project is coming along sharply, but I'm getting hung up on the
> simplest of concepts.
>
> I've read the tutorials, looked at the source code to them, searched the
> net, read tons of source code and can't seem to make it work in my
> project. What I'm talking about is simply displaying a picture on an
> element of my forms.
>
> For example, displaying a graphic on my About screen that is related to
> the program. Or, showing screenshots that are previews of the various
> themes for the Metal look and feel that I have already made.
>
> I've got these images stored under my project source tree in a folder
> called resources, but as far as I can tell, the only way these images get
> compiled into the resources folder in the JAR are if they are actually
> loaded at design time in the IDE.
>
> I don't mind if I have to add these images to the JAR after I've compiled,
> if I'm able to load them at runtime. I've tried loading them using a
> path-like string, but that doesn't work. I've tried loading them using
> getResource() and that doesn't seem to work as well.
>
> I've been pouring over the JavaDocs for the APIs and I'm just getting more
> and more confused. I've seen constructors that require an Icon object,
> but when I look up that Icon object, it seems to want an Image object for
> its constructor. And when I looked up Image, it didn't want a string
> either. I've tried building the path using an URL object and can't seem
> to get any of this to work.
>
> If someone could please post a code snippet that shows how to load an
> image file from the resources folder of the JAR file into a LayeredPane
> object (or another object that may work better), I would greatly appreciate
> it. It's driving me crazy that the simplest thing (nice-to-have, but *not*
> needed) is bogging me down.
>
> Thank you all for your time in reading this very long-winded message. I
> hope to hear from some of you in the near future. Have a great day.
>
> Cheers,
>
> Sean
Sorry I don't use NetBeans so I can't help with that.
Use the program below but change the package to suit. Put the image
file (kittens.jpg in my case) in a directory called images. You can
either add the image directory and files to the jar or not, the loader
will get them no matter. Remember that paths are relative to the class
being used to load the resource unless you specify / as the first
character of the resource name. I like to put my images on JPanels but
that's just me. If you compile this and jar it up it should work just
fine. If you run it in the directory with the images directory, be
cautious that you know which image is being loaded! Change the name of
the images directory when you test just to make sure.
package com.knutejohnson.test;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class test7 extends JPanel {
BufferedImage image;
public test7(String fname) {
URL url = test7.class.getResource(fname);
try {
image = ImageIO.read(url);
setPreferredSize(new Dimension(
image.getWidth(),image.getHeight()));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void paintComponent(Graphics g) {
g.drawImage(image,0,0,null);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test7 t7 = new test7("images/kittens.jpg");
f.add(t7,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
--
Knute Johnson
email s/nospam/knute/
|
|
0
|
|
|
|
Reply
|
Knute
|
2/19/2007 2:34:16 AM
|
|
Sean,
This is NOT an easy problem. Especially, if you use WebStart or a
different thread/classloader needs to load the resource.
I wrote a Java Application framework about 7 years ago. One of the
first utility classes I wrote was something called IconLoader. Feel
free to use as needed.
The entire project can be found at:
https://tframe.dev.java.net/
Kind regards,
Ed Tidwell
package net.java.dev.tframe.util;
import java.awt.Frame;
import java.net.URL;
import javax.swing.*;
/**
* This class will load an icon and add it to the control passed in.
We will
* add more methods as they are needed to support future controls not
currently
* implemented. If an icon is not found an error message is printed to
system
* out to inform the caller that the icon was not located.
*
* Currently we have an "icons" directory in our jar file for all icons.
* This directory should exist in your classes directory if your using
* JBuilder to create your jar file. <recommended solution>
*
* Made 2.0 so you do not have to create an instance of IconLoader.
Just call
* the static method. This is a LOT more efficient.
*
* Made 3.0 to work with WebStart
*
* @Author Ed Tidwell
* @version 3.0
*/
public final class IconLoader {
// this directory MUST exist in the jar AND
// must contain all your icons
//JBuilder note... Create this directory in your classes directory and
//copy your icons to that location.
public static final String ICON_PATH = "resources/icons/";
/** All methods in this class are static - forces access through
class only */
private IconLoader() {}
/**
* This helper method insures we redirect to the class loader that
loads us.
* @param iconName path to resources
* @return url to resource
*/
private static URL getURL(String iconName) {
// have to get the class to get to the classLoader that LOADED us.
// this is so we work correctly under WebStart
URL iconURL = Thread.currentThread().getContextClassLoader().
getResource(ICON_PATH + iconName);
return iconURL;
}
/**
* Give the name of an icon to look for and a JButton target for
the icon
* IF it is found.
* @param iconName file name
* @param setMe button to set
*/
public static void setIcon(String iconName, JButton setMe) {
URL iconURL = getURL(iconName);
if (iconURL != null) {
setMe.setIcon(new ImageIcon(iconURL));
} else {
System.out.println(iconName + " not found in " + ICON_PATH);
} // if else
} // setIcon for JButton
/**
* Give the name of an icon to look for and a JLabel target for the
icon
* IF it is found.
* @param iconName file name
* @param setMe button to set
*/
public static void setIcon(String iconName, JLabel setMe) {
URL iconURL = getURL(iconName);
if (iconURL != null) {
setMe.setIcon(new ImageIcon(iconURL));
} else {
System.out.println(iconName + " not found in " + ICON_PATH);
} // if else
} // setIcon for JButton
/**
* Give the name of an icon to look for and a Frame target for the icon
* @param iconName of file
* @param frame to set icon image against
*/
public static void setIcon(String iconName, Frame frame) {
URL iconURL = getURL(iconName);
if (iconURL != null) {
// set upper left hand corner icon for OS platforms that
support it
// child applications can easily override the default
frame.setIconImage(new ImageIcon(iconURL).getImage());
} else {
System.out.println(iconName + " not found in " + ICON_PATH);
} // if else
} // setIcon for JButton
/**
* Given an icon name return the icon.
* @param iconName file name of icon
* @return the icon if it exists. Otherwise, null.
*/
public static ImageIcon getIcon(String iconName) {
URL iconURL = getURL(iconName);
if (iconURL == null) {
System.out.println(iconName + " not found in " + ICON_PATH);
}
return (iconURL == null ? null : new ImageIcon(iconURL));
} // getIcon
} //IconLoader
|
|
0
|
|
|
|
Reply
|
Ed
|
2/24/2007 5:13:24 AM
|
|
On Feb 18, 7:13 pm, "Sean C." <BrknSh...@gmail.com> wrote:
> Hey, All!
>
> I'm writing a rather complex GUI program using Java 6 and NetBeans IDE
> 5.5. My project is coming along sharply, but I'm getting hung up on the
> simplest of concepts.
>
> I've read the tutorials, looked at the source code to them, searched the
> net, read tons of source code and can't seem to make it work in my
> project. What I'm talking about is simply displaying a picture on an
> element of my forms.
>
> For example, displaying a graphic on my About screen that is related to
> the program. Or, showing screenshots that are previews of the various
> themes for the Metal look and feel that I have already made.
>
> I've got these images stored under my project source tree in a folder
> called resources, but as far as I can tell, the only way these images get
> compiled into the resources folder in the JAR are if they are actually
> loaded at design time in the IDE.
>
> I don't mind if I have to add these images to the JAR after I've compiled,
> if I'm able to load them at runtime. I've tried loading them using a
> path-like string, but that doesn't work. I've tried loading them using
> getResource() and that doesn't seem to work as well.
>
> I've been pouring over the JavaDocs for the APIs and I'm just getting more
> and more confused. I've seen constructors that require an Icon object,
> but when I look up that Icon object, it seems to want anImageobject for
> its constructor. And when I looked upImage, it didn't want a string
> either. I've tried building the path using an URL object and can't seem
> to get any of this to work.
>
> If someone could please post a code snippet that shows how to load animagefile from the resources folder of the JAR file into a LayeredPane
> object (or another object that may work better), I would greatly appreciate
> it. It's driving me crazy that the simplest thing (nice-to-have, but *not*
> needed) is bogging me down.
>
> Thank you all for your time in reading this very long-winded message. I
> hope to hear from some of you in the near future. Have a great day.
>
> Cheers,
>
> Sean
Hope this
helps////////////////////////////////////////////////////////////////////
protected final ImageIcon completeIcon = createImageIcon("Images/
Complete16.gif");
///////////////////////////////////////////////////////////////////////////////////
private static ImageIcon createImageIcon(String path)
{
//imgURL is the loading path of where in the system the picture
is located
java.net.URL imgURL = RRStartUp.class.getResource(path);
if (imgURL != null)
{
//sets the image icon in frame top left
return new ImageIcon(imgURL);
}
else
{
System.err.println("Couldn't find file: " +
RRStartUp.class.getResource(path));
return null;
}
}
private static Image getFDImage()
{
//imgURL is the loading path of where in the system the
picture is located
//RRStartUp is your main class
java.net.URL imgURL = RRStartUp.class.getResource("Images/
medImage.jpg");
if (imgURL != null)
{
//sets the picture in content pane
return new ImageIcon(imgURL).getImage();
}
else
{
return null;
}
}
//////////////////////////////////////////////////////////////////////////////////
you can do this :
//////////////////////////////////////////////////////////////////////////////////
JButton complete = new JButton(" Completion ", completeIcon);
/////////////////////////////////////////////////////////////////////////////////
Or:
/////////////////////////////////////////////////////////////////////////////////
ImageIcon secImage = createImageIcon("Images/ChangePassword.jpg");
JLabel picture2 = new JLabel();
if (secImage != null)
{
//////// create a object label to hold first image
picture2 = new JLabel(secImage);
//sets picture in pictureLabel
picture2.setIcon(secImage);
}
/////////////////////////////////////////////////////////////////////////////////
then set it to a panel:
/////////////////////////////////////////////////////////////////////////////////
panel.add(picture2, BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////////////
|
|
0
|
|
|
|
Reply
|
cjavatalk
|
2/24/2007 11:54:49 PM
|
|
|
4 Replies
289 Views
(page loaded in 0.12 seconds)
Similiar Articles: Displaying Images - comp.lang.java.guiHey, All! I'm writing a rather complex GUI program using Java 6 and NetBeans IDE 5.5. My project is coming along sharply, but I'm getting hung up on... Displaying multiple dicom images in gui - comp.soft-sys.matlab ...Hi there, I am making a matlab gui with dicom images. I am stuck on the problem where I need to display multiple dicom images in a single Axes i.e.... Displaying image in GUI via push button - comp.soft-sys.matlab ...Greetings, I'm actually trying to display two images in two different axes in the GUI. Two push button were created to display image1 and image2 to... Read and display images in bsq format - comp.soft-sys.matlab ...hello, i'm having real problem in reading bsq files and display the images inside i know that we can read a bsq file by the function multibandread, ... Displaying 10-bit image data using OpenGL - comp.graphics.api ...I am trying to take the data in a framegrabber buffer and display it in a dialog ... The camera taking the images, streams the data at 10-bits per pixel to the buffer. using gui slider to display 3d image stack - comp.soft-sys.matlab ...I am new to GUI programming. I am trying to do the following: 1. Display an 3-D MRI image (represented as a stack of 2D images). 2. Use a slider to... how to display an HSV image? - comp.soft-sys.matlab... comp.soft-sys.matlab 3D image display - comp.soft-sys.matlab how to display an HSV image? - comp.soft-sys.matlab using gui slider to display 3d image ... Displaying Images ... GUI Slider and dicom image - comp.soft-sys.matlabGUI Slider and dicom image - comp.soft-sys.matlab Trouble reading DICOM files - comp.soft-sys.matlab Displaying multiple dicom images in gui - comp.soft-sys.matlab ... out of memory problems on 3d dicom image - comp.soft-sys.matlab ...using gui slider to display 3d image stack - comp.soft-sys.matlab ... I am having the ... MATLAB - comp.soft-sys.matlab ..... of the image file and then decide which images ... display 2d cross-sections in 3d box - comp.lang.idl-pvwave ...Hi, I have 2d data composed of resistivity profiles of underground. Data simply composed of x,z and Resistivity vectors. I have no problem in displaying these images ... Problems Displaying Images in Internet Explorer - Microsoft SupportWhen you are using Internet Explorer, you may experience one or more of the following symptoms: Images may not be displayed, or may be displayed improperly ... Displaying images - Multimedia - Development Guide - BlackBerry ...Displaying an image for zooming and panning; Display an image for zooming and panning; Code sample: Displaying an image for zooming and panning; Displaying a row of ... 7/25/2012 3:12:16 PM
|