COS 160: Star Sampler Please Help! Objectives Creating a method with parameters.
ID: 3707171 • Letter: C
Question
COS 160: Star Sampler
Please Help!
Objectives
Creating a method with parameters.
Part 1: Drawing Polygons
Java has a graphics method fillPolygon() for drawing any arbitrary polygon. It works by taking arrays of the X and Y coordinates for the points and then drawing a polygon whose border goes from point to point and then returns to the starting point. Its API description is:
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates.
This method draws the polygon defined by nPoints line segments, where the first nPoints - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ? i ? nPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
The area inside the polygon is defined using an even-odd fill rule, also known as the alternating rule.
Parameters:
xPoints - an array of x coordinates.
yPoints - an array of y coordinates.
nPoints - the total number of points.
See Also:
drawPolygon(int[], int[], int)
Test it by making a little test program that makes arrays of x and y coordinates and draws a polygon.
Part 2: Method to draw a filled regular polygon
You will now create a method to draw a regular polygon:
public static void fillRegularPolygon(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints)
It will calculate nPoints around a circle of radius centered at the point (ctrX, ctrY), and put them into two arrays of size nPoints to be used by fillPolygon(). The diagram above shows a pentagon (5 points) and an octagon (8 points). Remember the formulas for calculating points around a circle:
x = centerX + radius * cos(angle)
y = centerY + radius * sin(angle)
You should be using type double for angles, and remember to covert it to radians before calling sin() or cos().
Adjust your starting angle so that the first point is centered vertically straight up from the center. In main() draw a few different examples to make sure it works.
Turn in the code for your fillRegularPolygon() method and screenshots of 2 example results.
Part 3: Method to draw a filled star
Once you get the previous part working, copy and paste your method and rename it fillStar().
public static void fillStar (Graphics2D g, int ctrX, int ctrY, int radius, int points, double spikiness)
It will now have 1 extra parameter spikiness. The diagram shows that stars are polygons with points regularly spaced on 2 circles and, alternating between the outer and inner circles. The spikiness parameter determines the radius of the inner circle using the formula:
innerRadius = radius * (1.0 - spikiness)
The spikiness parameter can range from 0.0 to 1.0 with 0.0 being not spikey (like a soccer ball) and 1.0 being extremely spikey (like a sea urchin).
Draw a few different examples to make sure it works.
Turn in the code for your fillStar() method and screenshots of 2 example results.
Part 4: Star Sampler
Modify your main() method to draw lots of different stars. You can do whatever you like as long as there are at least 25 different star shapes with a varying number of points and varying spikiness. Have fun! For a quick and simple star sampler, I recommend that you just make 2 nested loops and draw a grid of stars with varying numbers of points by row and varying spikiness by column.
Turn in the main() method that draws the star sampler and a screenshot of the drawing panel.
Extra Credit: Super spikey
Something amazing and beautiful happens when the spikiness goes beyond 1.0. Experiment with this (try small values like 1.5) and draw one big example that you really like. Can you explain why it looks the way it does?
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates.
This method draws the polygon defined by nPoints line segments, where the first nPoints - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ? i ? nPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.
The area inside the polygon is defined using an even-odd fill rule, also known as the alternating rule.
Parameters:
xPoints - an array of x coordinates.
yPoints - an array of y coordinates.
nPoints - the total number of points.
See Also:
drawPolygon(int[], int[], int)
Explanation / Answer
fillPolygon(int[] xPoints, int[] yPoints, int nPoints) : Graphics « java.awt « Java by API Method1: You need to import the graphics package for the polygon view or for any graphical view of an polygon. Abstract Window Toolkit (AWT) is a set of application program interfaces ( API s) used by Java programmers to create graphical user interface ( GUI ) objects, such as buttons, scroll bars, and windows. AWT is part of the Java Foundation Classes ( JFC ) from Sun Microsystems, the company that originated Java. import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass extends JPanel { public void paint(Graphics g) { int xpoints[] = {25, 145, 25, 145, 25}; int ypoints[] = {25, 25, 145, 145, 25}; int npoints = 5; g.fillPolygon(xpoints, ypoints, npoints); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new MainClass()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200,200); frame.setVisible(true); } } Question 2: public void drawDragArea(Graphics2D g, Point dragPoint, Point beginPoint, Color c) { g.setColor(c); Polygon poly = new Polygon(); poly.addPoint((int) beginPoint.getX(), (int) beginPoint.getY()); poly.addPoint((int) beginPoint.getX(), (int) dragPoint.getY()); poly.addPoint((int) dragPoint.getX(), (int) dragPoint.getY()); poly.addPoint((int) dragPoint.getX(), (int) beginPoint.getY()); //Set the widths of the shape's outline Stroke oldStro = g.getStroke(); Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g.setStroke(stroke); g.drawPolygon(poly); g.setStroke(oldStro); //Set the trasparency of the iside of the rectangle Composite oldComp = g.getComposite(); Composite alphaComp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f); g.setComposite(alphaComp); g.fillPolygon(poly); g.setComposite(oldComp); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.