Graphics Programming Exercise
This exercise is in lieu of a Graphics lab. There is nothing to hand in. Work through the steps and ask your teacher or another student if you need help understanding something.
Remember the "Three Steps to GUI Programming" that we covered last week:
- Make a background window
- Put Components into the window
- Add Listeners to the Components
The Graphics experiments we are doing here allow us to create our own custom UI widgets, which would be used in Step 2 above.
Some examples
Here are two examples of basic custom UIs. Copy each to a local file and run them. Change at least two variables or functions in each to see how they work.
RectWindow.java demonstrates how the component figures out the size of its own window. Run this program and resize the window. Make some changes to figure out how it works. Try 2-3 of these:
- Change the size of the rectangle
- Change the thickness of the rectangle
- Change the spacing between the rectangle and the edge of the panel
- Make the rectangle and ellipse different colors
- Check out different drawing options in Java's Graphics class. Try drawing the rectangle with "fill3DRect()".
Ripple.java demonstrates how to set up animation. Make some changes to figure out how it works. Try 2-3 of these:
- Change the speed of the ripple
- Fill in the circle with a solid color
- Make a ripple with concentric circles
- Animate the ripple's size to make it grows and shrink
- Animate the ripple's color so that it changes over time
Next Steps
The best way to learn about code is by playing with it. Pick one of the following tasks and implement it as your own custom UI widget. Use the above examples as a starting point.
- Create a custom PushButton that stays pushed in when pressed. (i.e. the button goes from convex to concave) Draw the button yourself using rectangles and polygons from the Graphics class.
- Create a custom ImageButton that displays one image when on, and another when off.
- Create a custon ToggleSwitch that displays a picture of a toggle switch in the up or down position. Instead of pressing it, you can require the use to drag the mouse up or down to set it.
- Draw some String Art.
- Implement an object that lets the user draw on it with a mouse.
- Use the Graphics class to draw a recognizable object or scene.