Hello All. This program lets the user select any three points within the applet window and draw a triangle. It then prints out the perimeter of the triangle and the area of the triangle. After the user has selected three points, you should then be able to click a fourth time to clear the screen and to start a new triangle. I can not figure out how to clear the applet and start the new points of the triangle. Here is my code so far:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Triangle extends Applet {

 int[] xcoords = new int [3];
 int[] ycoords = new int [3];
 int pointct = 0;

 public void init () {

  resize (500,500);
  addMouseListener (new MouseAdapter ()

     public void mousePressed (MouseEvent e) {

      xcoords[pointct] = e.getX();
      ycoords[pointct] = e.getY();
      pointct ++;


 public void paint (Graphics g) {

 g.drawPolygon(xcoords, ycoords, pointct); //Draws Triangle
 g.drawLine (xcoords[0],ycoords[0],xcoords[0],ycoords[0]); //Draws first point

 double A = Math.sqrt(Math.pow((xcoords[0]-xcoords[1]),2)+Math.pow((ycoords[0]-ycoords[1]),2));
 double B = Math.sqrt(Math.pow((xcoords[1]-xcoords[2]),2)+Math.pow((ycoords[1]-ycoords[2]),2));
 double C = Math.sqrt(Math.pow((xcoords[2]-xcoords[0]),2)+Math.pow((ycoords[2]-ycoords[0]),2));

 double perimeter = (A+B+C);
 double S = (perimeter * 0.5);
 double Area = (Math.sqrt(S*(S-A)*(S-B)*(S-C)));

 g.drawString ("Perimeter =" + perimeter,0,400);
 g.drawString ("Area =" + Area,0,425);



Thank you for reading :) Any help or information would be greatly appreciated.

overwrite the canvas with a solid colour.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.