Image problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackraven1425
    New Member
    • Mar 2008
    • 27

    #1

    Image problem

    I'm trying to draw an image to a panel, but am having problems with it not showing up. I know I'm creating/loading the image properly, but I have the feeling I'm not painting it right.

    public class ImageGet extends JPanel{
    public String file = null;
    public ImageGet(String fileName){
    super();
    file = fileName;
    setPreferredSiz e(new Dimension(300, 300));
    }
    public void paintComponent( Graphics comp){
    Graphics2D comp2D = (Graphics2D)com p;
    Image image = getToolkit().cr eateImage(file) ;
    while(comp2D.dr awImage(image, 0, 0, null)){
    System.out.prin tln("Displaying now.");
    }
    }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    The simplest way to include an image in your Swing GUI is to use JLabel+ImageIco n:

    [CODE=Java]import java.awt.*;
    import java.net.*;
    import javax.swing.*;

    public class ImageExample {
    public static void main(String[] args) {
    EventQueue.invo keLater(new Runnable(){
    public void run() {
    URL url = null;
    try {
    url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");
    } catch (MalformedURLEx ception e) {
    throw new RuntimeExceptio n(e);
    }
    JFrame f = new JFrame("ImageEx ample");
    f.getContentPan e().add(new JLabel(new ImageIcon(url)) );
    f.setDefaultClo seOperation(JFr ame.EXIT_ON_CLO SE);
    f.pack();
    f.setLocationRe lativeTo(null);
    f.setVisible(tr ue);
    }
    });
    }
    }[/CODE]

    If you want to work directly with an image object, java.awt.Image subclass BufferedImage is useful, and class javax.imageio.I mageIO has static methods for reading and writing images:

    [CODE=Java]BufferedImage image = ImageIO.read(ur l_or_file);[/CODE]

    Comment

    • blackraven1425
      New Member
      • Mar 2008
      • 27

      #3
      Ok, I tried that, but now I have another problem: the images don't display. Any ideas? What might I be doing wrong here?

      public JPanel getImagePanel(S tring imageFile){
      JPanel panel2 = new JPanel();
      ImageIcon icon = new ImageIcon(image File);
      panel2.add(new JLabel(icon));
      return panel2;
      }

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by blackraven1425
        Ok, I tried that, but now I have another problem: the images don't display. Any ideas? What might I be doing wrong here?

        public JPanel getImagePanel(S tring imageFile){
        JPanel panel2 = new JPanel();
        ImageIcon icon = new ImageIcon(image File);
        panel2.add(new JLabel(icon));
        return panel2;
        }
        The path imageFile may be incorrect. You can use ImageIcon's method getImageLoadSta tus() to see if the image was successfully loaded.

        Comment

        • blackraven1425
          New Member
          • Mar 2008
          • 27

          #5
          Ok, I went and loked up the getImageLoadSta tus constants and had them print out to the command line. One didnt load, the others (2 for now) did, but those two aren't showing up. Is there any other reason why this approach wouldn't be working?

          Comment

          Working...