teaching machines

CS 245 Lab 3 – Custom Drawing

February 7, 2014 by . Filed under cs245, labs, spring 2014.

First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab.

Work in pairs, where possible. Prefer working with someone that you did not work with last week. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.

Synopsis

In this lab, you will examine custom drawing with Java’s 2-D graphics library, slider widgets, and inheritance. You will want to consult the documentation for Java’s Graphics class. Feel free to base your solutions on the code we have written together in lecture.

Checkpoint 1

Person A types.

Create a subclass of JFrame with a JSlider in its north or south region. Make the frame the slider’s ChangeListener, which is similar to ActionListener. For this first checkpoint, simply print the slider’s current value to the console on a change.

Checkpoint 2

Person B types.

Place a subclass of JPanel in the frame’s center region. Make your extension an inner class of the frame class, so that it may access the frame’s instance variables and easily call the frame’s methods. (If you made it a separate class, you would need to explicitly pass the frame to the JPanel for the two to work together.)

In the panel’s paintComponent method, draw a stick figure or some other creation using the Graphics object. Incorporate the slider’s current value into the drawing routine in some way. That is, changing the slider causes the figure to change in some way. For example, the slider may be linked to the figure’s hands x-position, with changes cause the figure to wave its hand back and forth.

When the slider is changed, call repaint on the panel to force it to redraw its content with the new slider value.