Docsity

CS251 Intermediate Programming Lab 8/Homework 4: Creating a Polygon Drawing Application - , Lab Reports of Computer Science

Partial preview of the text

Download CS251 Intermediate Programming Lab 8/Homework 4: Creating a Polygon Drawing Application - and more Lab Reports Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Lab 8/Homework 4

GUI Omnibus

• Midterm Wednesday.• Homework 3 due today.

To draw an arbitrary regular polygon:

circle, evenly spaced.The vertex points lie along the circumscribed

Each point is 2

(^) n (^) radians from the next point

(where

n (^) is the number of polygon sides). This

makes it easy to write a

(^) for

(^) loop to get the

2 vertices in polar coordinates (loop from 0 to  ).

coordinates (r,(from Trigonometry) To translate from polar

) to rectangular (x, y):

(^) x = Math.cos(

) * r

(^) y = Math.sin(

) * r

The

(^) x , y pairs will be relative to the center, so

you’ll need to add the

x , y (^) offsets from the text

fi elds, then plug each pair into a

GeneralPath

r

Your class will need model fields for Details:

centerX

centerY

sides

radius

fillColor

, and

outlineColor

to pass data from the listeners to the

paintComponent(Graphics)

method.

then callWhen “Draw” is clicked, copy all text fields into member fields of your class,

repaint()

on the drawing component. To convert the

String

results

s of

JTextField.getText()

to an

int

, call

Integer.parseInt(s)

new color containing the user-chosen value. The (The color picker starts from the color passed as an argument and returns a Outline Color", oldColor); Color newColor = JColorChooser.showDialog(button, "Choose To display a color picker, call something like:

button

argument is the

JButton

that caused the

JColorChooser

to be shown.

JColorChooser

is in

the

javax.swing

package. Make the appropriate changes to the above in the

case of the fill color button.)