Can't draw graphics.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HxRLxY
    New Member
    • Sep 2008
    • 23

    Can't draw graphics.

    I am trying to write a simple program which will allow users to view pictures. I am loading an image as an icon, then assigning that ImageIcon to an Image object. Then i get the graphics context of my panel. Finally I am trying to use the drawImage method to draw the Image object to the panel. However I keep getting a nullpointerexce ption when I run the program. I think it has something to do with the drawImage statement I use but I don't know. Any help would be greatly appreciated.

    Here is my source code:

    Code:
     public class ItemPictureViewer
       {
          
           public static void main(String[] args)
          {
          
             JFrame frame = new JFrame("Image Demo");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setResizable(false);
             
             DemoPane panel_1 = new DemoPane();
             
             frame.setPreferredSize (new Dimension (250,325));
             frame.getContentPane().add(panel_1);
             frame.setLocationRelativeTo (null);
             frame.pack();
             frame.setVisible(true);
          	
          
          }
       
       }
       
        class DemoPane extends JPanel 
       {
          JPanel panelImages, panelButtons;
          JButton btnRight, btnLeft;
          JLabel labelPics;
          final int WIDTH = 250, HEIGHT = 325;
          Image eyeball, dewgrass, dna, dock, face;
          ImageIcon eyeballIcon, dewgrassIcon, dnaIcon, dockIcon, faceIcon;
          Image[] imageList;
          
             	
            	
           public DemoPane()
          {	
             
          	//assign images to the icons
             eyeballIcon = new ImageIcon ("EyeBall.jpg");
             dewgrassIcon = new ImageIcon ("DewGrass.jpg");
             dnaIcon = new ImageIcon ("DNA.jpg");
             dockIcon = new ImageIcon ("Dock.jpg");
             faceIcon = new ImageIcon ("Face.jpg");
          	
          	//assign the icons to their respective images
             eyeball = eyeballIcon.getImage();
             dewgrass = dewgrassIcon.getImage();
             dock = dockIcon.getImage();
             dna = dnaIcon.getImage();
             face = faceIcon.getImage();
          	
          	//set up image array
             Image[] imageList = {eyeball,
                   					dewgrass,
                   					dock,
                   					dna,
                   					face};
          									
          	//set pane layout
             setLayout (new BorderLayout());
          
             //set up panels
             panelImages = new JPanel();
             panelButtons = new JPanel();
          	
          	//set up label
             labelPics = new JLabel();
          	
          	//set up buttons
             btnRight = new JButton ("Next");
             btnLeft = new JButton ("Previous");	
          	
          	//set up layout of buttons
             panelButtons.setLayout (new GridLayout (1,2));
             panelButtons.add (btnLeft);
             panelButtons.add (btnRight);
             
             //draw image to panelImages
             Graphics g = panelImages.getGraphics();
             g.drawImage (imageList[1], 1, 1, panelImages);     
             
                          	
          	//set up window
             add (panelImages, BorderLayout.CENTER);
             add (panelButtons, BorderLayout.SOUTH);
             
            
              
          }
            
           
       }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    At what line does your NullPointerExce ption occur?

    kind regards,

    Jos

    Comment

    • HxRLxY
      New Member
      • Sep 2008
      • 23

      #3
      Originally posted by JosAH
      At what line does your NullPointerExce ption occur?

      kind regards,

      Jos
      This is the message my runtime I/O gives me.

      Exception in thread "main" java.lang.NullP ointerException
      at DemoPane.<init> (ItemPictureVie wer.java:90)
      at ItemPictureView er.main(ItemPic tureViewer.java :20)


      Through my feeble attempts at debugging, I found that if I comment-out the line
      g.drawImage (imageList[1], 1, 1, panelImages) then the exception doesn't occur. But that actually defeats the purpose of the program. So I'm kinda stuck

      Thanks,
      HxRLxy
      Last edited by HxRLxY; Sep 12 '08, 12:59 AM. Reason: had written wrong I/O

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by HxRLxY
        This is the message my runtime I/O gives me.

        Exception in thread "main" java.lang.NullP ointerException
        at DemoPane.<init> (ItemPictureVie wer.java:90)
        at ItemPictureView er.main(ItemPic tureViewer.java :20)


        Through my feeble attempts at debugging, I found that if I comment-out the line
        g.drawImage (imageList[1], 1, 1, panelImages) then the exception doesn't occur. But that actually defeats the purpose of the program. So I'm kinda stuck

        Thanks,
        HxRLxy
        So that tells you that one of the objects accessed at that line is null at the time at which that line is invoked. There is only a finite number of objects there so you should be able to devise a way of finding out which one.

        Comment

        • HxRLxY
          New Member
          • Sep 2008
          • 23

          #5
          Originally posted by r035198x
          So that tells you that one of the objects accessed at that line is null at the time at which that line is invoked. There is only a finite number of objects there so you should be able to devise a way of finding out which one.
          Well I messed around with it some more and found that when I call the Graphics g = panelImages.get Graphics() , the g object that is returned is null. But I can't figure out why it's null. I know that the JPanels have a graphics context. And the Java API says the way to get that object is through the getGraphics method. So I'm really not sure what I'm doing wrong.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            The documentation tells you that the method will return null if the component is
            currently not displayable.

            kind regards,

            Jos

            Comment

            • HxRLxY
              New Member
              • Sep 2008
              • 23

              #7
              Okay. I changed my code to allow the panel to be displayed before calling the getGraphics method. I added a method to the end of my DemoPane class to draw the image after the panel is shown. So now the panelImages.get Graphics call doesn't return null (according to my debugger), however I am still getting a NullPointerExec ption at g.drawImage (imageList[0], 0, 0, null). Thank you again for your help.

              Here is my revised code:

              Code:
                  public class ItemPictureViewer
                 {
                    
                     public static void main(String[] args)
                    {
                    
                       JFrame frame = new JFrame("Image Demo");
                       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                       frame.setResizable(false);  
                               
                       DemoPane panel_1 = new DemoPane();
                       
                       frame.setPreferredSize (new Dimension (250,325));
                       frame.getContentPane().add(panel_1);
                       frame.setLocationRelativeTo (null);
                       frame.pack();
                       frame.setVisible(true);
                       
                       panel_1.draw();
                    }
                    
                 
                 }
                 
                  class DemoPane extends JPanel 
                 {
                    JPanel panelImages, panelButtons;
                    JButton btnRight, btnLeft;
                    JLabel labelPics;
                    final int WIDTH = 250, HEIGHT = 325;
                    Image eyeball, dewgrass, dna, dock, face;
                    ImageIcon eyeballIcon, dewgrassIcon, dnaIcon, dockIcon, faceIcon;
                    Image[] imageList;
                    
                       	
                      	
                     public DemoPane()
                    {	
                       
                    	//assign images to the icons
                       eyeballIcon = new ImageIcon ("EyeBall.jpg");
                       dewgrassIcon = new ImageIcon ("DewGrass.jpg");
                       dnaIcon = new ImageIcon ("DNA.jpg");
                       dockIcon = new ImageIcon ("Dock.jpg");
                       faceIcon = new ImageIcon ("Face.jpg");
                    	
                    	//assign the icons to their respective images
                       eyeball = eyeballIcon.getImage();
                       dewgrass = dewgrassIcon.getImage();
                       dock = dockIcon.getImage();
                       dna = dnaIcon.getImage();
                       face = faceIcon.getImage();
                    	
                    	//set up image array
                       Image[] imageList = {eyeball,
                             					dewgrass,
                             					dock,
                             					dna,
                             					face};
                    									
                    	//set pane layout
                       setLayout (new BorderLayout());
                    
                       //set up panels
                       panelImages = new JPanel();
                       panelButtons = new JPanel();
                    	
                    	//set up label
                       labelPics = new JLabel();
                    	
                    	//set up buttons
                       btnRight = new JButton ("Next");
                       btnLeft = new JButton ("Previous");	
                    	
                    	//set up layout of buttons
                       panelButtons.setLayout (new GridLayout (1,2));
                       panelButtons.add (btnLeft);
                       panelButtons.add (btnRight);
                       
                           
                       
                                    	
                    	//set up window
                       add (panelImages, BorderLayout.CENTER);
                       add (panelButtons, BorderLayout.SOUTH);
                                
                    }
                    
                     public void draw()
                    {
                           
                       Graphics g = panelImages.getGraphics();
                       g.drawImage (imageList[0], 0, 0, panelImages);
                    }
                    
                 }

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Have you read (and understood) how painting works in Swing? It is very much
                unlike how you attempt to accomplish it. You should implement/override a
                paintComponent( ) method that gets a Graphics object passed as its parameter
                and is called by the AWT dispatch thread whenever needed.

                kind regards,

                Jos

                Comment

                Working...