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 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
- */
- public class RotatedCircle extends JComponent {
- 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();
- setPreferredSize(new Dimension(SIZE, SIZE));
- }
- /**
- * Draw a series of dots in a circular pattern
- * @param g
- */
- @Override
- public void paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D) 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) {
- JFrame frame = new JFrame("Rotated circle");
- frame.add(new RotatedCircle());
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement