import javax.swing.*; import java.awt.*; public class Triangles extends JApplet { double perimeter; double area; double base; double height; double length; String triType; public void init() { String input; // type of triangle to be computed triType = JOptionPane.showInputDialog( "Enter the type of triangle" + "(R, E, I)" ); triType = triType.toUpperCase(); if ( triType.equals("E") ) // equilateral triangle { input = JOptionPane.showInputDialog( "Enter length of the equilateral triangle " ); length = Double.parseDouble( input ); height = (Math.sqrt(3) / 2) * length; if ( length <= 0 ) JOptionPane.showMessageDialog( null, "error, please enter value greater than 0" ); else { area = length / 2 * height; perimeter = length * 3; /*JOptionPane.showMessageDialog( null, "Area = " + area + "\n" + "Perimeter = " + perimeter ); */ } } else if ( triType.equals("R") ) { // right triangle input = JOptionPane.showInputDialog( "Enter the length of one" + " non-hypotenuse side of the right triangle." ); base = Double.parseDouble( input ); input = JOptionPane.showInputDialog( "Enter the length of the other" + " non-hypotenuse side of the right triangle." ); height = Double.parseDouble( input ); if ( base<=0 || height<=0 ) JOptionPane.showMessageDialog( null, "error, please enter values greater than 0" ); else { area = base / 2 * height; perimeter = height + base + Math.sqrt(height*height+ base*base); /*JOptionPane.showMessageDialog( null, "Area = " + area + "\n" + "Perimeter = " + perimeter ); */ } } else if ( triType.equals("I") ) { // isosceles triangle input = JOptionPane.showInputDialog( "Enter the length of the" + " base of the isoceles triangle." ); base = Double.parseDouble( input ); input = JOptionPane.showInputDialog( "Enter the length of the other" + " side of the isoceles triangle." ); length = Double.parseDouble( input ); height = Math.sqrt(length * length - (base * base / 4)); if (base > (length + length)) JOptionPane.showMessageDialog( null, "error: the base must be less"+ " than the sum of the sides" ); else if (length <= 0 || base <=0) JOptionPane.showMessageDialog( null, "error, please enter values greater than 0" ); else { area = base / 2 * height; perimeter = length + length + base; /*JOptionPane.showMessageDialog( null, "Area = " + area + "\n" + "Perimeter = " + perimeter ); */ } } else JOptionPane.showMessageDialog( null, "sorry: Only R, E, or I" ); } public void paint( Graphics g ) { super.paint( g ); g.drawString( "Area = " + area, 10, 100 ); g.drawString( "Perimeter = " + perimeter, 10, 200 ); //optional //draw the triangle int iBase = (int)base; int iHeight = (int)height; int iLength = (int)length; if ( triType.equals("R") ) { g.drawLine( 300, 300, 300, 300+iBase ); g.drawLine( 300, 300+iBase, 300+iHeight, 300+iBase ); g.drawLine( 300, 300, 300+iHeight, 300+iBase ); } else if ( triType.equals("E") ) { g.drawLine( 300, 300, 300, 300+iLength ); double angle = Math.toRadians(30); //30 degrees "down" int x2 = (int)(Math.cos(angle)*length+300); int y2 = (int)(Math.sin(angle)*length+300); g.drawLine( 300, 300, x2, y2 ); g.drawLine( 300, 300+iLength, x2, y2 ); } else if ( triType.equals("I") ) { g.drawLine( 300, 300, 300+iBase, 300 ); //double angle = Math.toRadians(30); double theta = Math.asin(height/length); int x2 = (int)(Math.cos(-theta)*length+300); int y2 = (int)(Math.sin(-theta)*length+300); g.drawLine( 300, 300, x2, y2 ); g.drawLine( 300+iBase, 300, x2, y2 ); } } } /* w/out trig: Right: (x,y) up: (x,y-s1) right:(x+s2,y) Iso: (x,y) up: (x+b/2,y-h) right:(x+b,y) Equi (special case of Iso, b=s) (x,y) up: (x+s/2,y-h) right:(x+s,y) */