Results 1 to 11 of 11
  1. #1
    luis.depedro is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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)

    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) {}
    
    }
    But I get a NullPointer Exception on line:

    myGraphic=offscreen.getGraphics();

    What's wrong?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: offscreen.getGraphics() error

    Another AWT problem.
    Is there some bogus tutorial out there that focuses on AWT or something?

  4. #4
    luis.depedro is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: offscreen.getGraphics() error

    When I use JFrame and paintComponent, the rectangle doesn't even appear

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: offscreen.getGraphics() error

    Please post the code with the problem.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: offscreen.getGraphics() error

    Quote Originally Posted by luis.depedro View Post
    When I use JFrame and paintComponent, the rectangle doesn't even appear
    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)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    luis.depedro is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: offscreen.getGraphics() error

    A Jpane inside a Jframe? A canvas?
    Neither are Swing classes.
    Try a JPanel

  9. #9
    luis.depedro is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: offscreen.getGraphics() error

    Glad you got it working.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: offscreen.getGraphics() error

    Quote Originally Posted by luis.depedro View Post
    I had to add a setFocusable() so that it could listen the keboard.
    Swing is designed to use Key Bindings. Learning how they work and reimplementing without a KeyListener would be time well spent.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Question on getGraphics() in a JComponent
    By Shayke_ in forum Java 2D
    Replies: 2
    Last Post: 02-10-2011, 08:31 PM
  2. getGraphics() method problem
    By Aleksas in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2010, 12:34 AM
  3. JApplet.getGraphics() draws but JFrame doesn't
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 07-27-2009, 11:02 PM
  4. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 07:17 PM
  5. Problem Setting offscreen background
    By D34N0 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2007, 12:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •