Results 1 to 4 of 4
  1. #1
    apchar is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default image correctly loaded, but JPanel wont display

    The following is almost straight out of Horstmann but I did it in netbeans. But there's just not much to it. I know the image loads correctly because it prints the right width. But the frame comes up blank. What did I do wrong??

    Java Code:
    package my.stuff;
    
    import java.awt.*;
    import javax.imageio.*;
    import javax.swing.*;
    import java.net.*;
    
    public class jarredImages extends JFrame
    {
    	Image image = null;
    	
    	public jarredImages()
    	{
    		initComponents();
    		ImagePanel imagePanel = new ImagePanel();
    		add(imagePanel);
    	}
    
    	private class ImagePanel extends JPanel
    	{
    		private Image image;
    
    		public ImagePanel()
    		{
    			URL url = jarredImages.class.getResource("/s2000.jpg");
    			try { image = ImageIO.read(url); }
    			catch (Exception ee) { ee.printStackTrace(); }
    			System.out.println("width = " + image.getWidth(this));
    		}
    
    		public void paintComponent(Graphics g)
    		{
    			super.paintComponent(g);
    			if (image == null) return;
    
    			g.drawImage(image, 1, 1, this);
    		}
    	}
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
            // <editor-fold defaultstate="collapsed" desc="Generated Code">
            private void initComponents() {
    
                    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
                    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                    getContentPane().setLayout(layout);
                    layout.setHorizontalGroup(
                            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGap(0, 400, Short.MAX_VALUE)
                    );
                    layout.setVerticalGroup(
                            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGap(0, 300, Short.MAX_VALUE)
                    );
    
                    pack();
            }// </editor-fold>
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new jarredImages().setVisible(true);
                }
            });
        }
    
            // Variables declaration - do not modify
            // End of variables declaration
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default

    I have a dumb question: what is the width and height of the ImagePanel displaying the image?

    In the paintComponent() method, does that if(image==null) evaluate to true? Do you ever get to the paintImage() call?

    And why do you have an image variable in your outer class?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    I agree, are you absolutely sure that the Image object is not null? You are using two separate objects inside your program named "image". You could check real easy to see if it is evaluating correctly:

    Java Code:
    if (image == null) {
         System.out.println("image returned null!");
         return;
    }

    Also, if you are deadset on using two separate variables named "image", i would also force the inner class ImagePanel to use its copy of the object.

    Java Code:
    private class ImagePanel extends JPanel
    {
    	private Image image;
    
    	public ImagePanel()
    	{
    		URL url = jarredImages.class.getResource("/s2000.jpg");
    			try { this.image = ImageIO.read(url); }
    			catch (Exception ee) { ee.printStackTrace(); }
    			System.out.println("width = " + this.image.getWidth(this));
    	}
    
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		if (this.image == null) return;
    			g.drawImage(this.image, 1, 1, this);
    	}
    }
    I have a dumb question: what is the width and height of the ImagePanel displaying the image?
    Also, as KevinWorkman has asked, are you sure of the width and height of the panel? The code you have provided does not show us where, or within what type of container, your ImagePanel is going to live. None of your code modifies the actual size of the panel.

    If you were to put this panel inside a frame or another container with a BorderLayout, in pane Center, the panel should automatically resize, but from your code there is no way of telling.

    I would also try and programatically set the size of your container.

  4. #4
    apchar is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    It was the size thing. I just assumed the panel would fill the frame. Apparently not. When I set the size of the panel to that of the image it worked just fine. Thanks guys!

Similar Threads

  1. My jar files wont run correctly
    By fatalen in forum Java Applets
    Replies: 1
    Last Post: 03-24-2011, 03:31 AM
  2. wont display selected array
    By newbie80 in forum New To Java
    Replies: 7
    Last Post: 03-21-2011, 02:10 AM
  3. uploaded image wont display if i change filename
    By schenker in forum Java Servlet
    Replies: 12
    Last Post: 06-11-2010, 12:13 PM
  4. How to display image from byte array in JPANEL
    By waqasdaar in forum AWT / Swing
    Replies: 0
    Last Post: 03-22-2009, 01:11 AM
  5. Storing image loaded in applet in mysql database
    By mrvigneshmca in forum Java Applets
    Replies: 1
    Last Post: 03-20-2009, 05:17 PM

Posting Permissions

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