Program to design an applet which display a triangle filled with red colour and a message as "The triangle" in blue below it

A simple program which display a triangle using fillPolygon( ) method with a caption below it. Here, xPoints[ ] array holds the x coordinates and yPoints[ ] array holds the y coordinates of polygon.

The fillPolygon( ) method fills a closed polygon defined by arrays of x and y coordinates. The syntax for fillPolygon( ) is given below:
void fillPolygon (int[ ] xPoints, int[ ] yPoints, int nPoints)

PROGRAM
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class FilledTriangleApplet extends Applet {

 public void paint(Graphics g) {
  
  int xPoints[] = {110,180,30,110};
  int yPoints[] = {30,100,100,30};
  
  g.setColor(Color.RED);
  g.fillPolygon(xPoints, yPoints, 4);
  
  g.setColor(Color.BLUE);
  g.drawString("The triangle", 80, 140);
 }
}

/* <applet code="FilledTriangleApplet.class" width=250 height=250>
   </applet>
*/

OUTPUT

C:\>javac FilledTriangleApplet.java
C:\>appletviewer FilledTriangleApplet.java



Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to draw Smiley in Applet