Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.util.Random;
  4.  
  5. public class DrawingPolygons {
  6. public static void main(String[] args) {
  7. DrawingPanel panel = new DrawingPanel(401,401);
  8. panel.setBackground(Color.white);
  9. Graphics2D g = panel.getGraphics();
  10. Random randy = new Random();
  11.  
  12. //fillRegularPolygon(g, 200, 200, 150, randy.nextInt(10)+3);
  13. fillStar(g,200,200,200,7,0.1);
  14. }
  15. public static void fillRegularPolygon(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints){
  16. int[] xCoords = new int[nPoints];
  17. int[] yCoords = new int[nPoints];
  18.  
  19. double angle = 0;
  20.  
  21. for (int i = 0; i < nPoints; i++){
  22. xCoords[i] = (int) (ctrX + radius * Math.cos(Math.toRadians(angle)));
  23. yCoords[i] = (int) (ctrX + radius * Math.sin(Math.toRadians(angle)));
  24. angle += 360/nPoints;
  25. }
  26. g.setColor(Color.BLACK);
  27. g.fillPolygon(xCoords, yCoords, nPoints);
  28. }
  29. public static void fillStar(Graphics2D g,int ctrX,int ctrY,int radius,int nPoints,double spikes){
  30. int[] xCoords = new int[nPoints];
  31. int[] yCoords = new int[nPoints];
  32. double inner = radius*(1.0 - spikes);
  33. int angle = 360/nPoints;
  34. int index = 0;
  35.  
  36. for (int i = 0; i < 360; i += angle){
  37. xCoords[index] = (int) (ctrX + radius * Math.cos(Math.toRadians(i-90)));
  38. yCoords[index] = (int) (ctrY + radius * Math.sin(Math.toRadians(i-90)));
  39. index++;
  40. xCoords[index] = (int) (ctrX + inner * Math.cos(Math.toRadians(i-90)+(angle/2)));
  41. yCoords[index] = (int) (ctrY + inner * Math.sin(Math.toRadians(i-90)+(angle/2)));
  42. index++;
  43. }
  44. g.setColor(Color.BLACK);
  45. g.fillPolygon(xCoords, yCoords, nPoints*2);
  46. }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement