import java.applet.*; import java.awt.*; import java.awt.event.*; public class Kaleidoscope extends Applet implements ActionListener, Runnable { private volatile Thread tickThread = null; private boolean isRunning; private KaleidoscopeCanvas kaleidoscopeCanvas; private Checkbox rectangleCheckbox, squareCheckbox, ellipseCheckbox, circleCheckbox, triangleCheckbox; private Button startStopButton; public void init() { isRunning = false; setLayout(new BorderLayout()); Panel shapePanel = new Panel(); rectangleCheckbox = new Checkbox("Rectangle"); shapePanel.add(rectangleCheckbox); squareCheckbox = new Checkbox("Square"); shapePanel.add(squareCheckbox); ellipseCheckbox = new Checkbox("Ellipse"); shapePanel.add(ellipseCheckbox); circleCheckbox = new Checkbox("Circle"); shapePanel.add(circleCheckbox); triangleCheckbox = new Checkbox("Triangle"); shapePanel.add(triangleCheckbox); startStopButton = new Button("Start"); startStopButton.addActionListener(this); shapePanel.add(startStopButton); add(shapePanel, BorderLayout.NORTH); kaleidoscopeCanvas = new KaleidoscopeCanvas(); add(kaleidoscopeCanvas, BorderLayout.CENTER); } // start a thread : You should leave this method be public void start() { if ((isRunning) && (tickThread == null)) { tickThread = new Thread(this); tickThread.start(); } } // run a thread and update every X milliseconds public void run() { Thread myThread = Thread.currentThread(); while (tickThread == myThread) { // // THIS IS the point where you can generate random shapes, and pass each random shape to the canvas for painting. // A Kaleidoscope should ideally have 4 or 8 mirrored images of the same object. So, if you generate a circle at // position (100,100) in an 800x600 screen, then the same circle should also appear at position (700,100), // (100,500), and (700,500) // It is up to you how you would like to implement this functionality. You can make 4 calls here, and assume the // applet's preset size, e.g. 800x600. // When generating random shapes, make sure that you are only using shapes that have been selected in the UI! // kaleidoscopeCanvas.drawShape(); // // // try { // updates every 100 milliseconds Thread.sleep(100); } catch (InterruptedException e) { } } } // listen for Start/Stop action: You should leave this method be public void actionPerformed(ActionEvent e) { if (isRunning) { isRunning = false; tickThread = null; startStopButton.setLabel("Start"); } else { isRunning = true; startStopButton.setLabel("Stop"); start(); } } }