Results 1 to 9 of 9
- 06-28-2008, 07:59 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 13
- Rep Power
- 0
NullPointerException - createImage()
In this prgram i get NullpoinerExceptiion. Program reports it in lin marked ***, but the problem one line above (marked **). After calling createImage mamImage is still null.
What is the problem in how to fix it?
Java Code:import java.awt.*; import javax.swing.*; public class Panela extends JPanel implements Runnable{ int korak=0; int zakasnitev=50; boolean running=false; Image memImage; Graphics memGraphics; Thread nit; Panela(){ **memImage= createImage(500,500); ***memGraphics = memImage.getGraphics(); setVisible(true); start(); } public void run(){ while(running){ long cas=System.currentTimeMillis(); korak++; repaint(); cas=System.currentTimeMillis(); try{ if(cas<zakasnitev) Thread.sleep(zakasnitev-(int) cas); } catch(InterruptedException e){e.printStackTrace(); System.exit(-1);} } } public void start(){ if(!running) { nit= new Thread(this); nit.start(); running=true; } } public void paint(Graphics g){ update(g); } public void update(Graphics g){ memGraphics.setColor(getBackground()); memGraphics.fillRect(0, 0, getWidth(), getHeight()); memGraphics.setColor(Color.black); int odmik= korak % 20; for(int x=0; x<getWidth()+getHeight(); x+=20){ memGraphics.drawLine(x, 0, 0, x); }//for g.drawImage(memImage, 0, 0, this); } }I HATE SMURFS!!!!!
-
I've never used Component.getImage, so I'm not that familiar with its use, or whether what you are trying to do (what are you trying to do, by the way) is the correct way to do things, but I'm going to guess that no image will be available until a component has been rendered either with a call to pack() or setVisible(true) on a root container that holds your panel.
But again, what is your goal here? You're overriding paint which probably shouldn't be done. Instead you should probably override paintComponent.
Also, you're using a while(true) construct, and I'll bet you want to use a Swing Timer instead.
HTH.
- 06-28-2008, 11:41 PM #3
Have you tried the code without the memGraphics object? Are you trying to double buffer the output?
For Swing components, override paintComponent() vs paint().
- 06-28-2008, 11:55 PM #4
Member
- Join Date
- Aug 2007
- Posts
- 13
- Rep Power
- 0
My goal is learn about graphics and animation.
The whole program is from a book, but it was written for applets and im trying to conver it normal program.
I have tried the code without memGraphics and i works
and yes the point of it double buffering.I HATE SMURFS!!!!!
- 06-29-2008, 03:59 AM #5
Java Code:import java.awt.*; import javax.swing.*; public class PanelaRx extends JPanel implements Runnable{ int korak=0; int zakasnitev=50; boolean running=false; Image memImage; Graphics memGraphics; Thread nit; PanelaRx(){ // See Method Detail for Component [i]createImage[/i] method. System.out.println("createImage = " + createImage(500,500)); // memImage= createImage(500,500); // memGraphics = memImage.getGraphics(); // setVisible(true); startFirstTime(); } public void run(){ while(running){ long cas=System.currentTimeMillis(); korak++; update(); repaint(); cas=System.currentTimeMillis(); try{ if(cas<zakasnitev) Thread.sleep(zakasnitev-(int) cas); } catch(InterruptedException e) { e.printStackTrace(); System.exit(-1); } } } public void start(){ if(!running) { running=true; nit= new Thread(this); nit.setPriority(Thread.NORM_PRIORITY); nit.start(); } } private void startFirstTime() { // Wait for gui to be realized. new Thread(new Runnable() { public void run() { do { try { Thread.sleep(25); } catch(InterruptedException e) { System.out.println("wait interrupted"); break; } } while(!isDisplayable()); // Now it should be safe to make these calls. memImage= createImage(500,500); memGraphics = memImage.getGraphics(); start(); } }).start(); } protected void paintComponent(Graphics g){ g.drawImage(memImage, 0, 0, this); } /** * Swing is double buffered by default. There are times when * it is useful in Swing. It is recommended that we do not * override the [i]update[/i] method in swing (see Method * Detail section in JComponent api). Your drawing code looks * like older AWT drawing code. */ private void update(){ memGraphics.setColor(getBackground()); memGraphics.fillRect(0, 0, getWidth(), getHeight()); memGraphics.setColor(Color.black); int odmik= korak % 20; for(int x=0; x<getWidth()+getHeight(); x+=20){ memGraphics.drawLine(x, 0, 0, x); } } public Dimension getPreferredSize() { return new Dimension(500,500); } public static void main(String[] nargs) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new PanelaRx()); f.pack(); f.setLocation(200,200); f.setVisible(true); } }
- 06-29-2008, 04:02 AM #6
I think Swing components are automatically double buffered.
Look at using the BufferedImage class to build your image in before drawing it.
- 06-29-2008, 01:04 PM #7
Member
- Join Date
- Aug 2007
- Posts
- 13
- Rep Power
- 0
Thank you all for your help.
It seem the problem was frame wasn created by the time image was created (if i understood it correctly).
I also have a few other questions:
Why is overrindind paintComponent better than overrinding paint?
hardwired: thank you for the code, but it seems to be "blickering" (i hope i said that right), which was the thing i was trying to aviod.
Also here is my new working code(althought not as good as it should be.
It overides paintC instead of PaintComponent (i did try overriding painComponent, but I spotted no difference between the two, so i just left it paint).
And i could include delay or some other failsefe that memImage wont initialize before frame.
Here is my working code, any comment are welcome.
Java Code:import java.awt.*; import javax.swing.*; public class Panela extends JPanel implements Runnable{ int korak=0; int zakasnitev=500; boolean running=false; Image memImage; Graphics memGraphics; Thread nit; Panela(){ } public void run(){ while(running){ long cas=System.currentTimeMillis(); korak++; repaint(); cas=System.currentTimeMillis(); try{ if(cas<zakasnitev) Thread.sleep(zakasnitev-(int) cas); } catch(InterruptedException e){e.printStackTrace(); System.exit(-1);} } } public void start(){ if(!running) { nit= new Thread(this); nit.start(); running=true; } } public void paintComponent(Graphics g){ memImage= createImage(500,500); memGraphics = memImage.getGraphics(); if(!running) start(); update(g); } public void update(Graphics g){ memGraphics.setColor(getBackground()); memGraphics.fillRect(0, 0, getWidth(), getHeight()); memGraphics.setColor(Color.black); int odmik= korak % 20; for(int x=0; x<getWidth()+getHeight(); x+=20){ memGraphics.drawLine(x, 0, 0, x); }//for g.drawImage(memImage, 0, 0, this); } }Last edited by Bojevnik; 06-29-2008 at 01:06 PM. Reason: just waqnted make some thing clearer
I HATE SMURFS!!!!!
-
Ah, just as I suspected!
paintComponent is responsible for just that: painting the component. paint on the other hand is responsible for more: painting the component, painting the border, and painting the component's children. If your paint override method is not attempting to affect the painting of the border or the child components, it's best not to mess with it and instead you should override the more specific method, paintComponent.I also have a few other questions:
Why is overrindind paintComponent better than overrinding paint?
Good luck.
- 06-29-2008, 09:20 PM #9
Java Code:import java.awt.*; import javax.swing.*; public class PanelaRx extends JPanel implements Runnable{ Thread nit; boolean running=false; int korak = 0; PanelaRx(){ start(); } public void run(){ while(running){ korak++; repaint(); try{ Thread.sleep(100); } catch(InterruptedException e) { break; } } } public void start(){ if(!running) { running=true; nit= new Thread(this); nit.setPriority(Thread.NORM_PRIORITY); nit.start(); } } protected void paintComponent(Graphics g){ super.paintComponent(g); int odmik= korak % 20; for(int x=odmik; x<getWidth()+getHeight(); x+=20){ g.drawLine(x, 0, 0, x); } } public static void main(String[] nargs) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new PanelaRx()); f.setSize(500,500); f.setLocation(200,200); f.setVisible(true); } }
Similar Threads
-
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 04:20 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM -
nullPointerException problem
By conandor in forum NetworkingReplies: 1Last Post: 08-14-2007, 01:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks