1
public class Points extends JPanel {

  public void paintComponent(Graphics g) {
  super.paintComponent(g);

  Graphics2D g2d = (Graphics2D) g;

  g2d.drawLine(60, 20, 80, 90);
 }
}

I'm not really sure what's the Graphics2D g2d = (Graphics2D) g; supposed to do.

It's just a plain JPanel that's later added onto a JFrame.

It would be really helpfull if anyone could give me some advice as I'm stuck at this line of the code for a long time now.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2347921
  • 83
  • 1
  • 5
  • 12

5 Answers5

1

It's a problem of compatibility with older Java code.

Graphics2D, as explained in documentation, is a class that inherits from Graphics and provides some additional graphic features: in short Graphics2D is a more powerful Graphics.

Now, the method paintComponent(Graphics g) exists from before Graphics2D so even if with current Java the Graphics which is under the hood of a JPanel is a Graphics2D, the signature hasn't been changed to break existing code.

At runtime the g passed is a Graphics2D but you need to cast it so that you will be allowed to call more advanced operations on it.

Jack
  • 131,802
  • 30
  • 241
  • 343
1

The statement

Graphics2D g2d = (Graphics2D) g;

just casts the Graphics object to a Graphics2D. It's used to access the methods provided by Graphics2D. In this case it is unnecessary as Graphics also has a drawLine method so if you don't have a requirement for the more advanced methods such as rotate and translate, you can use

@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
   g.drawLine(60, 20, 80, 90);
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I didn't know that you can do this with objects at all. Im pretty new to programming and when I learned some of the basics I just jumped into coding, so theres a lot of things that I've skipped in the tutorial – user2347921 May 31 '13 at 04:40
  • And one last thing, you can cast an object only to a subclass, right ? – user2347921 May 31 '13 at 04:43
  • Not only to a subclass. In this case `Graphics2D` is a subclass of `Graphics` so you cast or more correctly downcast the reference `g`. But more generally if any Object implements an interface then you can cast to that interface also. You can cast provided the Object **is-a** type of the other object. – Reimeus May 31 '13 at 12:24
0

It casts the graphics context into a Graphics2D object. This is useful because Graphics2D allows for rotations, transformations, antialiasing, etc not possible with a normal Graphics object. All of the methods available in Graphics are still available to you when you use Graphics2D.

Zong
  • 6,160
  • 5
  • 32
  • 46
0

You are casting g as a Graphics2D so you can get at the advanced functionality in the Graphics2D class.

Jesse Craig
  • 560
  • 5
  • 18
0

That just casts your Graphics object into a Graphics2D object. Graphics2D has a lot of features that Graphics doesn't provide that are very useful for painting 2D graphics. Check out the documentation for Graphics2D here:

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

Also, according to this other question, that cast should always be safe to do.

Community
  • 1
  • 1
Shrike
  • 155
  • 9