Assignment #8
Due: Tues, July 29
Objective
This assignment will help you gain basic practice with class
Graphics
Task
Complete these two exercises, each of which involve drawing things
on a panel, from inside the paintComponent() method:
Filenames should be
- Circles.java
- Triangles.java
Exercise 1
Filename: Circles.java
Write an application that draws a series of eight concentric circles.
The circles should be separated by 10 pixels.
Notes and requirements
- Note that concentric circles have the same center point, but different
radii. (i.e. one circle is enclosed inside another, with the same
center)
- Cirles can easily be drawn using either method drawArc() or
with drawOval()
- Set the application up as a JPanel embedded inside of a JFrame. Make
the title bar of the JFrame read: ": Circles", where your own name
is on the title bar. My title bar would read "Bob Myers: Circles".
- The drawing of the circles should be done on a class that extends
JPanel -- see the chapter 12 examples for this setup
- Make the default starting size of the frame 400 x 400. You can make
your circles any size, as long as they all fit in the window, and the
smallest circle has at least a radius of 5 pixels, and each circle is
separated from the next by 10 pixels
- Here is a screenshot of what my
version looks like
- Hint: On code layout, the chapter 12 examples from Deitel (the
recommended reference text) illustrate the idea of doing a JPanel inside
of a JFrame. However, the textbook does each example in 2 files.
Instead, you should put your code in a single file. See these sample
layouts -- you may use either one of these if you like, to get you
started:
- Sample layout 1: This one is like
the ch12 examples, but just with both classes placed in the same
file.
- Sample layout 2: This one will
also work, but it does everything in one class. So the
main() method would still need to create the JFrame, an
instance of the panel (which is now the Circles class), and put the
panel into the frame. The paintComponent() method is still
where the drawing of the circles would be done
- Example: Recall the first example we looked at from the
textbook (Deitel):
Drawing example
- That example contained two different files, with two classes
- You could easily put both classes into one file, called
ShowColors.java, as long as you made the ShowColors
class the public class
- Alternatively, here's the same example merged into a single class,
with filename
ColorPanel.java.
This is an example of Sample Layout 2 above
Exercise 2:
Filename: Triangles.java
Write an application that creates 5 random triangles, each one filled
with random color.
Notes and requirements
Submitting:
Submit your files through the Blackboard submission link, as usual.
Make sure to upload both files before you submit:
Circles.java
Triangles.java