Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.util.Random;
- public class DrawingPolygons {
- public static void main(String[] args) {
- DrawingPanel panel = new DrawingPanel(401,401);
- panel.setBackground(Color.white);
- Graphics2D g = panel.getGraphics();
- Random randy = new Random();
- //fillRegularPolygon(g, 200, 200, 150, randy.nextInt(10)+3);
- fillStar(g,200,200,200,7,0.1);
- }
- public static void fillRegularPolygon(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints){
- int[] xCoords = new int[nPoints];
- int[] yCoords = new int[nPoints];
- double angle = 0;
- for (int i = 0; i < nPoints; i++){
- xCoords[i] = (int) (ctrX + radius * Math.cos(Math.toRadians(angle)));
- yCoords[i] = (int) (ctrX + radius * Math.sin(Math.toRadians(angle)));
- angle += 360/nPoints;
- }
- g.setColor(Color.BLACK);
- g.fillPolygon(xCoords, yCoords, nPoints);
- }
- public static void fillStar(Graphics2D g,int ctrX,int ctrY,int radius,int nPoints,double spikes){
- int[] xCoords = new int[nPoints];
- int[] yCoords = new int[nPoints];
- double inner = radius*(1.0 - spikes);
- int angle = 360/nPoints;
- int index = 0;
- for (int i = 0; i < 360; i += angle){
- xCoords[index] = (int) (ctrX + radius * Math.cos(Math.toRadians(i-90)));
- yCoords[index] = (int) (ctrY + radius * Math.sin(Math.toRadians(i-90)));
- index++;
- xCoords[index] = (int) (ctrX + inner * Math.cos(Math.toRadians(i-90)+(angle/2)));
- yCoords[index] = (int) (ctrY + inner * Math.sin(Math.toRadians(i-90)+(angle/2)));
- index++;
- }
- g.setColor(Color.BLACK);
- g.fillPolygon(xCoords, yCoords, nPoints*2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement