/* Author: Gerard Whyte */ /* AKA: Gearóid Dé Faoite */ /* Date: 3/5/01 */ /* Copyright (C) 2001 Gerard Whyte */ //Importing in all the classes, that will be used in the applet, provided by the Java Libraries import java.applet.*; import java.awt.*; import java.awt.event.*; /////////////////////////////////////////////////////////////////////////////////////////////////////////////// /*********** This is the class that defines the Pad, where most of the events in the applet will occur *******/ /////////////////////////////////////////////////////////////////////////////////////////////////////////////// class Pad extends Canvas { private Dimension size; private int startX; private int startY; private int endX; private int endY; private Color darkPurple = new Color(117, 115, 145); public Pad(int width, int height) { size = new Dimension(width, height); setSize(size); } public void paint(Graphics g) { this.drawLine(this.getGraphics(), 0, 0, 549, 0, darkPurple); this.drawLine(this.getGraphics(), 0, 0, 0, 300, darkPurple); this.drawLine(this.getGraphics(), 549, 0, 549, 299, darkPurple); this.drawLine(this.getGraphics(), 0, 299, 549, 299, darkPurple); } public void drawLine(Graphics g, int x1, int y1, int x2, int y2) { g.drawLine(x1, y1, x2, y2); } public void drawLine(Graphics g, int x1, int y1, int x2, int y2, Color c) { g.setColor(c); g.drawLine(x1, y1, x2, y2); g.setColor(Color.black); } public void setStart(int x1, int y1) { startX = x1; startY = y1; } public void setEnd(int x1, int y1) { endX = x1; endY = y1; } public int getStartX() { return this.startX; } public int getStartY() { return this.startY; } public int getEndX() { return this.endX; } public int getEndY() { return this.endY; } public void drawDot(Graphics g, int x, int y, int w, Color c) { g.setColor(c); g.fillOval(x, y, w, w); g.setColor(Color.black); } } ////////////////////////////////////////////////////////////////////////////////////// /************ Applet Class - This is the class which defines the applet *************/ ////////////////////////////////////////////////////////////////////////////////////// public class VectorLaws extends Applet implements ActionListener, MouseListener, MouseMotionListener { //Attributes /* Components */ private Label modeTitle; private Pad pad; private Button triangle; private Button parallel; private Button game; private Button go; private Button reset; /* Non-changed Attributes */ private Graphics p; private Color lightPurple = new Color(160, 160, 183); private Color darkPurple = new Color(117, 115, 145); private Color blue = new Color(0, 64, 128); private Font arial = new Font("Arial", Font.PLAIN, 12); /* Local Variables */ int mode = 0; boolean ready = false; int point = 0; int lastX; int lastY; int[] paraArray = new int[6]; //Methods /* Initialiser */ public void init() { this.setBackground(Color.white); modeTitle = new Label("Triangle and Parallelogram Laws: Please select a mode."); modeTitle.setFont(arial); add(modeTitle); pad = new Pad(550, 300); pad.addMouseListener(this); add(pad); p = pad.getGraphics(); triangle = new Button("Triangle Law"); triangle.addActionListener(this); triangle.setFont(arial); add(triangle); parallel = new Button("Parallelogram Law"); parallel.addActionListener(this); parallel.setFont(arial); add(parallel); game = new Button("Game"); game.addActionListener(this); game.setFont(arial); add(game); go = new Button("Go"); go.addActionListener(this); go.setFont(arial); add(go); reset = new Button("Reset"); reset.addActionListener(this); reset.setFont(arial); add(reset); } /* Method that decides what happens when an action is performed on a component */ public void actionPerformed(ActionEvent e) { if (e.getSource() == triangle) { mode = 1; // Mode set to Triangle Law modeTitle.setText("Triangle Law"); pad.repaint(); point = 0; ready = false; } if (e.getSource() == parallel) { mode = 2; // Mode set to Parallelogram Law modeTitle.setText("Parallelogram Law"); pad.repaint(); point = 0; ready = false; } if (e.getSource() == game) { mode = 3; // Mode set to Game modeTitle.setText("Parallelogram Law Game"); pad.repaint(); point = 0; ready = false; } if (e.getSource() == go) { if (ready) { if (mode == 1) // Triangle Law { /* Draw Equivalent vector */ pad.drawLine(p, pad.getStartX(), pad.getStartY(), pad.getEndX(), pad.getEndY(), Color.red); /* Draw dot at the end of the equivalent vector */ pad.drawDot(p, pad.getEndX() - 3, pad.getEndY(), 5, Color.red); mode = 0; //reset mode } if (mode == 2 || mode == 3) // Parallogram Law or Game { /* Draw Parallelogram */ pad.drawLine(p, paraArray[2], paraArray[3], paraArray[4] + (paraArray[2] - paraArray[0]), paraArray[5] + (paraArray[3] - paraArray[1]), lightPurple); pad.drawLine(p, paraArray[4], paraArray[5], paraArray[4] + (paraArray[2] - paraArray[0]), paraArray[5] + (paraArray[3] - paraArray[1]), lightPurple); /* Draw Resultant vector */ pad.drawLine(p, pad.getStartX(), pad.getStartY(), paraArray[4] + (paraArray[2] - paraArray[0]), paraArray[5] + (paraArray[3] - paraArray[1]), Color.red); /* Draw dot at the end of the resultant vector */ pad.drawDot(p, (paraArray[4] + (paraArray[2] - paraArray[0])) - 3, paraArray[5] + (paraArray[3] - paraArray[1]), 5, Color.red); mode = 0; // reset mode } } } if (e.getSource() == reset) { mode = 0; // Mode reset to nothing point = 0; ready = false; pad.repaint(); modeTitle.setText("Triangle and Parallelogram Laws: Please select a mode."); } } /* Method that decides what happens when the mouse button is pressed */ public void mousePressed(MouseEvent e) { if (e.getSource() == pad) { if (mode == 1) // Triangle Law Mode { if (point == 0) // Set Starting Point { pad.setStart(e.getX(), e.getY()); point += 1; } else if (point == 1) // Draw First Vector { lastX = e.getX(); lastY = e.getY(); pad.drawLine(p, pad.getStartX(), pad.getStartY(), lastX, lastY); pad.drawDot(p, lastX - 3, lastY, 5, blue); point += 1; } else if (point == 2) // Draw Second and up Vectors { pad.drawLine(p, lastX, lastY, e.getX(), e.getY()); lastX = e.getX(); lastY = e.getY(); pad.drawDot(p, lastX - 3, lastY, 5, blue); pad.setEnd(lastX, lastY); ready = true; } } if (mode == 2) // Parallelogram Law Mode { if (point == 0) // Set Starting Point { pad.setStart(e.getX(), e.getY()); paraArray[0] = e.getX(); paraArray[1] = e.getY(); point += 1; } else if (point == 1) // Draw First Vector { pad.drawLine(p, pad.getStartX(), pad.getStartY(), e.getX(), e.getY()); pad.drawDot(p, e.getX() - 3, e.getY(), 5, blue); paraArray[2] = e.getX(); paraArray[3] = e.getY(); point += 1; } else if (point == 2) // Draw Second Vector { pad.drawLine(p, pad.getStartX(), pad.getStartY(), e.getX(), e.getY()); pad.drawDot(p, e.getX() - 3, e.getY(), 5, blue); paraArray[4] = e.getX(); paraArray[5] = e.getY(); ready = true; point += 1; } } if (mode == 3) // Game Mode { if (point == 0) // Set Starting Point { pad.setStart(e.getX(), e.getY()); paraArray[0] = e.getX(); paraArray[1] = e.getY(); point += 1; } else if (point == 1) // Draw First Vector { pad.drawLine(p, pad.getStartX(), pad.getStartY(), e.getX(), e.getY()); pad.drawDot(p, e.getX() - 3, e.getY(), 5, blue); paraArray[2] = e.getX(); paraArray[3] = e.getY(); point += 1; } else if (point == 2) // Draw Second Vector { pad.drawLine(p, pad.getStartX(), pad.getStartY(), e.getX(), e.getY()); pad.drawDot(p, e.getX() - 3, e.getY(), 5, blue); paraArray[4] = e.getX(); paraArray[5] = e.getY(); point += 1; } else if (point == 3) // Guess correct end point { pad.drawDot(p, e.getX() - 3, e.getY(), 5, darkPurple); if (((e.getX() < (paraArray[4] + (paraArray[2] - paraArray[0]) + 5)) && (e.getX() > (paraArray[4] + (paraArray[2] - paraArray[0]) - 5))) && ((e.getY() < (paraArray[5] + (paraArray[3] - paraArray[1]) + 5)) && (e.getY() > (paraArray[5] + (paraArray[3] - paraArray[1]) - 5)))) { pad.drawLine(p, paraArray[2], paraArray[3], paraArray[4] + (paraArray[2] - paraArray[0]), paraArray[5] + (paraArray[3] - paraArray[1]), lightPurple); pad.drawLine(p, paraArray[4], paraArray[5], paraArray[4] + (paraArray[2] - paraArray[0]), paraArray[5] + (paraArray[3] - paraArray[1]), lightPurple); pad.drawLine(p, pad.getStartX(), pad.getStartY(), paraArray[4] + (paraArray[2] - paraArray[0]), paraArray[5] + (paraArray[3] - paraArray[1]), Color.red); pad.drawDot(p, (paraArray[4] + (paraArray[2] - paraArray[0])) - 3, paraArray[5] + (paraArray[3] - paraArray[1]), 5, Color.red); modeTitle.setText("Correct. Press Game to play again."); point += 1; mode = 0; } else // Guessed incorrectly { modeTitle.setText("Incorrect. Please try again"); ready = true; } } } } } /* Methods to deal with other Mouse Events, none of these are used */ public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseClicked(MouseEvent e){ } public void mouseMoved(MouseEvent e){ } public void mouseDragged(MouseEvent e){ } }