//TriangleApplet.java import java.awt.*; import javax.swing.*; public class TriangleApplet extends JApplet { int x1, y1, x2, y2, x3, y3; public void init() { String input; input = JOptionPane.showInputDialog( "Enter x of point 1" ); x1 = Integer.parseInt( input ); input = JOptionPane.showInputDialog( "Enter y of point 1" ); y1 = Integer.parseInt( input ); input = JOptionPane.showInputDialog( "Enter x of point 2" ); x2 = Integer.parseInt( input ); input = JOptionPane.showInputDialog( "Enter y of point 2" ); y2 = Integer.parseInt( input ); input = JOptionPane.showInputDialog( "Enter x of point 3" ); x3 = Integer.parseInt( input ); input = JOptionPane.showInputDialog( "Enter y of point 3" ); y3 = Integer.parseInt( input ); } public void paint (Graphics g) { g.setFont( new Font( "Serif", Font.BOLD, 16 ) ); //the 3 lines g.drawLine( x1, y1, x2, y2 ); g.drawLine( x1, y1, x3, y3 ); g.drawLine( x3, y3, x2, y2 ); //label the vertices g.setColor( Color.RED ); g.drawString("("+x1+","+y1+")",x1,y1); g.drawString("("+x2+","+y2+")",x2,y2); g.drawString("("+x3+","+y3+")",x3,y3); //midpoints of the 3 lines int x12mid = (x1+x2) / 2; int y12mid = (y1+y2) / 2; int x13mid = (x1+x3) / 2; int y13mid = (y1+y3) / 2; int x23mid = (x2+x3) / 2; int y23mid = (y2+y3) / 2; //lengths of the lines (Pythagorean formula) int len12 = (int)Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); int len13 = (int)Math.sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3)); int len23 = (int)Math.sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3)); //display the length at the midpoint g.setColor( Color.CYAN ); g.fillOval(x12mid-1,y12mid-1,2,2); g.drawString(""+len12,x12mid,y12mid); g.fillOval(x13mid-1,y13mid-1,2,2); g.drawString(""+len13,x13mid,y13mid); g.fillOval(x23mid-1,y23mid-1,2,2); g.drawString(""+len23,x23mid,y23mid); //centroid of triangle: (average of x's, average of y's). //center of mass, center of gravity, balance point. //average of all points in the triangle. int xc, yc; xc = (x1+x2+x3) / 3; yc = (y1+y2+y3) / 3; //display the centroid g.setColor( Color.BLUE ); g.fillOval(xc-1,yc-1,2,2); g.drawString("("+xc+","+yc+")",xc,yc); //line from vertex to centroid g.drawLine( x1, y1, xc, yc ); g.drawLine( x2, y2, xc, yc ); g.drawLine( x3, y3, xc, yc ); //line from midpoint to centroid g.setColor( Color.YELLOW ); g.drawLine( x12mid,y12mid, xc, yc ); g.drawLine( x13mid,y13mid, xc, yc ); g.drawLine( x23mid,y23mid, xc, yc ); //circle centered at vertex, of radius to centroid g.setColor( Color.MAGENTA ); int len1c = (int)Math.sqrt((x1-xc)*(x1-xc) + (y1-yc)*(y1-yc)); g.drawOval(x1-len1c, y1-len1c, 2*len1c, 2*len1c); int len2c = (int)Math.sqrt((x2-xc)*(x2-xc) + (y2-yc)*(y2-yc)); g.drawOval(x2-len2c, y2-len2c, 2*len2c, 2*len2c); int len3c = (int)Math.sqrt((x3-xc)*(x3-xc) + (y3-yc)*(y3-yc)); g.drawOval(x3-len3c, y3-len3c, 2*len3c, 2*len3c); } }