SHARE
TWEET

Nick Dunn

a guest Feb 2nd, 2010 340 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 java.awt.Graphics2D;
  6. import javax.swing.JComponent;
  7. import javax.swing.JFrame;
  8.  
  9. /**
  10.  * Alternate way of drawing previous example, using rotations rather than
  11.  * manually calculating x and y positions
  12.  * @author Nick
  13.  */
  14. public class RotatedCircle extends JComponent {
  15.  
  16.     private static final int NUM_DOTS = 20;
  17.     private static final int SIZE = 200;
  18.     private static final double RADIANS_PER_DOT = 2.0 * Math.PI / NUM_DOTS;
  19.  
  20.     public RotatedCircle() {
  21.         super();
  22.         setPreferredSize(new Dimension(SIZE, SIZE));
  23.     }
  24.  
  25.     /**
  26.      * Draw a series of dots in a circular pattern
  27.      * @param g
  28.      */
  29.     @Override
  30.     public void paintComponent(Graphics g) {
  31.         Graphics2D g2 = (Graphics2D) g;
  32.         // Don't forget to call the super method
  33.         super.paintComponent(g);
  34.  
  35.         int radius = getWidth()/3;
  36.  
  37.         // Translate the origin to the center of window
  38.         g2.translate(getWidth() /2, getHeight() /2);
  39.         for (int i = 0; i < NUM_DOTS; i++) {
  40.             g2.rotate(RADIANS_PER_DOT);
  41.             // We have rotated about the origin; draw a ray out along x axis
  42.             // of new coordinate system
  43.             g2.drawOval(radius, 0, 1, 1);
  44.  
  45.         }
  46.  
  47.     }
  48.  
  49.     public static void main(String[] args) {
  50.         JFrame frame = new JFrame("Rotated circle");
  51.         frame.add(new RotatedCircle());
  52.         frame.pack();
  53.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.         frame.setVisible(true);
  55.     }
  56.  
  57. }
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