Tuesday, 29 September 2020

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)

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.fillPolygon(xPoints, yPoints, 3);
  // Draws a circle
  g.fillOval(90, 120, 70, 70);

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


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

No comments:

Post a comment