Results 1 to 11 of 11
Thread: offscreen.getGraphics() error
- 01-20-2012, 01:04 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
offscreen.getGraphics() error
I'm trying to write a simple spaceship game using Java.
It's annoying that images flash when they move on a window.
I have heard about double buffering but I can't get it work.
First I create a program that draws a rectangle which moves when yoy press right and left arrows:
Java Code:import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; class DoubleBuffering { public static void main(String[] args)throws InterruptedException{ InteractiveWindow myWindow=new InteractiveWindow(); myWindow.setSize(500,400); myWindow.setVisible(true); myWindow.setResizable(false); myWindow.setTitle("Moving Rectangle"); } } class InteractiveWindow extends Frame implements WindowListener, KeyListener{ Dimension dim; int posx=300; int posy=300; public InteractiveWindow(){ addKeyListener(this); addWindowListener(this); } public void paint(Graphics g) { g.drawRect(posx,posy,50,50); } public void keyPressed(KeyEvent e) { int numKey = e.getKeyCode(); if (numKey==37) { posx--; repaint(); } if (numKey==39){ posx++; repaint(); } } public void keyReleased(KeyEvent arg0) {} public void keyTyped(KeyEvent arg0) {} public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {System.exit(0);} public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} }
Then I tryed to introduce an offscreen image to draw on it and then draw it on the Graphics variable of the paint(Graphics g) method, like this: (look at lines 29 and 30)
But I get a NullPointer Exception on line:Java Code:import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; class DoubleBuffering { public static void main(String[] args)throws InterruptedException{ InteractiveWindow myWindow=new InteractiveWindow(); myWindow.setSize(500,400); myWindow.setVisible(true); myWindow.setResizable(false); myWindow.setTitle("Moving Rectangle"); } } class InteractiveWindow extends Frame implements WindowListener, KeyListener{ Dimension dim; int posx=300; int posy=300; Graphics myGraphic; Image offscreen; public InteractiveWindow(){ dim=getSize(); offscreen=createImage(500,400); myGraphic=offscreen.getGraphics(); addKeyListener(this); addWindowListener(this); } public void paint(Graphics g) { myGraphic.drawRect(posx,posy,50,50); g.drawImage(offscreen,0,0,this); } public void keyPressed(KeyEvent e) { int numKey = e.getKeyCode(); if (numKey==37) { posx--; repaint(); } if (numKey==39){ posx++; repaint(); } } public void keyReleased(KeyEvent arg0) {} public void keyTyped(KeyEvent arg0) {} public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {System.exit(0);} public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} }
myGraphic=offscreen.getGraphics();
What's wrong?
- 01-20-2012, 01:29 PM #2
Re: offscreen.getGraphics() error
Try using Swing classes instead of AWT classes.
With Swing classes you override the paintComponent method instead of the paint method.
- 01-20-2012, 01:49 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: offscreen.getGraphics() error
Another AWT problem.
Is there some bogus tutorial out there that focuses on AWT or something?
- 01-20-2012, 01:55 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: offscreen.getGraphics() error
When I use JFrame and paintComponent, the rectangle doesn't even appear
- 01-20-2012, 01:57 PM #5
Re: offscreen.getGraphics() error
Please post the code with the problem.
- 01-20-2012, 04:14 PM #6
Re: offscreen.getGraphics() error
I wouldn't expect it to. Custom painting isn't done in a top level window.
Please go through Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-21-2012, 12:34 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: offscreen.getGraphics() error
This is why I didnt want to use swing components Its getting more and more complicated.
What should I use to paint on?
A Jpane inside a Jframe? A canvas?
- 01-21-2012, 01:36 AM #8
Re: offscreen.getGraphics() error
Neither are Swing classes.A Jpane inside a Jframe? A canvas?
Try a JPanel
- 01-21-2012, 01:22 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: offscreen.getGraphics() error
Thank you guys. I did it using JPanel.
I had to add a setFocusable() so that it could listen the keboard.
And had to add a super.paintComponent() so that the JPanel might clear everytime.
But at last, it's fine. I don't need double buffering beacause Jpanel implements it.
- 01-21-2012, 01:29 PM #10
Re: offscreen.getGraphics() error
Glad you got it working.
- 01-21-2012, 09:43 PM #11
Re: offscreen.getGraphics() error
Swing is designed to use Key Bindings. Learning how they work and reimplementing without a KeyListener would be time well spent.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Question on getGraphics() in a JComponent
By Shayke_ in forum Java 2DReplies: 2Last Post: 02-10-2011, 07:31 PM -
getGraphics() method problem
By Aleksas in forum AWT / SwingReplies: 1Last Post: 05-25-2010, 11:34 PM -
JApplet.getGraphics() draws but JFrame doesn't
By ChazZeromus in forum New To JavaReplies: 5Last Post: 07-27-2009, 10:02 PM -
drawing an image to an offscreen image
By hunterbdb in forum Java 2DReplies: 9Last Post: 10-30-2008, 06:17 PM -
Problem Setting offscreen background
By D34N0 in forum Java AppletsReplies: 1Last Post: 07-14-2007, 11:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks