Write a Java application that draws a pattern in the output window that is drawn
with a for
loop, and is drawn in successive stages based on the
number of times the user has clicked on a button on screen. For an example
of how your program should work,
click here.
Create an application called PA5
(no
spaces in the name, so PA5,
not PA 5). This program will have only
two class definitions: the MUFrame
class and the MUPanel
class.
The GUI
for the program will have two JButton
s and a JLabel
, the behavior
of which will be defined below.
Start with the three files that you extracted from the MUFrameWithGUIDesigner.zip file as a starting point for this program. Use the Designer to place two Buttons in the bottom corners of the graphical output screen in MUFrame.java, and a Label in the bottom-center of the screen. Rename the buttons and label so that they have meaningful names, change the text of the bottom-left button to be "Increase Repetitions", change the text of the bottom-right button to be "Reset Drawing", and change the text of the label to be "Num. of Repetitions = 1".
You should declare a
private Integer
property named numberOfReps
in the MUPanel class.
In the constructor method of MUPanel
, store 1 as the initial
value of the numberOfReps
property.
You should have
a for
loop in the paintComponent
method of MUPanel that will draw one
or more shapes, and will be executed numberOfReps
times. You will need some
combination of g.setColor
, g.drawRect
,
g.fillRect
, g.drawOval
,
g.fillOval
, g.drawArc
and/or g.fillArc
method calls to draw
your assigned pattern. After the shapes are drawn inside the for
loop,
the values of the variables over
, down
, width
and/or
height
may need to be changed, based on how your assigned pattern changes.
Define three methods in the MUPanel class in addition to the constructor and paintComponent
:
increaseReps
→ accepts no parameters and returns nothing. This method should
increase the value of the numberOfReps
property by one. resetReps
→ accepts no parameters and returns nothing. This method should
store the value 1 in the numberOfReps
property. getNumberOfReps
→ accepts no parameters and returns an Integer
value, which is the value of the numberOfReps
property.
One of the buttons in the GUI should cause the value stored in numberOfReps
to be
increased by one by calling the appropriate method of the muPanel
object,
set the text of the label to "Num. of Repetitions = " + muPanel.getNumberOfReps()
and then muPanel.repaint()
should be called.
The other button should
cause the value of numberOfReps
to be reset to 1 by calling the appropriate method of
the muPanel
object,
set the text of the label to "Num. of Repetitions = " + muPanel.getNumberOfReps()
and then muPanel.repaint()
should be called.
Each student in the class has a unique pattern to work on — you MUST use the pattern that has been assigned to you for this project.
Convert your
CSC120\Java\PA5 folder into a .zip file named PA5.zip
and submit it to the PA5 dropbox
in D2L.
Evaluation Item | Possible points |
---|---|
CSC 120, PA 5, name, date, & description comments in MUPanel.java | 3 |
One private data member: an
Integer named numberOfReps or something similar. |
1 |
constructor sets numberOfReps to 1. |
1 |
for loop in the paintComponent method that draws
one or more shapes, and is executed numberOfReps times. |
3 |
A combination of g. methods used to draw your assigned pattern. | 4 |
After shapes are drawn inside for loop, over ,
down , width and/or height are
changed to make pattern change. |
6 |
One button causes numberOfReps to increase by one. Other
button causes numberOfReps to be reset to 1.
|
1 |
Pattern is correctly drawn, and matches the pattern assigned to the student. | 8 |
Organization and readability of the Java code, including appropriate indenting | 3 |
Total Points ====> | 30 |