In: Computer Science
Write an applet in Java that creates a yellow colored filled circle on screen. Inside this circle the word “GO” with Arial font and size 24, bold face needs to be printed. Justify your syntax.
There are two standard ways in which you can run an applet :
first approach:-
//this is HelloWorld.java program
// A Hello World Applet
// Save file as HelloWorld.java
import java.applet.Applet;
import java.awt.Graphics;
// HelloWorld class extends Applet
public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
s.setColor(Color.YELLOW);
s.fillOval(50, 50, 100, 100);
Font f = new Font("Arial", Font.BOLD, 24);
g.setFont(f);
g.setColor(Color.GREEN);
s.drawString("GO", 60, 75);
}
}
//this is HelloWorld.html file
<html><body>
<applet code=HelloWorld width=500 height=500></applet>
</body></html>
With this approach, first compile HelloWorld.java file and then simply run below command to run applet :-
appletviewer HelloWorld.html
second approaach:-
//this is HelloWorld.java file
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="HelloWorld" width=200 height=60>
</applet>
*/
// HelloWorld class extends Applet
public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
s.setColor(Color.YELLOW);
s.fillOval(50, 50, 100, 100);//this will form a yellow color
circle
Font f = new Font("Arial", Font.BOLD, 24);
g.setFont(f);
g.setColor(Color.GREEN);
s.drawString("GO", 60, 75);//this will write a green string on
applet inside the circle
}
}
With this approach, first compile HelloWorld.java file and then simply run below command to run applet :-
appletviewer HelloWorld