Lab_1_2_StartUp/SelectShape.java
Lab_1_2_StartUp/SelectShape.java
// Lab 1 Question 2 StartUp file
import
javax
.
swing
.
JFrame
;
public
class
SelectShape
{
public
static
void
main
(
String
args
[])
{
SelectShapeJFrame
selectShapeJFrame
=
new
SelectShapeJFrame
();
selectShapeJFrame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
selectShapeJFrame
.
setSize
(
420
,
470
);
// set frame size
selectShapeJFrame
.
setVisible
(
true
);
// display frame
}
}
// end class SelectShape
/**************************************************************************
* (C) Copyright 1992-2018 by Deitel & Associates, Inc. and *
* Prentice Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
Lab_1_2_StartUp/SelectShapeJFrame.java
Lab_1_2_StartUp/SelectShapeJFrame.java
// Lab 1 Question 2 StartUp file
// Draw a shape 20 times in random positions
import
java
.
awt
.
BorderLayout
;
import
java
.
awt
.
event
.
ItemListener
;
import
java
.
awt
.
event
.
ItemEvent
;
import
javax
.
swing
.
JComboBox
;
import
javax
.
swing
.
JFrame
;
public
class
SelectShapeJFrame
extends
JFrame
{
private
ShapeOption
shapeOptions
[]
=
{
ShapeOption
.
OVAL
,
ShapeOption
.
RECTANGLE
};
private
ShapeOption
shape
=
ShapeOption
.
OVAL
;
private
JComboBox
<
ShapeOption
>
choiceJComboBox
;
private
SelectShapeJPanel
selectShapeJPanel
;
public
SelectShapeJFrame
()
{
super
(
"Selecting Shapes"
);
selectShapeJPanel
=
new
SelectShapeJPanel
();
choiceJComboBox
=
new
JComboBox
<
ShapeOption
>
(
shapeOptions
);
choiceJComboBox
.
addItemListener
(
new
ItemListener
()
// anonymous inner class
{
public
void
itemStateChanged
(
ItemEvent
e
)
{
selectShapeJPanel
.
setShape
(
(
ShapeOption
)
choiceJComboBox
.
getSelectedItem
());
repaint
();
}
}
);
add
(
selectShapeJPanel
,
BorderLayout
.
CENTER
);
add
(
choiceJComboBox
,
BorderLayout
.
SOUTH
);
}
}
// end class SelectShapeJFrame
Lab_1_2_StartUp/SelectShapeJPanel.java
Lab_1_2_StartUp/SelectShapeJPanel.java
// Lab 1 Question 2 StartUp file
// Draw a shape 20 times in random positions
import
java
.
awt
.
Color
;
import
java
.
awt
.
Graphics
;
import
java
.
util
.
Random
;
import
javax
.
swing
.
JPanel
;
public
class
SelectShapeJPanel
extends
JPanel
{
private
final
int
SIZE
=
400
;
private
ShapeOption
shape
=
ShapeOption
.
OVAL
;
// draw the new shape in random locations 20 times
public
void
paintComponent
(
Graphics
g
)
{
super
.
paintComponent
(
g
);
Random
random
=
new
Random
();
// get random-number generator
for
(
int
count
=
1
;
count
<=
20
;
count
++
)
{
// add 10 and 25 to prevent drawing over edge
int
x
=
(
int
)
(
random
.
nextFloat
()
*
SIZE
)
+
10
;
int
y
=
(
int
)
(
random
.
nextFloat
()
*
SIZE
)
+
25
;
int
width
=
(
int
)
(
random
.
nextFloat
()
*
(
SIZE
-
x
));
int
height
=
(
int
)
(
random
.
nextFloat
()
*
(
SIZE
-
y
));
g
.
setColor
(
Color
.
BLUE
);
// draw the appropriate shape
switch
(
shape
)
{
case
OVAL
:
g
.
drawOval
(
x
,
y
,
width
,
height
);
break
;
case
RECTANGLE
:
g
.
drawRect
(
x
,
y
,
width
,
height
);
break
;
}
}
}
// set new shape
public
void
setShape
(
ShapeOption
preference
)
{
shape
=
preference
;
}
}
// end class SelectShapeJPanel
Lab_1_2_StartUp/ShapeOption.java
Lab_1_2_StartUp/ShapeOption.java
// Lab 1 Question 2 StartUp file
// Defines an enum type for the program's shape options.
public
enum
ShapeOption
{
// declare contents of enum type
OVAL
(
1
),
RECTANGLE
(
2
);
private
final
int
value
;
// current shape
ShapeOption
(
int
valueOption
)
{
value
=
valueOption
;
}
public
int
getValue
()
{
return
value
;
}
}
// end enum ShapeOption