/** * @(#)CoolProgram.java * * Sample Applet application * * @author * @version 1.00 05/10/02 */ import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowListener; import java.awt.event.WindowEvent; import javax.swing.JApplet; import java.util.Random; public class Main extends Frame implements MouseListener { private Point p1; private Point p2; private Point p3; private Point p4; private int pointCount; public Main() { setBackground(Color.black); p1 = new Point(); p2 = new Point(); p3 = new Point(); p4 = new Point(); pointCount = 0; this.addMouseListener(this); repaint(); } // Description: This method is called any time the mouse is clicked // On the first click, it stores the location in variable p1 // On the second click, it stores the location in variable p2 // On the third click, it stores the location in variable p3 // On the fourth click, it stores the location in variable p4 // Then repaint. public void mouseClicked(MouseEvent evt) { if(pointCount == 0) { p1 = evt.getPoint(); pointCount++; } else if (pointCount == 1) { p2 = evt.getPoint(); pointCount++; } else if (pointCount == 2) { p3 = evt.getPoint(); pointCount++; } else if (pointCount == 3) { p4 = evt.getPoint(); pointCount++; } repaint(); } public void mouseEntered(MouseEvent evt) { /* no action */ } public void mouseExited(MouseEvent evt) { /* no action */ } public void mousePressed(MouseEvent evt) { /* no action */ } public void mouseReleased(MouseEvent evt) { /* no action */ } public void paint(Graphics g) { g.setColor(Color.yellow); if(pointCount == 0) { g.drawString("Click somewhere", 100, 100); } else if(pointCount == 1) { g.drawString("Click somewhere else", 100, 100); g.drawLine(p1.x, p1.y, p1.x, p1.y); // Draw point at location p1 } else if(pointCount == 2) { g.drawString("Click a third place to make a Triangle", 100, 100); g.drawLine(p2.x, p2.y, p2.x, p2.y); // Draw point at location p2 g.drawLine(p1.x, p1.y, p2.x, p2.y); // Draw line from point p1 to point p2 } else if(pointCount == 3) { g.drawString("Click one last time for MathMagic to happen!", 100, 100); g.drawLine(p3.x, p3.y, p3.x, p3.y); // Draw point at location p3 g.drawLine(p2.x, p2.y, p3.x, p3.y); // Draw line from point p2 to p3 g.drawLine(p1.x, p1.y, p3.x, p3.y); // Draw line from point p1 to p3 g.drawLine(p2.x, p2.y, p1.x, p1.y); // Draw line from point p2 to p1 } else { g.drawLine(p3.x, p3.y, p3.x, p3.y); // Draw point at location p3 g.drawLine(p2.x, p2.y, p3.x, p3.y); // Draw line from point p2 to p3 g.drawLine(p1.x, p1.y, p3.x, p3.y); // Draw line from point p1 to p3 g.drawLine(p2.x, p2.y, p1.x, p1.y); // Draw line from point p2 to p1 for(int i = 0; i < 100000; i++) { // *** TO DO *** Please see the description on the Unit 8 assignment } } } public static void main(String [] args) { Main f = new Main (); f.setSize(1000,800); f.setVisible(true); f.setLayout(new FlowLayout()); } }