Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Graphics and Graphics2D?

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
}
like image 953
Samiey Mehdi Avatar asked Oct 13 '13 11:10

Samiey Mehdi


1 Answers

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.

like image 53
libik Avatar answered Sep 22 '22 12:09

libik