package KinematicSimulator; import java.awt.Color; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.util.Random; import java.util.Vector; /** * The class serves as the engine of the application. * - It listens to mouse events from the Simulator Viewer window to * create balls or to move balls in the window. * - It informs the Simular Model when there are changes in the ball list. * - It listens to the scroll bar changes in the Kinematic Controller in * order to apply strategy to determine the velocity of the balls. * - It starts an infinite thread to keep the application running and updates * the Simulator Viewer window periodically. * * @author Helena */ public class SimulatorController extends Thread implements KinematicEventListener, MouseListener, MouseMotionListener, ComponentListener, ActionListener, WindowListener { boolean m_continue; ModelListener m_modelListener = null; Vector m_ballList = null; PhysicsStrategy m_physics = null; BallFactory m_bf = new BallFactory(); Ball current_ball = null; boolean chucking = false; int width = 500, height = 500; int m_radius = 20; void SetModelListener(ModelListener listener, Vector ballList) { m_modelListener = listener; m_ballList = ballList; } public void kinematicEvent(PhysicsStrategy s) { m_physics = s; m_physics.setSize(width, height); } public void removeEntities() { m_ballList.clear(); } public void changeRadius(int radius) { m_radius = radius; } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Exit")) { m_continue = false; } } public void mouseDragged(MouseEvent e) { int i = e.getX(); int j = e.getY(); if (current_ball != null && !chucking) current_ball.setPos(i, j); } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { int i = e.getX(); int j = e.getY(); current_ball = findBall(i, j); if (current_ball == null) { Random random = new Random(); current_ball = (Ball) m_bf.createEntity( i, j, m_radius, new Color( (int) (random.nextDouble() * 256D), (int) (random.nextDouble() * 256D), (int) (random.nextDouble() * 256D))); m_ballList.add(current_ball); current_ball.lock(); } else { chucking = true; } } public void mouseReleased(MouseEvent e) { int i = e.getX(); int j = e.getY(); if (current_ball != null) { current_ball.unlock(); if (chucking) { current_ball.accelerate( current_ball.pos_x() - (double) i, current_ball.pos_y() - (double) j); chucking = false; } } current_ball = null; } public void componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } public void componentResized(ComponentEvent e) { updateSize(e.getComponent()); } public void componentShown(ComponentEvent e) { } public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { m_continue = false; } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void updateSize(Component c) { width = c.getWidth(); height = c.getHeight(); m_physics.setSize(width, height); } protected Ball findBall(double x, double y) { for (int i = 0; i < m_ballList.size(); i++) { Ball ball = (Ball) m_ballList.elementAt(i); Vector2D vector2d = new Vector2D(x, y); Vector2D vector2d1 = new Vector2D(ball.pos_x(), ball.pos_y()); if (vector2d.subtract(vector2d1).length() < ball.radius()) return ball; } return null; } public void run() { m_continue = true; while (m_continue) { try { Thread.currentThread().sleep(10); } catch (InterruptedException e) { } m_physics.applyPhysics(m_ballList); m_modelListener.SimulationOccurred(); } System.exit(0); } }