SHARE
TWEET

Nick Dunn

a guest Feb 2nd, 2010 350 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 RotatedCircleWithBoxes 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.     private static final int BOX_SIZE = 5;
  20.  
  21.     public RotatedCircleWithBoxes() {
  22.         super();
  23.         setPreferredSize(new Dimension(SIZE, SIZE));
  24.     }
  25.  
  26.     /**
  27.      * Draw a series of dots in a circular pattern
  28.      * @param g
  29.      */
  30.     @Override
  31.     public void paintComponent(Graphics g) {
  32.         Graphics2D g2 = (Graphics2D) g;
  33.         // Don't forget to call the super method
  34.         super.paintComponent(g);
  35.  
  36.         int radius = getWidth()/3;
  37.  
  38.         // Translate the origin to the center of window
  39.         g2.translate(getWidth() /2, getHeight() /2);
  40.         for (int i = 0; i < NUM_DOTS; i++) {
  41.             g2.rotate(RADIANS_PER_DOT);
  42.             // We have rotated about the origin; draw a ray out along x axis
  43.             // of new coordinate system
  44.             g2.fillRect(radius, 0, BOX_SIZE, BOX_SIZE);
  45.  
  46.         }
  47.  
  48.     }
  49.  
  50.     public static void main(String[] args) {
  51.         JFrame frame = new JFrame("Rotated circle");
  52.         frame.add(new RotatedCircleWithBoxes());
  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