Results 1 to 14 of 14
- 08-30-2011, 07:33 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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:
My error is at the line: g2d.draw(g);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(); } }
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?
- 08-30-2011, 07:45 AM #2
Either declare g2d as an instance variable or pass it as a parameter.
- 08-30-2011, 02:07 PM #3
What about defining it like you do here:
Graphics2D g2d = (Graphics2D)g;
- 08-30-2011, 08:20 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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.
- 08-30-2011, 08:25 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Ok never mind I managed to get that working. But I am now getting a different error. Here is my code now:
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"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(); } }
I'm not sure how to get it to work if I don't cast it to shape?
- 08-30-2011, 08:28 PM #6
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.How can I make Graphics g an instance variable
- 08-30-2011, 08:29 PM #7
What line of code causes the error?Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.java2d.SunGraphics2D cannot be cast to java.awt.Shape"
- 08-30-2011, 08:32 PM #8
What do you expect to be drawn for a Graphics object? It is not a Shape!Java Code:g2d.draw((Shape) g)
Where is the Shape object you want to draw?
- 08-30-2011, 08:39 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Sorry, that was just one of elcipse's suggested fixes and I don't fully understand (obviously) what I'm doing.What do you expect to be drawn for a Graphics object? It is not a Shape!
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
- 08-30-2011, 08:43 PM #10
Does b have a valid value or is it null?a null pointer exception at g = b.getDrawGraphics();
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?
- 08-30-2011, 08:56 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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:
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)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); }
- 08-30-2011, 09:03 PM #12
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?
- 08-30-2011, 09:18 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
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!
- 08-30-2011, 09:22 PM #14
Similar Threads
-
Double buffering problem
By Perakp in forum New To JavaReplies: 1Last Post: 08-11-2011, 08:18 PM -
Double Buffering
By Macpwnage in forum Java 2DReplies: 8Last Post: 07-18-2011, 09:17 PM -
Double Buffering Graphics in a Frame
By Atriamax in forum New To JavaReplies: 6Last Post: 11-02-2009, 02:25 AM -
Double Buffering
By dunff2k in forum SWT / JFaceReplies: 1Last Post: 11-14-2008, 03:26 PM -
Double Buffering problem
By aprenz in forum Java AppletsReplies: 0Last Post: 05-28-2008, 04:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks