/** * File: PsychBoxes2.java * * Draws boxes that seem to move inward. * This version allows starting and stopping via mouse clicks. * Reverses direction of movement when the cursor enters the applet. * * @author THC, converted to Java and JApplet by Scot Drysdale. */ import java.awt.*; // needed for the drawing code import javax.swing.*; // for the definition of JApplet import java.awt.event.*; import javax.swing.Timer; public class PsychBoxes2 extends JApplet { private static final long serialVersionUID = 1L; private Timer timer; // a Timer for periodic redrawing private int iter; // the current iteration number private boolean inward; // true if boxes head inward, false if they head outward public void init() { setSize(350, 350); final int DELAY = 100; // 100 milliseconds between redrawing the boxes iter = 0; inward = true; // Set up the listener for Timer events and mouse clicks. LocalPsychListener listener = new LocalPsychListener(); timer = new Timer(DELAY, listener); addMouseListener(listener); Container cp = getContentPane(); // Content pane holds components cp.add(new PsychBoxesCanvas()); // The canvas is the only component timer.start(); setVisible(true); // Makes the applet (and its components) visible } // Class to listen for Timer events and mouse clicks. private class LocalPsychListener implements ActionListener, MouseListener { // When a Timer event occurs, increment the iteration number and repaint // the window. public void actionPerformed(ActionEvent event) { iter = (iter + 1) % 4; repaint(); } // When the mouse is clicked, if the Timer is running then stop it. If // the Timer is stopped then start it. public void mouseClicked(MouseEvent event) { if (timer.isRunning()) timer.stop(); else timer.start(); } // Every time the mouse enters the applet window, change the direction // of the boxes. public void mouseEntered(MouseEvent event) { inward = !inward; } // We don't care about these events. public void mouseExited(MouseEvent event) {} public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} } /** * Create the drawing canvas to be added to the JApplet content panel */ private class PsychBoxesCanvas extends JPanel { private static final long serialVersionUID = 1L; /** * Paints the boxes on the graphics page. * Note that it can see iter from the outer class. * @param page the graphic object of the panel for drawing on */ public void paintComponent(Graphics page) { final int RECTS = 55; // draw this many concentric rectangles final int WIDTH = 1; // pen width final int DELTA = WIDTH + 2; // how far apart the Rects are final int BOUND = DELTA * RECTS; // used for determining Rect boundary super.paintComponent(page); for (int i = 1; i <= RECTS; i++) { // Rotate among four colors, depending on direction. int color; if (inward) color = (i + iter) % 4; else color = (RECTS + iter - i) % 4; // Avoid negative values switch (color) { case 0 : page.setColor(Color.yellow); break; case 1: page.setColor(Color.green); break; case 2: page.setColor(Color.blue); break; case 3: page.setColor(Color.magenta); break; } page.drawRect(BOUND - DELTA*i, BOUND - DELTA*i, // try drawOval() also! 2*DELTA*i, 2*DELTA*i); } } } }