Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Create a Graphics2D object.  RSS feed

 
Lisa Carter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

i need to create a Graphics2D object but I can't. So I decided to extend the class and just create a default constructor that will allow me to instantiate the class. The problem is that now the object is null. How do I get around this. I hope that I'm asking the question correctly as I'm very new to swing, I usually create JSP's so all of this is new to me.
 
Brandon Broschinsky
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to extend JComponent or JPanel, or another component, and then override the paintComponent method.

public class MyPanel extends JPanel{

public void paintComponent(Graphics g) {
//here is your graphics object
}

}
 
Lisa Carter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a new class that does as you've said and I'm able to create the object but it is null. I need to object to pass to a method in another class and the object can't be null;

Basically I'm writing a test case for a class. I'm trying to crate a Graphics2D object to pass to one of the methods. The test fails because the graphics object is null.
 
Romain Guy
author
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on why you need a Graphics2D object. One solution is to create a BufferedImage and get its Graphics2D:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.getGraphics();
// have fun with g2
 
Chet Haase
author
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa,

There's a fundamental problem in what you're trying to do:
Graphics (or Graphics2D) objects cannot be created from scratch - they are only meaningful as the drawing context for a destination "surface" (either a component or an image).

How you get the Graphics object you want depends on what you want to do. If you want to draw to an image, then create the image and get the Graphics object from it, as Romain suggested. If you want to draw to a Swing component, then create a component, override paintComponent(Graphics g), and Swing will give you a Graphics object that represents the drawing context for that component when the component needs to be displayed.

For the latter example, here's an example:

public MyComponent extends JComponent {
protected void paintComponent(Graphics g) {
// any rendering will happen to the component's area
}
}
// Now, add the above component to a Swing window (JFrame), make the window visible (setVisible(true)) and paintComponent will be called.

Check out the demos on the book's site - there are a lot of simpler demos
for the early chapters (like chapters 2 and 3) that show very simple applications that get and use Graphics objects.

Chet.
 
Lisa Carter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks I will give this a shot. My way I kept an error saying the object was null.
 
Lisa Carter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every one thank you for your help. I was able to use the BufferedImage to create my Graphics2D object successfully. Thank you also for your explanations. This will help me to get back up to speed. I've not looked at Swing in over 5 years. Thanks again everyones help.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeh, Chet has hit the nail on the head...
Theres basically two reasons for a Graphics2D object to be used.

1. To draw into a component like a JPanel, using the paintComponent(..) method.
This has a Graphics object as parameter that you can use.

2. Create a BufferedImage object, call getGraphics(), then you can draw into the image. This is very useful for making images in java, which you can then write to some location on hard drive.

I noticed recently that most of the Graphics2D methods are abstract, thats because they are implemented in a special private class called SunGraphics2D, which isn't really accessible for users since it does the speical stuff like drawing squares etc. etc, which is different to say the String class which uses normal java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!