473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Can't draw graphics.

23
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 nullpointerexception 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:

Expand|Select|Wrap|Line Numbers
  1.  public class ItemPictureViewer
  2.    {
  3.  
  4.        public static void main(String[] args)
  5.       {
  6.  
  7.          JFrame frame = new JFrame("Image Demo");
  8.          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9.          frame.setResizable(false);
  10.  
  11.          DemoPane panel_1 = new DemoPane();
  12.  
  13.          frame.setPreferredSize (new Dimension (250,325));
  14.          frame.getContentPane().add(panel_1);
  15.          frame.setLocationRelativeTo (null);
  16.          frame.pack();
  17.          frame.setVisible(true);
  18.  
  19.  
  20.       }
  21.  
  22.    }
  23.  
  24.     class DemoPane extends JPanel 
  25.    {
  26.       JPanel panelImages, panelButtons;
  27.       JButton btnRight, btnLeft;
  28.       JLabel labelPics;
  29.       final int WIDTH = 250, HEIGHT = 325;
  30.       Image eyeball, dewgrass, dna, dock, face;
  31.       ImageIcon eyeballIcon, dewgrassIcon, dnaIcon, dockIcon, faceIcon;
  32.       Image[] imageList;
  33.  
  34.  
  35.  
  36.        public DemoPane()
  37.       {    
  38.  
  39.           //assign images to the icons
  40.          eyeballIcon = new ImageIcon ("EyeBall.jpg");
  41.          dewgrassIcon = new ImageIcon ("DewGrass.jpg");
  42.          dnaIcon = new ImageIcon ("DNA.jpg");
  43.          dockIcon = new ImageIcon ("Dock.jpg");
  44.          faceIcon = new ImageIcon ("Face.jpg");
  45.  
  46.           //assign the icons to their respective images
  47.          eyeball = eyeballIcon.getImage();
  48.          dewgrass = dewgrassIcon.getImage();
  49.          dock = dockIcon.getImage();
  50.          dna = dnaIcon.getImage();
  51.          face = faceIcon.getImage();
  52.  
  53.           //set up image array
  54.          Image[] imageList = {eyeball,
  55.                                    dewgrass,
  56.                                    dock,
  57.                                    dna,
  58.                                    face};
  59.  
  60.           //set pane layout
  61.          setLayout (new BorderLayout());
  62.  
  63.          //set up panels
  64.          panelImages = new JPanel();
  65.          panelButtons = new JPanel();
  66.  
  67.           //set up label
  68.          labelPics = new JLabel();
  69.  
  70.           //set up buttons
  71.          btnRight = new JButton ("Next");
  72.          btnLeft = new JButton ("Previous");    
  73.  
  74.           //set up layout of buttons
  75.          panelButtons.setLayout (new GridLayout (1,2));
  76.          panelButtons.add (btnLeft);
  77.          panelButtons.add (btnRight);
  78.  
  79.          //draw image to panelImages
  80.          Graphics g = panelImages.getGraphics();
  81.          g.drawImage (imageList[1], 1, 1, panelImages);     
  82.  
  83.  
  84.           //set up window
  85.          add (panelImages, BorderLayout.CENTER);
  86.          add (panelButtons, BorderLayout.SOUTH);
  87.  
  88.  
  89.  
  90.       }
  91.  
  92.  
  93.    }
  94.  
Sep 11 '08 #1
7 3162
JosAH
11,448 Expert 8TB
At what line does your NullPointerException occur?

kind regards,

Jos
Sep 11 '08 #2
HxRLxY
23
At what line does your NullPointerException occur?

kind regards,

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

Exception in thread "main" java.lang.NullPointerException
at DemoPane.<init>(ItemPictureViewer.java:90)
at ItemPictureViewer.main(ItemPictureViewer.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
Sep 12 '08 #3
r035198x
13,262 8TB
This is the message my runtime I/O gives me.

Exception in thread "main" java.lang.NullPointerException
at DemoPane.<init>(ItemPictureViewer.java:90)
at ItemPictureViewer.main(ItemPictureViewer.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.
Sep 12 '08 #4
HxRLxY
23
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.getGraphics() , 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.
Sep 12 '08 #5
JosAH
11,448 Expert 8TB
The documentation tells you that the method will return null if the component is
currently not displayable.

kind regards,

Jos
Sep 12 '08 #6
HxRLxY
23
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.getGraphics call doesn't return null (according to my debugger), however I am still getting a NullPointerExecption at g.drawImage (imageList[0], 0, 0, null). Thank you again for your help.

Here is my revised code:

Expand|Select|Wrap|Line Numbers
  1.     public class ItemPictureViewer
  2.    {
  3.  
  4.        public static void main(String[] args)
  5.       {
  6.  
  7.          JFrame frame = new JFrame("Image Demo");
  8.          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9.          frame.setResizable(false);  
  10.  
  11.          DemoPane panel_1 = new DemoPane();
  12.  
  13.          frame.setPreferredSize (new Dimension (250,325));
  14.          frame.getContentPane().add(panel_1);
  15.          frame.setLocationRelativeTo (null);
  16.          frame.pack();
  17.          frame.setVisible(true);
  18.  
  19.          panel_1.draw();
  20.       }
  21.  
  22.  
  23.    }
  24.  
  25.     class DemoPane extends JPanel 
  26.    {
  27.       JPanel panelImages, panelButtons;
  28.       JButton btnRight, btnLeft;
  29.       JLabel labelPics;
  30.       final int WIDTH = 250, HEIGHT = 325;
  31.       Image eyeball, dewgrass, dna, dock, face;
  32.       ImageIcon eyeballIcon, dewgrassIcon, dnaIcon, dockIcon, faceIcon;
  33.       Image[] imageList;
  34.  
  35.  
  36.  
  37.        public DemoPane()
  38.       {    
  39.  
  40.           //assign images to the icons
  41.          eyeballIcon = new ImageIcon ("EyeBall.jpg");
  42.          dewgrassIcon = new ImageIcon ("DewGrass.jpg");
  43.          dnaIcon = new ImageIcon ("DNA.jpg");
  44.          dockIcon = new ImageIcon ("Dock.jpg");
  45.          faceIcon = new ImageIcon ("Face.jpg");
  46.  
  47.           //assign the icons to their respective images
  48.          eyeball = eyeballIcon.getImage();
  49.          dewgrass = dewgrassIcon.getImage();
  50.          dock = dockIcon.getImage();
  51.          dna = dnaIcon.getImage();
  52.          face = faceIcon.getImage();
  53.  
  54.           //set up image array
  55.          Image[] imageList = {eyeball,
  56.                                    dewgrass,
  57.                                    dock,
  58.                                    dna,
  59.                                    face};
  60.  
  61.           //set pane layout
  62.          setLayout (new BorderLayout());
  63.  
  64.          //set up panels
  65.          panelImages = new JPanel();
  66.          panelButtons = new JPanel();
  67.  
  68.           //set up label
  69.          labelPics = new JLabel();
  70.  
  71.           //set up buttons
  72.          btnRight = new JButton ("Next");
  73.          btnLeft = new JButton ("Previous");    
  74.  
  75.           //set up layout of buttons
  76.          panelButtons.setLayout (new GridLayout (1,2));
  77.          panelButtons.add (btnLeft);
  78.          panelButtons.add (btnRight);
  79.  
  80.  
  81.  
  82.  
  83.           //set up window
  84.          add (panelImages, BorderLayout.CENTER);
  85.          add (panelButtons, BorderLayout.SOUTH);
  86.  
  87.       }
  88.  
  89.        public void draw()
  90.       {
  91.  
  92.          Graphics g = panelImages.getGraphics();
  93.          g.drawImage (imageList[0], 0, 0, panelImages);
  94.       }
  95.  
  96.    }
  97.  
Sep 12 '08 #7
JosAH
11,448 Expert 8TB
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
Sep 13 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jan Kopcsek | last post by:
Hello, is there a way to draw Windows.Forms components into a specified surface (a D3DSurface for example)? i want to fill a texture with the graphics and render it to the screen. it looks like...
6
by: John | last post by:
I wrote this class class CLine { public float sX,sY,dX,dY,m,x,y; public void Draw (PaintEventArgs g) { //Graphics g = this.CreateGraphics (); Bitmap bm=new Bitmap(1,1);
3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
1
by: Robin Tucker | last post by:
Hi ppl, My owner draw list box controls do not "refresh" old selected items when a new selection is made. This means that as you click to make selections, the previously selected items stay...
2
by: dan heskett | last post by:
I am owner-drawing a listbox, in an attempt to create a nice list with some custom "fields" and text layout. Essentially it works, but I must be missing something big, conceptually, because I...
0
by: Smokey Grindle | last post by:
This is just a wierd one... I am trying to draw the sub items in the drawitem event of the list view in owner drawn mode (because of documented W32 rendering bugs) instead of in two seperate items...
2
by: Overseer | last post by:
What is the best way to draw graphics like Task Manager's Performance Graphic??
2
by: =?Utf-8?B?S3VuIE5pdQ==?= | last post by:
Dear all, I'm new here and I'm sorry to trouble. But I've got the following problem when trying to draw a PNG file on a panel. My code is like this: Image mapImage = Image.FromFile...
14
by: Joe | last post by:
this file is drawn in VB.NET and input output goes by XmlSerializer. Therefore, simple output file looks like: <?xml version="1.0" encoding="utf-8"?> <DrawablePicture...
8
by: ofiras | last post by:
Hi, I made a "Paint" program, but I couldn't find a method to paint 1 pixel using graphic state ("Graphics g = Graphics.FromHwnd(this.Handle);") How can I paint 1 pixel? I guess I can make a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.