Results 1 to 6 of 6
  1. #1
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default 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.
    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");
     
    }
    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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

  4. #4
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    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.
    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);
        }
    
    }
    Why is it not showing the ImageIcon?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);
      }
    
    }

  6. #6
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    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

  1. need help in uploading images in swing program!!!
    By ashton in forum New To Java
    Replies: 10
    Last Post: 01-26-2009, 11:01 AM
  2. Default Swing Images
    By Bubba_OH in forum New To Java
    Replies: 5
    Last Post: 10-10-2008, 04:00 PM
  3. images
    By amith in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2008, 09:38 PM
  4. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 08:02 PM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 07:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •