Results 1 to 14 of 14
  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Question Double buffering graphics problem

    Hey, I'm new to using java's graphics and am just trying to make a simple frame that displays a green rectangle on a black background that you can move around with the wasd keys.

    I have been using the double buffering tutorial here: Java:Tutorials:Double Buffering - GPWiki

    Here is my code:
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class graphics extends JFrame {
    
    	
    	public graphics() {
    		super.setTitle("");
    		this.setSize(600, 400);
    		this.setUndecorated(true);
    		this.setVisible(true);
    		this.createBufferStrategy(2);
    		gameLoop();
    	}
    
    	public void drawStuff() {
    		BufferStrategy b = this.getBufferStrategy();
    		Graphics g = null;
    		
    		try {
    			g = b.getDrawGraphics();
    			
    			g2d.draw(g); // Error here: g2d cannot be resolved
    		}
    		finally {
    			g.dispose();
    		}
    		b.show();
    		Toolkit.getDefaultToolkit().sync();
    	}
    
    	public void drawGraphics(Graphics g) {
    
    		Graphics2D g2d = (Graphics2D)g;
    		
    		g2d.setBackground(Color.black);
    		g2d.setColor(Color.blue);
    		Rectangle2D.Float bG = new Rectangle2D.Float(0F, 0F, (float)getSize().width, (float)getSize().height);
    		g2d.fill(bG);
    	}
    	
    	public void gameLoop() {
    		while (true) {
    			drawStuff();
    		}
    	}
    	
    	public static void main(String[] args) {
    		graphics g = new graphics();
    
    	}
    }
    My error is at the line: g2d.draw(g);
    I understand why I'm getting the 'cannot be resolved error' but I'm not sure how to declare my graphics outside that method. Also not sure if graphics2D is the right thing to be using?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Either declare g2d as an instance variable or pass it as a parameter.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default

    What about defining it like you do here:

    Graphics2D g2d = (Graphics2D)g;

  4. #4
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Thanks for the replies! How can I make Graphics g an instance variable? I tried Graphics g = new Graphics(); but that didn't work and I can't call my method without having that as an instance variable. The rest seems to be working though.

  5. #5
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok never mind I managed to get that working. But I am now getting a different error. Here is my code now:

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class graphics extends JFrame {
    
    
    	public graphics() {
    		super.setTitle("");
    		this.setSize(600, 400);
    		this.setUndecorated(true);
    		this.setVisible(true);
    		this.createBufferStrategy(2);
    		gameLoop();
    	}
    
    	public void drawStuff(Graphics2D g2d) {
    		BufferStrategy b = this.getBufferStrategy();
    		Graphics g = null;
    		
    		try {
    			g = b.getDrawGraphics();
    			
    			g2d.draw((Shape) g); // This line is giving errors
    		
    		}
    		finally {
    			g.dispose();
    		}
    		b.show();
    		Toolkit.getDefaultToolkit().sync();
    	}
    
    	public void paint(Graphics g) {
    
    		Graphics2D g2d = (Graphics2D)g;
    		
    		g2d.setBackground(Color.black);
    		g2d.setColor(Color.blue);
    		Rectangle2D.Float bG = new Rectangle2D.Float(0F, 0F, (float)getSize().width, (float)getSize().height);
    		g2d.fill(bG);
    		drawStuff(g2d);
    	}
    	
    	public void gameLoop() {
    		while (true) {
    			repaint();
    		}
    	}
    	
    	public static void main(String[] args) {
    		graphics g = new graphics();
    
    	}
    }
    This code compiles but when I run it it says "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.java2d.SunGraphics2D cannot be cast to java.awt.Shape"
    I'm not sure how to get it to work if I don't cast it to shape?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default

    How can I make Graphics g an instance variable
    Are you referring to a Graphics object for a BufferedImage? It has a method the will return a Graphics object for drawing on the BufferedImage.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.java2d.SunGraphics2D cannot be cast to java.awt.Shape"
    What line of code causes the error?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default

    Java Code:
    g2d.draw((Shape) g)
    What do you expect to be drawn for a Graphics object? It is not a Shape!

    Where is the Shape object you want to draw?

  9. #9
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    What do you expect to be drawn for a Graphics object? It is not a Shape!
    Sorry, that was just one of elcipse's suggested fixes and I don't fully understand (obviously) what I'm doing.
    What I had originally was just g2d.draw(g); but that says "The method draw(Shape) in the type Graphics2D is not applicable for the arguments (Graphics)"
    If I leave that line out completely it runs the 'gameLoop' once and then gives me a null pointer exception at "g.dispose()" and "drawStuff(g2d);"
    Last edited by Guy; 08-30-2011 at 08:44 PM. Reason: Made a mistake

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default

    a null pointer exception at g = b.getDrawGraphics();
    Does b have a valid value or is it null?

    Can you explain what the code is supposed to do?
    And also explain what the steps you are taking in the code to do it?

  11. #11
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Sorry I made a mistake, the null pointer exceptions are at "g.dispose()" and "drawStuff(g2d);"

    I'm just trying to make a very simple code where a blue rectangle is moved around with the wasd keys. In the code at the moment I am just trying to set up the graphics so that there is a loop the repaints the screen (using double buffering). I my code I have made a JFrame which is my game window and then in the constructor i'm calling gameLoop() which is basically a loop that will repeat until the game is over. In it, repaint() is called. Here is the paint() method:
    Java Code:
    public void paint(Graphics g) {
    		Graphics2D g2d = (Graphics2D)g;
    		
    		g2d.setColor(Color.blue);
    		Rectangle2D.Float bG = new Rectangle2D.Float(0F, 0F, 100F, 100F);
    		g2d.fill(bG);
    		drawStuff(g2d);
    	}
    The drawStuff method deals with the buffering and I just followed the tutorial but I don't really understand it (busy reading more about it at the moment but I'm a bit confused)

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default

    What is the drawStuff method supposed to do?

    You get a Graphics object but never use it?
    Where is the Shape that is to be drawn?

  13. #13
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    The paint() method is meant to be where I actually create the image and then that calls the drawStuff method which is meant to take the rectangle drawn in paint() and add it to the buffer and display it. Sorry for my ignorance, I think theres too much I don't understand like what the Graphics object is, what Graphics2D is, whether I'm meant to be adding the graphics object to the buffer or the actual rectangle I created on the graphics2D. So I guess I should just continue reading tutorials until I start to understand
    Thank you for your time!

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default

    I agree, your coding has gotten too far ahead of your knowledge.
    Time to do some more reading.

Similar Threads

  1. Double buffering problem
    By Perakp in forum New To Java
    Replies: 1
    Last Post: 08-11-2011, 08:18 PM
  2. Double Buffering
    By Macpwnage in forum Java 2D
    Replies: 8
    Last Post: 07-18-2011, 09:17 PM
  3. Double Buffering Graphics in a Frame
    By Atriamax in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 02:25 AM
  4. Double Buffering
    By dunff2k in forum SWT / JFace
    Replies: 1
    Last Post: 11-14-2008, 03:26 PM
  5. Double Buffering problem
    By aprenz in forum Java Applets
    Replies: 0
    Last Post: 05-28-2008, 04:26 AM

Tags for this Thread

Posting Permissions

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