Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- */
- public class StandardCircle extends JComponent {
- private static final int NUM_DOTS = 20;
- private static final int SIZE = 200;
- public StandardCircle() {
- super();
- setPreferredSize(new Dimension(SIZE, SIZE));
- }
- /**
- * 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) {
- JFrame frame = new JFrame("Standard circle");
- frame.add(new StandardCircle());
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement