Question 1- Write a Java program to create an applet to draw a circle. Set background color of the circle as red. Also write your name and roll number below the circle.
Solution -
import java.applet.*;
import java.awt.*;
public class drawcircle extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(50,50,150,150);
g.setColor(Color.black);
g.drawString("Name = IGNOULIA",90,220);
g.drawString("Roll No = FSJX3543",90,240);
}
}
/*
<applet code="drawcircle.class" width="330" height="300"></applet>
*/
filename: drawcircle.java
For compiling the file type this command in cmd: javac drawcircle.javaFor execution of the file use this command: appletviewer drawcircle.java
Explanation:
setColor method of graphics object is used to set the color. fillOval(int x,int y,int width,int height) method is used to draw a oval,here x and y argument of fillOval() is used to specify the x and y coordinate of oval and length,width parameter is used to specifiy the width,height of oval.
Because here we are drawing a circle height,width parameter should be equal.
Output:

0 comments:
Post a Comment