/* ExampleLetter @author Warren Sack @version February 2003 **/ import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class ExampleLetter extends ApplicationFrame implements MouseListener, MouseMotionListener { double horizX1; double horizX2; double horizY; double leftX; double middleX; double rightX; double upperY; double lowerY; public static void main (String[] args) { new ExampleLetter(Double.parseDouble(args[0]), Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3])); } private ExampleLetter (double leftX, double upperY, double width, double height) { super("ExampleLetter"); setSize(600,300); center(); this.leftX = leftX; this.upperY = upperY; theLetterA(width, height); addMouseListener(this); addMouseMotionListener(this); setVisible(true); } // // SLOPE // // Calculate the slope of a line that runs through // the two points specified by (X1,Y1) & (X2,Y2). // private double slope (double x1, double y1, double x2, double y2) { double finalResult = 999999; if ( x1 != x2 ) { finalResult = ( (y1-y2) / (x1-x2) ); } return(finalResult); } // // YINTERCEPT // // // Given a point on a line (X,Y) and the Slope of the // line, calculate where the line intersects the Y axis. // private double yIntercept (double x, double y, double slope) { return(y-(slope*x)); } // // XINTERSECTION // // Given the equations for two lines (parameterized as // their respective YIntercepts and Slopes) calculate // the X position of their intersection. // private double xIntersection (double slope1, double yIntercept1, double slope2, double yIntercept2) { double finalResult = 999999; if ( slope1 != slope2 ) { finalResult = ((yIntercept2-yIntercept1)/(slope1-slope2)); } return(finalResult); } // // SQUIRT // // Approximate a square root function. // private double squirt (double in) { return(java.lang.Math.sqrt(in)); } // // THELETTERA // // Draw a letter A to the screen given an upper lefthand position // (LeftX,UpperY) and a Width and Height for the letter // private void theLetterA (double width, double height) { // calculate the outside top and bottom points of the A rightX = leftX + width; lowerY = upperY - height; middleX = leftX + ( width / 2.0); // set the horizontal bar of the A at 2/3 of the way down // from the top of the letter horizY = upperY - ( 2.0 * ( height / 3.0 ) ); // calculate the slope and y intercept of the line that // constitutes the lefthand side of the A double leftA = slope(leftX,lowerY,middleX,upperY); double leftB = yIntercept(leftX,lowerY,leftA); // do the same for the righthand side of the A double rightA = slope(middleX,upperY,rightX,lowerY); double rightB = yIntercept(middleX,upperY,rightA); // set the slope to 0 and the y intercept to HorizY // for the horizontal bar of the A double horizA = 0.0; double horizB = horizY; // calculate where the horizontal bar intersects with // the lefthand and the righthand sides of the A horizX1 = xIntersection(horizA,horizB,leftA,leftB); horizX2 = xIntersection(horizA,horizB,rightA,rightB); } public void paint (Graphics g) { Graphics2D g2 = (Graphics2D)g; // System.out.println("painting again!"); // Draw the lines g2.setPaint(Color.black); g2.drawLine((int)horizX1,(int)horizY,(int)horizX2,(int)horizY); g2.drawLine((int)leftX,(int)lowerY,(int)middleX,(int)upperY); g2.drawLine((int)middleX,(int)upperY,(int)rightX,(int)lowerY); g2.setPaint(Color.black); g2.drawLine(20,-40,50,20); // These print statements are here for debugging purposes and can be removed. System.out.println("(" + (int)horizX1 + "," + (int)horizY + ") (" + (int)horizX2 + "," + (int)horizY + ")"); System.out.println("(" + (int)leftX + "," + (int)lowerY + ") (" + (int)middleX + "," + (int)upperY + ")"); System.out.println("(" + (int)middleX + "," + (int)upperY + ") (" + (int)rightX + "," + (int)lowerY + ")"); } public void mouseClicked (MouseEvent me) { } public void mousePressed (MouseEvent me) { } public void mouseReleased (MouseEvent me) { } public void mouseMoved (MouseEvent me) { } public void mouseDragged (MouseEvent me) { } public void mouseEntered (MouseEvent me) { } public void mouseExited (MouseEvent me) { } }