Tuesday, 29 November 2016

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)

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.fillPolygon(xPoints, yPoints, 4);
  g.drawString("The triangle", 80, 140);

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


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

No comments:

Post a Comment