//Meghan Hoke //sec 1 //1667 import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class BouncingBalls extends Applet implements MouseMotionListener { public static int width; public static int height; private Vector balls = new Vector(); Choice choice; TextArea echoArea; int clickNumber = 0; int enableMouse = 0; int lastX = 0; int lastY = 0; int curX = 0; int curY = 0; Button ballButton; public void init ( ) { setBackground(Color.white); height = getBounds().height; width = getBounds().width; addMouseListener(new MouseHandler()); addMouseMotionListener(this); Thread t = new BallThread(); t.start(); Button clearButton = new Button("Clear"); add(clearButton); clearButton.addActionListener(new ClearButtonHandler()); ballButton = new Button("New Ball"); add(ballButton); ballButton.addActionListener(new BallButtonHandler()); choice = new Choice(); choice.addItem("Orange"); choice.addItem("Green"); choice.addItem("Magenta"); choice.addItem("Blue"); add(choice); choice.addItemListener(new ChoiceHandler()); echoArea = new TextArea(2,40); echoArea.setEditable(false); add(echoArea); } // init public void start() { echoArea.setText("Add a new " + choice.getSelectedItem() + " ball: "); echoArea.append("\nClick on screen to set the ball."); } //start public void mouseMoved(MouseEvent e) { if(enableMouse == 1) { curX = e.getX(); curY = e.getY(); } else { curX = -5; curY = -5; lastX = -5; lastY = -5; } } public void mouseDragged(MouseEvent e) { if(enableMouse == 1) { curX = e.getX(); curY = e.getY(); } else { curX = -5; curY = -5; lastX = -5; lastY = -5; } } public void paint (Graphics g) { Enumeration e = balls.elements(); while(e.hasMoreElements()) { Ball b = (Ball)(e.nextElement()); b.paint(g); } if(enableMouse == 1 && clickNumber == 1) { g.setColor(Color.red); g.drawLine(curX,curY,lastX,lastY); } if(enableMouse == 1) { g.setColor(Color.lightGray); g.fillOval(lastX-5, lastY-5, 10, 10); g.fillOval(curX-5, curY-5, 10, 10); } } //paint private class Ball { int x, y; // current location int dx, dy; // speed int diameter = 10; Color color; boolean alive; public Ball (int ix, int iy, int idx, int idy, Color icolor) { x = ix; y = iy; dx = idx; dy = idy; color = icolor; alive = true; } public void move () { if (x < 0 || x >= BouncingBalls.width) dx = - dx; if (y < 0 || y >= BouncingBalls.height) dy = - dy; x += dx/10; y += dy/10; } public void paint (Graphics g) { g.setColor(color); g.fillOval(x, y, diameter, diameter); } } // Ball private class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { if(enableMouse == 1) { int x = e.getX(); int y = e.getY(); Color newColor = Color.black; if(choice.getSelectedItem().equals("Orange")) newColor = Color.orange; if(choice.getSelectedItem().equals("Green")) newColor = Color.green; if(choice.getSelectedItem().equals("Magenta")) newColor = Color.magenta; if(choice.getSelectedItem().equals("Blue")) newColor = Color.blue; clickNumber = clickNumber + 1; if(clickNumber == 1) { ballButton.setEnabled(false); echoArea.setText("Mouse clicked at " + x + ", " + y); echoArea.append("\nClick another point to set direction and speed"); lastX = x; lastY = y; curX = x; curY = y; } if(clickNumber == 2) { int dx = Math.abs(x-lastX); int dy = Math.abs(y-lastY); Ball b = new Ball(x,y,dx,dy,newColor); balls.addElement(b); enableMouse = 0; clickNumber = 0; ballButton.setEnabled(true); echoArea.setText("Make a color selection and click"); echoArea.append("\n'New Ball' to add another ball."); } } } // mousePressed } // MouseHandler private class BallThread extends Thread { public void run() { while (true) { Enumeration e = balls.elements(); while(e.hasMoreElements()){ Ball b = (Ball)(e.nextElement()); b.move(); } repaint(); try { Thread.sleep(50); } catch (InterruptedException exc) { } } } } // BallThread private class ChoiceHandler implements ItemListener { public void itemStateChanged(ItemEvent e) { String currentChoice = (String)(e.getItem()); echoArea.setText("Color: " + currentChoice); echoArea.append("\nClick the New Ball button."); } } //ChoiceHandler private class ClearButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { balls.removeAllElements(); } } // ClearButtonHandler private class BallButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { enableMouse = 1; echoArea.setText("Add a new " + choice.getSelectedItem() + " ball: "); echoArea.append("\nClick on screen to set the ball."); } } // BallButtonHandler } // BouncingBalls