SHARE
TWEET

Nick Dunn

a guest Feb 2nd, 2010 754 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package circle;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import javax.swing.JComponent;
  6. import javax.swing.JFrame;
  7.  
  8. /**
  9.  * Simple example that shows a standard way of drawing, manually calculating
  10.  * x and y coordinates of the points of a circle.
  11.  * @author Nick
  12.  */
  13. public class StandardCircle extends JComponent {
  14.  
  15.     private static final int NUM_DOTS = 20;
  16.     private static final int SIZE = 200;
  17.  
  18.     public StandardCircle() {
  19.         super();
  20.         setPreferredSize(new Dimension(SIZE, SIZE));
  21.     }
  22.  
  23.     /**
  24.      * Draw a series of dots in a circular pattern
  25.      * @param g
  26.      */
  27.     @Override
  28.     public void paintComponent(Graphics g) {
  29.         // Don't forget to call the super method
  30.         super.paintComponent(g);
  31.  
  32.         int radius = getWidth() / 3;
  33.  
  34.         for (int i = 0; i < NUM_DOTS; i++) {
  35.             double theta = 2 * Math.PI * ((double) i / NUM_DOTS);
  36.             int x = (int) (radius * Math.cos(theta));
  37.             int y = (int) (radius * Math.sin(theta));
  38.  
  39.             // these x and y are relative to center of circle; currently origin
  40.             // is at upper left corner of window.  Add to x and y to
  41.             // translate.
  42.             x += getWidth() / 2;
  43.             y += getHeight() / 2;
  44.  
  45.             g.drawOval(x, y, 1, 1);
  46.         }
  47.  
  48.     }
  49.  
  50.     public static void main(String[] args) {
  51.         JFrame frame = new JFrame("Standard circle");
  52.         frame.add(new StandardCircle());
  53.         frame.pack();
  54.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.         frame.setVisible(true);
  56.     }
  57.  
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top