Java program to draw a Rectangle using Graphics2D class
KW.java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
class KW extends Frame 
{
    Rectangle2D rectangle=new Rectangle2D.Double(50,50,120,80);
    public void paint(Graphics g) 
    {
        Graphics2D ga=(Graphics2D)g;
        ga.draw(rectangle);
        ga.setPaint(Color.blue);
        ga.fill(rectangle);
        ga.fill(rectangle);
    }
    public static void main(String args[]) 
    {
        Frame frame=new KW();
        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
        frame.setSize(300,300);
        frame.setVisible(true);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW
Advertisement