Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Given program will draw a circle inside a triangle. The color of circle is "blue" and triangle having color "yellow". To fill circle with respective colours first we have to set the colour using setColor(Color c) method of Graphics class and then use the fillPolygon() and fillOval( ) method.

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)
The fillOval( ) method fills an oval bounded by the specified rectangle with the current color. The rectangle is filled using the graphics context's current color. The syntax for fillOval( ) is given below: 
void fillOval (int x, int y, int width, int height)

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

public class CircleInsideTriangle extends Applet {

 public void paint(Graphics g) {
  
  // Draws a triangle 
  int xPoints[] = {120, 220, 30};
  int yPoints[] = {30, 220, 220};
  
  g.setColor(Color.YELLOW);
  g.fillPolygon(xPoints, yPoints, 3);
  
  // Draws a circle
  g.setColor(Color.BLUE);
  g.fillOval(90, 120, 70, 70);
  
 }
}

/* <applet code="CircleInsideTriangle" width=250 height=250>
   </applet>
*/

OUTPUT

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




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.

First Android App | Android Development | Java