1:import java.awt.*;
   2:import java.awt.event.*;
   3:
   4:/**
   5:  * Filename: RoseApplet.java
   6:  * Creator: John C. Linford (johnlinford@mail.weber.edu)
   7:  * Date Created: July 15, 2003
   8:  * 
   9:  * Description: Uses the polar equation "r = cos(n * x)" 
  10:  * (where n is the complexity) to generate "rose curves."
  11:  * The domain is restricted to [0, 2PI].
  12:  * 
  13:  * Copyright: This software is public domain.  Do what you like to it!
  14:  */
  15:public class RoseApplet extends java.applet.Applet {
  16:
  17:        //The complexity of the curve
  18:        private int complexity = 2;
  19:        //Button to draw the curve
  20:        private Button drawBtn;
  21:        //Text field in which to enter the complexity
  22:        private TextField petalsFld;
  23:        //Checkbox to let the user animate the drawing of the curve
  24:        private Checkbox animateBox;
  25:
  26:        /**
  27:          * Constructor.
  28:        */
  29:        public RoseApplet() {
  30:                setLayout(new BorderLayout());
  31:                setBackground(Color.black);
  32:                drawBtn = new Button("Draw it!");
  33:                petalsFld = new TextField("2");
  34:                animateBox = new Checkbox("Animate", false);
  35:                animateBox.setForeground(Color.white);
  36:                Panel p1 = new Panel(new GridLayout(1, 3, 10, 2));
  37:                Label l1 = new Label("Complexity:");
  38:                l1.setForeground(Color.white);
  39:                p1.add(l1);
  40:                p1.add(petalsFld);
  41:                p1.add(animateBox);
  42:                Panel p2 = new Panel(new BorderLayout());
  43:                p2.add(p1, BorderLayout.WEST);
  44:                p2.add(drawBtn, BorderLayout.EAST);
  45:                add(p2, BorderLayout.SOUTH);
  46:
  47:                drawBtn.addActionListener(
  48:                        new ActionListener() {
  49:                                public void actionPerformed(ActionEvent evt) {
  50:                                        drawGraph();
  51:                                }
  52:                        }
  53:                );
  54:
  55:                petalsFld.addKeyListener(
  56:                        new KeyAdapter() {
  57:                                public void keyPressed(KeyEvent evt) {
  58:                                        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
  59:                                                drawGraph();
  60:                                        }
  61:                                }
  62:                        }
  63:                );
  64:        }
  65:
  66:        /*
  67:         * Convience method to call to draw the graph.
  68:         * Called from action listeners on both the button and the text field.
  69:         */
  70:        private void drawGraph() {
  71:                try {
  72:                        petals = Integer.parseInt(petalsFld.getText());
  73:                        if (petals < 0) {
  74:                                petals = 1;
  75:                                petalsFld.setText("1");
  76:                        }
  77:                } catch (Exception err) {
  78:                        petalsFld.setText("1");
  79:                }
  80:                repaint();
  81:        }
  82:
  83:        /*
  84:         * Returns the x-cordinate of the polar point.  This is done by converting
  85:         * the polar equation to a parametric equation.
  86:         */
  87:        private double x(double t) {
  88:                return Math.cos(petals*t)*Math.cos(t);
  89:        }
  90:
  91:        /*
  92:         * Returns the y-cordinate of the polar point.  This is done by converting
  93:         * the polar equation to a parametric equation.
  94:         */
  95:        private double y(double t) {
  96:                return Math.cos(petals*t)*Math.sin(t);
  97:        }
  98:
  99:        /**
 100:          * Repaints the applet.
 101:          */
 102:        public void paint(Graphics g) {
 103:                g.setColor(Color.red);
 104:                for (double t = 0; t <= 2*Math.PI; t += 0.001) {
 105:                        g.fillRect((int)(x(t) * 100) + getSize().width / 2,
 106:                                   (int)(y(t) * 100) + getSize().height / 2, 1, 1);
 107:                        if (animateBox.getState()) {
 108:                                try {
 109:                                        Thread.sleep(1);
 110:                                } catch (Exception err) {}
 111:                        }
 112:                }
 113:        }
 114:}