
Nick Dunn
By: a guest on Feb 2nd, 2010 | syntax:
Java | size: 1.57 KB | hits: 124 | expires: Never
package circle;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
/**
* Alternate way of drawing previous example, using rotations rather than
* manually calculating x and y positions
* @author Nick
*/
private static final int NUM_DOTS = 20;
private static final int SIZE = 200;
private static final double RADIANS_PER_DOT
= 2.0
* Math.
PI / NUM_DOTS
;
public RotatedCircle() {
super();
}
/**
* Draw a series of dots in a circular pattern
* @param g
*/
@Override
public void paintComponent
(Graphics g
) {
// Don't forget to call the super method
super.paintComponent(g);
int radius = getWidth()/3;
// Translate the origin to the center of window
g2.translate(getWidth() /2, getHeight() /2);
for (int i = 0; i < NUM_DOTS; i++) {
g2.rotate(RADIANS_PER_DOT);
// We have rotated about the origin; draw a ray out along x axis
// of new coordinate system
g2.drawOval(radius, 0, 1, 1);
}
}
public static void main
(String[] args
) {
frame.add(new RotatedCircle());
frame.pack();
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setVisible(true);
}
}