Graphics Demo 1

GRAPHICS! Kids, we're going to learn how to draw some stuff on the screen in a Java application. Download the following code file and get it to compile.

Files Needed

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

public class GraphicsDemo1 extends Canvas
    public void paint( Graphics g )
        g.drawRect(50,20,100,200);  // draw a rectangle
        g.fillOval(160,20,100,200); // draw a filled-in oval
        g.fillRect(200,400,200,20); // a filled-in rectangle
        g.drawString("Graphics are pretty neat.", 500, 100);
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        g.drawString("The first letter of this string is at (" + x + "," + y + ")", x, y);

    public static void main( String[] args )
        // You can change the title or size here if you want.
        JFrame win = new JFrame("GraphicsDemo1");
        GraphicsDemo1 canvas = new GraphicsDemo1();
        win.add( canvas );

What You Should Do on Your Own

Assignments turned in without these things will receive half credit or less. Answer any questions in comments at the top of

  1. How big is the window that appears? How many pixels wide? How many pixels tall?
  2. In the call to the function g.drawRect(), there are four numbers. What do they mean? Try changing them to figure it out.
  3. What about the call to fillOval()? What do the four numbers mean here?
  4. What are the two numbers in the call to drawString()?
  5. What happens when two objects overlap? Which one is drawn on top?
  6. Add a red, filled-in square somewhere in the lower-right of the canvas.

©2013 Graham Mitchell

This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Creative Commons License