Difference between Graphics and Graphics2D? - java

What is difference between Graphics and Graphics2D?
Whether Graphics2D is extend of Graphics?
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(25, 25, 20, 20); //use Graphics to paint rectangle
Graphics2D g2 =(Graphics2D)g;
g2.drawRect(0, 0, 20, 20); // use Graphics2D to paint rectangle
}

Graphics itself is an abstract class, therefore you cant create its instance. It only defines some interface and some functionality, so it can be extended by other class.
So even this Graphics g, which is used as parameter in paintComponent, is not only Graphics. The standard java library has only two extended class : DebugGraphics, Graphics2D, so the Graphics g you are using is Graphics2D instance stored in Graphics g.
If it is not, the line Graphics2D g2 =(Graphics2D)g; would end with an error.

Related

Offset brush when using graphics2d

Is it possible to offset the brush by a given amount when drawing in the paintComponent? The end result would be everything drawn offset by an X and Y value.
If you need to translate all the graphics then you can do:
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics g2 = g.create();
g2.translate(xValue, yValue);
// do custom painting
g2.dispose();
}

No Graphics2D.draw method in java?

I use NetBeans and the last version of JDK. For some reason there is no Graphics.draw() method, though java.awt.geom.Line2D and java.awt.Graphics2D are imported. How do I draw a Line2D.Double element?
Shape pLine;
private void playerDraw(){
Graphics g2 = getGraphics();
pLine = new Line2D.Double(px, py, Math.cos(angle)*10+px,Math.sin(angle)*10+py);
g2.drawRect(px-5, py-5, 10, 10);
g2.draw(pLine); //this doesn't compile(cannot find symbol)
}
Your main problem is that you're using a Graphics object as if it were a Graphics2D object, but it's not and as the Graphics class Java API entry will show you, the Graphics class does not have a draw method, while Graphics2D does. I think that you're missing a key line, something like:
Graphics g = getGraphics();
Graphics2D g2 = (Graphics2D) g; // the missing line
But having said that, you are using Graphics incorrectly as you should avoid getting it by calling getGraphics() on a Swing component since this gives you an unstable short-lived Graphics object whose use risks causing short-lived images or NullPointerExceptions, but rather you should do your drawing within the JComponent's paintComponent(Graphics g) method.
You missed to declare pLine variable in your Class:
Eg.
public class Example
{
public Line2D.Double pLine;
private void playerDraw(){
Graphics g2 = getGraphics();
pLine = new Line2D.Double(px, py,Math.cos(angle)*10+px,Math.sin(angle)*10+py);
g2.drawRect(px-5, py-5, 10, 10);
g2.draw(pLine); //this doesn't compile(cannot find symbol)
}
}

Getting the Graphics2D?

public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g; //
double r = 100; //the radius of the circle
//draw the circle
Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 2 * r, 2 * r);
g2.draw(circle);
This is part of a class within my program, my question lies in the
Graphics2D g2 = (Graphics2D)g;
Why must you include the "g" after the (Graphics2D), and what exactly does the "Graphics2D" Within the parenthesis mean, i am learning out of a book and neither of these were ever fully explained.
You are casting Graphics2D to the Graphics context g. Read more about casting here in Inheritance in the Casting section.
What this ultimately does is allot you use the available methods of Graphics2D with the Graphics context of the passed to the paintComponent method. Whitout the casting, you'd only be limited to the methods of the Graphics class
Graphics2D is a subclass of Graphics so by using Graphics2D you gain all the methods of Graphics while taking advantage of the methods in the Graphics2D class.
Side Notes
You shouldn't be override paint. Also if you are, you shouldn't be painting on top level containers like JApplet.
Instead paint on a JPanel or JComponent and override paintComponent instead of paint and call super.paintComponent. Then just add the JPanel to the parent container.
public DrawPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
}
}
See more at Custom Painting and Graphics2D
Yes, you must include the g.
When you enter your paint method you have an object g of type Graphics.
Then you are typecasting your Graphics object, g to a type of Graphics2D which is a type that extends Graphics.
You have to include the g so you have something to typecast. If you don't include object there you will get a compilation error because the statement isn't complete.
The reason you are typecasting g to a Graphics2D object is because you are telling the compiler, "This Graphics object is actually a Graphics2D object." that way you can perform functions that the Graphics2D Object has that the the Graphics object does not.
This stackoverflow answer explains casting variables in Java very well if you have more questions about that. And this stackoverflow answer explains why it is safe to cast from Graphics to Graphics2D

Is there any way of drawing a variable rectangle using the Graphics class

I have coded a simple Java game where there are two rectangles on the screen, one of the rectangles moves and the other stays still, the moving Rectangle moves with keyboard arrow input and can move either up, down, left or right. The problem I am having is drawing my rectangles on the screen, I mean I have the 2 rectangles set up with my variables as shown:
Rectangle rectOne = new Rectangle(shiftX, shiftY,90,90);
Rectangle rectTwo = new Rectangle(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
I have made a render method to draw the things on the screen which I want shown:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
The problem I am having is showing my rectangles on the screen by writing the code in the render method, I could do the following:
g.fillRect(x, y,90,90);
g.fillRect(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
Which makes 2 rectangles on the screen but I need the rectangles to be drawn using the Rectangle code written with the variables, I have been told this can be done using Graphics2D but I am wondering if there is a simpler way of just using the graphics function, if not could you please help me set this up?
Thank you in advance.
You can access attributes of the Rectangle instances easily:
g.fillRect(rectOne.getX(), rectOne.getY(), rectOne.getWidth(), rectOne.getHeight());
In any case mind that usually the Graphics object is a Graphics2D instance at runtime so this could work easily too:
Graphics2D g2d = (Graphics2D)g;
g2d.fill(rectOne);
Just use:
g.fillRect(myRect.getX(), myRect.getY(), myRect.getWidth(), myRect.getHeight());
where myRect is the rectangle you want to draw. You could even make a custom method drawRect(Graphics g, Rectangle myRect); if you have lots of rectangles to draw.
Using Graphics2D is not that difficult as well, as the provide Graphics object normally is a Graphics2D object:
Graphics2D g2d = (Graphics2D) g;
g2d.fill(myRect);

Java AffineTransform moving origin

I would like to move the origin from top left to bottom middle of the component?
I have been playing with AffineTransform class could not get it to work?
You will need the height and width of the component that you are trying to draw. Assuming you are in the paint(Graphics g) method the simplest way is:
paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.translate( component.getWidth()/2.0, component.getHeight()/2.0);
//...
}

Categories

Resources