Graphics Programming

Objectives
  1. Learn the basics of graphics programming in Java

  2. Help prepare for the next programming assignment

Getting Started

Create a project called L6 and import the following DrawingObjects-starter.jar.

Your project structure should now look like this:

L5
└── src
    ├── DrawInterface.java
    ├── DrawPrimitives.java
    └── UserInterface.java
Description

This lab provides the opportunity to take a closer look at graphical user interfaces. You are provided with a DrawPrimitives class. DrawPrimitives has a main method that instantiates and calls the UserInterface class to draw random graphics primitives.

The UserInterface class implements a provided Interface. You will be completing some methods in the UserInterface class by reading the provided javadoc and browsing the documentation for Java AWT and Swing.

This program is almost entirely graphical and does not read from the keyboard or write to the console. It includes working buttons and a dialog box. Here is an example of what the program draws, you will generate something similar assuming your code is correct:

Snapshot
  • The end result is a program that creates a window with three panels.

    • The top panel displays the total number of primitives rendered.

    • The middle panel is reserved for drawing graphics primitives.

    • The bottom panel has a LOAD, SAVE, and EXIT button (the load and save functions browse the file system but don’t really do anything).

Part One
  1. Finish the setupWindow method

Tip
Some of the methods you may need will be inherited, therefore you will also want to check out the parent classes. Here is the associated hierarchy:
JFrameHierarchy
  1. Finish the initializeGraphics method

    • Utilize methods in the Jpanel class and the Graphics2D class. Remember some of these methods may be inherited so look at parents classes, as well.

Your program should now be able to run.

Part Two

Complete the rest of the attribute and primitives methods in the UserInterface class, using graphics calls from the Graphics2D object.

Most are straightforward, however you will need to distinguish between filled and outlined primitive, which use different methods. The triangle also requires additional work to move coordinates into an array.

When complete the program will:

  • draw 8 random primitives onto the drawing surface every second.

  • keep track of the total count of primitives drawn.

  • clear the screen and reset the total count when the clear button is pressed.

  • loop continuously until the user quits.

Note that the other buttons are clickable and there is a dialog box.


Important
When you are finished show your program to the TA or helper to receive credit.