
Nick Dunn
By: a guest on
Feb 2nd, 2010 | syntax:
Java | size: 1.59 KB | hits: 285 | expires: Never
package circle;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
/**
* Simple example that shows a standard way of drawing, manually calculating
* x and y coordinates of the points of a circle.
* @author Nick
*/
private static final int NUM_DOTS = 20;
private static final int SIZE = 200;
public StandardCircle() {
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;
for (int i = 0; i < NUM_DOTS; i++) {
double theta
= 2
* Math.
PI * ((double) i
/ NUM_DOTS
);
int x
= (int) (radius
* Math.
cos(theta
));
int y
= (int) (radius
* Math.
sin(theta
));
// these x and y are relative to center of circle; currently origin
// is at upper left corner of window. Add to x and y to
// translate.
x += getWidth() / 2;
y += getHeight() / 2;
g.drawOval(x, y, 1, 1);
}
}
public static void main
(String[] args
) {
frame.add(new StandardCircle());
frame.pack();
frame.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
frame.setVisible(true);
}
}