Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.GeneralPath;
  3. import javax.swing.*;
  4.  
  5. public class DrawCircle extends JPanel
  6. {
  7. Point[] points;
  8. GeneralPath circle;
  9. final int INC = 5;
  10.  
  11. public DrawCircle()
  12. {
  13. initPoints();
  14.  
  15. initCircle();
  16. }
  17.  
  18. private void initPoints()
  19. {
  20. int numberOfPoints = 360/INC;
  21. points = new Point[numberOfPoints];
  22. double cx = 200.0;
  23. double cy = 200.0;
  24. double r = 100.0;
  25. // Dimension variables
  26. int count = 0;
  27. for(int theta = 0; theta < 360; theta+=INC)
  28. {
  29. int x = (int)(cx + r * Math.cos(Math.toRadians(theta)));
  30. int y = (int)(cy + r * Math.sin(Math.toRadians(theta)));
  31. points[count++] = new Point(x, y);
  32. }
  33. }
  34.  
  35. private void initCircle()
  36. {
  37. circle = new GeneralPath();
  38. for(int j = 0; j < points.length; j++)
  39. {
  40. if(j == 0)
  41. circle.moveTo(points[j].x, points[j].y);
  42. else
  43. circle.lineTo(points[j].x, points[j].y);
  44. }
  45. circle.closePath();
  46. }
  47.  
  48. protected void paintComponent(Graphics g)
  49. {
  50. // fill and color
  51. super.paintComponent(g);
  52. Graphics2D g2 = (Graphics2D)g;
  53. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  54. RenderingHints.VALUE_ANTIALIAS_ON);
  55. g2.setPaint(Color.red);
  56. g2.fill(circle);
  57. g2.setPaint(Color.red);
  58. Point p1 = points[0];
  59. for(int j = 1; j <= points.length; j++)
  60. {
  61. Point p2 = points[j % points.length];
  62. g2.drawLine(p1.x, p1.y, p2.x, p2.y);
  63. // Line coordinates
  64. p1 = p2;
  65. }
  66. }
  67.  
  68. public static void main(String[] args)
  69. {
  70. //Main functions
  71. JFrame f = new JFrame();
  72. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  73. f.setContentPane(new DrawCircle());
  74. f.setSize(400,400);
  75. // Frame
  76. f.setLocation(200,200);
  77. // Center
  78. f.setVisible(true);
  79. }
  80. }
  81.  
  82. public class StopWatch {
  83. private final long before;
  84.  
  85. StopWatch() {
  86. before = System.currentTimeMillis();
  87. //before = System.nanoTime();
  88. }
  89.  
  90. public long elapsedTime() {
  91. long after = System.currentTimeMillis();
  92. //long after = System.nanoTime();
  93. return after - before;
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement