2 Replies Latest reply: Aug 8, 2011 6:01 PM by 875749 RSS

    ImageIcon / Image Conflict

    875749
      Hello, all! I'm assembling together a simple image loading program from a series of online tutorials from thenewboston about simple Jave game programming. A program in particular is supposed to load two images together using Images and ImageIcons, but the compiler is saying there is a conflict between ImageIcons and Images, despite the fact that the program is copied exactly from the videos and the program appears to run just fine for the person instructing the tutorial. Here's the code and the link to the video for this program:
      import java.awt.*;
      import javax.swing.JFrame;
      
      public class Screen
      {
           private GraphicsDevice vc;
           
           public Screen()
           {
                GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
                vc = env.getDefaultScreenDevice();
           }
           
           public void setFullScreen(DisplayMode dm, JFrame window)
           {
                window.setUndecorated(true);
                window.setResizable(false);
                vc.setFullScreenWindow(window);
                
                if (dm != null && vc.isDisplayChangeSupported())
                {
                     try
                     {
                          vc.setDisplayMode(dm);
                     } catch (Exception ex) { }
                }
           }
           
           public Window getFullScreenWindow()
           {
                return vc.getFullScreenWindow();
           }
           
           public void restoreScreen()
           {
                Window w = vc.getFullScreenWindow();
                if (w != null)
                {
                     w.dispose();
                }
                
                vc.setFullScreenWindow(null);
           }
      }
      import java.awt.*;
      import javax.swing.ImageIcon;
      import javax.swing.JFrame;
      
      public class Images extends JFrame
      {
           public static void main(String args[])
           {
                DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
                Images i = new Images();
                i.run(dm);
           }
           
           private Screen s;
           private Image bg;
           private Image pic;
           private boolean loaded;
           
           public void run(DisplayMode dm)
           {
                setBackground(Color.PINK);
                setForeground(Color.WHITE);
                setFont(new Font("Arial", Font.PLAIN, 24));
                loaded = false;
                
                s = new Screen();
                
                try
                {
                     s.setFullScreen(dm, this);
                     loadpics();
                     try
                     {
                          Thread.sleep(5000);
                     }
                     catch (Exception ex) { }
                }
                finally
                {
                     s.restoreScreen();
                }
           }
           
           public void loadpics()
           {
                bg = new ImageIcon("C:\\Users\\Ned\\Desktop\\BuckyGame\\back.jpeg");
                pic = new ImageIcon("C:\\Users\\Ned\\Desktop\\BuckyGame\\face.png");
                loaded = true;
                repaint();
           }
           
           public void paint(Graphics g)
           {
                if (g instanceof Graphics2D)
                {
                     Graphics2D g2 = (Graphics2D) g;
                     g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
                          RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                }
                
                if (loaded)
                {
                     g.drawImage(bg, 0, 0, null);
                     g.drawImage(pic, 170, 180, null);
                }
           }
      }
      Link:
      http://www.youtube.com/watch?v=5jb3rOeIiOQ

      It makes sense to me why there would be a conflict, but what I am confused about is why it doesn't seem to cause conflicts for him but it causes them for me. Help would be much appreciated. :] Thanks!

      Colton
        • 1. Re: ImageIcon / Image Conflict
          801313
          A.
                         g.drawImage(bg, 0, 0, null);
                         g.drawImage(pic, 170, 180, null);
          you would need to call ImageIcon.getImage() to convert it to an image which can then be drawn by the Graphics object

          But it seems like you are not using ImageIcon the right way. ImageIcon is intended for use in JLabel or in renderers for things like JTree or JList. You really ought to check out ImageIO (http://download.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html) to load your images as Image objects
          • 2. Re: ImageIcon / Image Conflict
            875749
            Thanks tjacobs, ImageIO worked perfectly! :] I appreciate your time and help.

            Colton