Drawing lines, rectangles, and ovals

The following graphics primitives can be easily drawn with Java:

The above output is produced by the following Java code:

import javax.swing.*;
import java.awt.*;

public class Graph1 
{
    public static void main(String[] args) 
    {
	SwingApp app = new SwingApp(450, 300);    
	app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }
}

class SwingApp extends JFrame
{
    private final DrawPanel panel;		    // class variable
    public SwingApp( int width, int height)	    // class constructor
    {
	super();				    // call to super class constructor
	Container pane = super.getContentPane();    // create layout, set colors
	panel = new DrawPanel();		    // add GUI components
	panel.setBackground(Color.yellow);
	pane.add(panel);
	// add here more code for GUI components
	Toolkit toolkit = Toolkit.getDefaultToolkit();	// optionally position JFrame
	Dimension screenSize = toolkit.getScreenSize(); // in the middle of the screen 
	super.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2);	
	super.setTitle("Swing app");		    // set desired window title
	super.setSize(width, height);		    // set desired window size
	super.setVisible(true);	
    }
}

class DrawPanel extends JPanel			    // main window panel
{
    @Override
    public void paintComponent(Graphics g)
    {
	super.paintComponent(g);		    // must be the 1st line
	// add here more code for drawing on panel 
// setting background and foreground colors
	g.setColor(new Color(220, 220, 220));
	g.fillRect(0, 0, getWidth(), getHeight());
	g.setColor(Color.red);

// draw a line
	g.drawLine(10, 10, 200, 30);

// draw rectangle and filled rectangle
	g.drawRect(10, 35, 75, 35); 
	g.fillRect(95, 35, 75, 35);

// draw rectangles with rounded corners
	g.drawRoundRect(10, 90, 70, 70, 0, 0);
	g.drawRoundRect(90, 90, 70, 70, 10, 10);
	g.drawRoundRect(170, 90, 70, 70, 40, 40);
	g.drawRoundRect(250, 90, 70, 70, 70, 70);
	g.fillRoundRect(330, 90, 70, 70, 40, 40);
    
// draw an oval inscribed into a rectangle and a filled oval
	g.drawRect(10, 170, 100, 60);
	g.drawOval(10, 170, 100, 60);
	g.fillOval(130, 170, 100, 60);
    }
}