Results 1 to 6 of 6
Thread: Using Images in Swing
- 07-30-2009, 08:26 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
Using Images in Swing
I want to make a program that will list a group of words, then take certain letters from those words to make a new word (kind of like the end of Harry Potter 2 :)). I plan to have image files with parts of the words that do not move, then separate image files with the moving letters. To display and move images in swing, do I use the ImageIcon class? I tried to make a new ImageIcon but I got an error saying illegal escape character.
If I need to use this class...what is wrong with that constructor? If not, what do I need to use to display and moves images with swing?Java Code:package javaapplication3; import javax.swing.*; /** * * @author Sterling */ public class ImageIconTest { ImageIcon image = new ImageIcon("C:\Users\Sterling\Documents\NetBeansProjects\GGWEBINTRO\src\ggwebintro\Images\Mmoving.jpeg"); }
-
The problem is not with the constructor, it's with your creating a path String that uses single backslashes which as the escape character have special meaning in Java. Either use forward slashes instead or use double backslashes.
- 07-31-2009, 04:27 AM #3
Since you are on windows, you can use \\ if you want to make absolute paths. The \ in java is an escape character that signifies a special character. For example:
\t = tab
\n = newline on *nix systems
\r = newline on windoze systems
\\ = a \ character
- 07-31-2009, 10:05 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
Okay thanks for the info. However, I now have this code and whenever I run the program it displays the label text but it does not show the ImageIcon. Just has the text and a gray screen.
Why is it not showing the ImageIcon?Java Code:package javaapplication3; import javax.swing.*; /** * * @author Sterling */ public class ImageIconTest { ImageIcon image; JFrame frame = new JFrame(); JPanel panel = new JPanel(); JLabel label; public static void main(String args[]) { ImageIconTest test = new ImageIconTest(); test.go(); } public void go() { image = new ImageIcon("C:\\Users\\Sterling\\Documents\\NetBeansProjects\\GGWEBINTRO\\src\\ggwebintro\\Images\\Mmoving.jpeg"); label = new JLabel("C:\\Users\\Sterling\\Documents\\NetBeansProjects\\GGWEBINTRO\\src\\ggwebintro\\Images\\Mmoving.jpeg", image, JLabel.CENTER); label.setIcon(image); panel.add(label); frame.getContentPane().add(panel); frame.setSize(300, 300); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setVisible(true); } }
-
Are you looking in the right place for the image? It works well for me. Here's an example using an internet image (from Wikipedia):
Java Code:import java.awt.Color; import java.net.MalformedURLException; import java.net.URL; import javax.swing.*; public class ImageIconTest { private static final String IMAGE_STRING = "http://upload.wikimedia.org/wikipedia/" + "commons/thumb/2/28/The_Nightwatch_by_Rembrandt.jpg/" + "720px-The_Nightwatch_by_Rembrandt.jpg"; ImageIcon image; JFrame frame = new JFrame(); JPanel panel = new JPanel(); JLabel label; public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { ImageIconTest test = new ImageIconTest(); test.go(); } }); } public void go() { try { image = new ImageIcon(new URL(IMAGE_STRING)); } catch (MalformedURLException e) { e.printStackTrace(); } label = new JLabel("Night Watch", image, JLabel.CENTER); label.setIcon(image); label.setVerticalTextPosition(SwingConstants.BOTTOM); label.setHorizontalTextPosition(SwingConstants.CENTER); label.setForeground(Color.lightGray); panel.setBackground(Color.black); panel.add(label); frame.getContentPane().add(panel); //frame.setSize(300, 300); frame.pack(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
- 08-01-2009, 12:22 AM #6
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
Well the image is just of an "M" so it is relatively small. But when I run the program the frame pops up and has the text centered at the top and the rest is just gray. Does it matter that it is a jpeg format? Does it HAVE to be gif or jpg? Also, there is another class in the folder (basically just a folder for me to test random classes or other things), but it is entirely separate from this program and this should be able to run all on it's own. But could either of these things have something to do with it? I added a line calling the pack() method and when the frame pops up it is only the size of the text (kind of it's just a text field).
Similar Threads
-
need help in uploading images in swing program!!!
By ashton in forum New To JavaReplies: 10Last Post: 01-26-2009, 10:01 AM -
Default Swing Images
By Bubba_OH in forum New To JavaReplies: 5Last Post: 10-10-2008, 03:00 PM -
images
By amith in forum AWT / SwingReplies: 3Last Post: 06-27-2008, 08:38 PM -
map javax.swing.text.Element to javax.swing.text.View
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 07:02 PM -
Images in JSP
By Daniel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-05-2007, 06:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks