Results 1 to 4 of 4
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Image Not Displaying

    I have written a class that displays image in my program. The class extends JPanel and uses the paint component method to draw the image. tThe problem is that the image is not being displayed on the frame.

    The image Class
    Java Code:
    package classes;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.io.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    
    /**
     *
     * @author Mario
     */
    public class Images extends JPanel {
        BufferedImage img;
        
        //constructor
        public Images (String url)
        {
            setImage (url);
        }//end constructor
        
       
    
        public void setImage (String url)
        {
            try
            {
                img = ImageIO.read(new File (url));
            }
            catch (Exception e)
            {
                System.err.println (e.getMessage ());
            }
        }//end setImage
        
        public void paintComponent (Graphics g)
        {
            Graphics2D m = (Graphics2D)g;
            super.paintComponent (m);
           
            if (img != null)
            {
                m.drawImage(img,30, 23,null);
            }
            else
                System.err.println ("File was not loaded");
        }//end paintComponent method
        
    }//end Images class
    The class that haves the frame
    Java Code:
    package student.management.sys;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.BorderFactory;
    import java.awt.event.*;
    import javax.swing.ImageIcon;
    import classes.*;
    
    
    
    /**
     *
     * @author Mario
     */
    public class LogIn extends JFrame implements ActionListener,MouseListener{
        
        private JTextField userNameTf;
        private JPasswordField passwordTf;
        private JLabel label;
      
        //class constructor
        public LogIn ()
        {
            setTitle ("Log In");
            setSize (400,350);
            //setResizable (false);
            setLocation ();
            setLayout (new FlowLayout ());
            setDefaultCloseOperation (EXIT_ON_CLOSE);
            
            setMenuBar ();//creates JMenuBar
            LogInPanel ();//create login panel with log components
            setLogo ();//sets ncu logo
       
            setVisible (true);
        }//end constructor
        
     
        //centers logIn window on screen
        final public void setLocation ()
        {
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            int width = super.getSize().width;
            int height = super.getSize().height;
            int x = (screen.width - width) / 2;
            int y = ((screen.height - height)/ 2) - 50;
            
            super.setLocation (x,y);
        }//end setLocation
        
        
        //adds menu bar 
        final public void setMenuBar ()
        {
            JMenuBar mBar = new JMenuBar ();
            JMenu file = new JMenu ("File");
            JMenu help = new JMenu ("help");
            JMenuItem exit = new JMenuItem ("Exit");
            
            mBar.add(file);
            mBar.add(help);
            file.add(exit);
            setJMenuBar (mBar);
            
            //add ActionListeners
            exit.addActionListener (this);
        }//end setMenuBar
        
        //creates JPanel that contain login components
        final public void LogInPanel ()
        {
            //initialize instance variables
            userNameTf = new JTextField (12);
            passwordTf = new JPasswordField (12);
            JButton logInBtn =  new JButton ("Log In");
            JCheckBox remCb = new JCheckBox ("Remember me");
            label = new JLabel ("<html><u>Forgot password</u></html>");
            
            
            remCb.setForeground (Color.BLUE);
            remCb.setFont (new Font ("Segoe UI",Font.PLAIN,11));
            label.setFont (new Font ("Segoe UI",Font.PLAIN,11));
            
            
            //create JLabels 
            JLabel userNameLb = new JLabel ("User Name: ");
            JLabel passwordLb = new JLabel ("Password: ");
            
            
            //create JPanel and set properties
            JPanel panel = new JPanel (new GridBagLayout ());
            panel.setPreferredSize (new Dimension (250,180));
            panel.setBorder (BorderFactory.createTitledBorder ("Log In"));
            
            GridBagConstraints c = new GridBagConstraints ();
            c.insets = new Insets (5,5,5,5);
            
            //userNameLb constraints
            c.gridx = 0;
            c.gridy = 0;
            panel.add(userNameLb,c);
            
            
            //userNameTf constraints
            c.gridx = 1;
            c.gridy = 0;
            panel.add(userNameTf,c);
            
            
            //passwordLb constraints
            c.gridx = 0;
            c.gridy = 1;
            panel.add(passwordLb,c);
            
            
            //passwordTf constraints
            c.gridx = 1;
            c.gridy = 1;
            panel.add(passwordTf,c);
            
            c.gridx = 0;
            c.gridy = 2;
            panel.add(remCb,c); 
         
            //logInBtn constraints
            c.gridx = 1;
            c.gridy = 2;
            panel.add(label,c);
            
            c.gridx = 1;
            c.gridy = 3;
            panel.add(logInBtn,c);
                  
            
            add(panel);
            
            //add ActionListeners
            userNameTf.addActionListener (this);
            passwordTf.addActionListener (this);
            logInBtn.addActionListener (this);
            label.addMouseListener(this);
           
        }//end logInPanel method
       
       
       //sets application logo
        final public void setLogo ()
       {
          // ImageIcon icon = new ImageIcon ("ncu_logo_2.png");
          // JLabel lb = new JLabel (icon);
          // add(lb);
           
           Images img = new Images ("planetof the ape.jpg");
            add(img);
           
       }//end setLogo method
    Image Not Displaying-example.png

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

    Default Re: Image Not Displaying

    Your image holding JPanel has no reason to make its size bigger than very small. I suggest that you either use a JLabel/ImageIcon for this purpose or give your image-holding JPanel a getPreferredSize method override that makes sense.

    e.g.,

    Java Code:
       @Override
       public Dimension getPreferredSize() {
          if (img == null) {
             return super.getPreferredSize();
          } else {
             // borrowing the constants from  your paintComponent method
             // should be true constants though
             // not magic numbers
             int width = img.getWidth() + 30; 
             int height = img.getHeight() + 23;
             return new Dimension(width, height);
          }
       }

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: Image Not Displaying

    Thanks I override the getPrefferedSize method and the problem was fix. Question though is using the paintComponent method to paint the image more optimal that using ImageIcon and label to display the image

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

    Default Re: Image Not Displaying

    Quote Originally Posted by mDennis10 View Post
    Thanks I override the getPrefferedSize method and the problem was fix.
    Great!

    Question though is using the paintComponent method to paint the image more optimal that using ImageIcon and label to display the image
    Define "optimal". Using an ImageIcon in a JLabel is certainly a lot easier while overriding paintComponent is a bit more flexible.

Similar Threads

  1. Displaying an Image!
    By javabeginner29 in forum Java Applets
    Replies: 3
    Last Post: 02-15-2012, 12:08 PM
  2. Image not displaying
    By sandeep43 in forum New To Java
    Replies: 20
    Last Post: 07-08-2011, 12:31 AM
  3. Displaying an image
    By BariMutation in forum New To Java
    Replies: 6
    Last Post: 11-12-2010, 01:23 AM
  4. Displaying an image
    By SWEngineer in forum AWT / Swing
    Replies: 11
    Last Post: 01-28-2010, 05:48 AM
  5. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 06: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
  •