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
Java code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class FillOvalExample extends Applet { // extends
Applet class
public void paint(Graphics g) {
g.setColor(Color.yellow);
// set yellow color for circle
g.fillOval(100, 100, 80, 80);
// draw fillOval
g.setColor(Color.black); //
set black color for text "go"
Font f1 = new Font("Arial",
Font.BOLD, 24); // Create Font object with Arial font,bold
and size 24
g.setFont(f1); // set the
font
g.drawString("GO", 122, 146);
// write go text
}
}
Sample
Output:
Explanation:
1. Applet has fillOval to draw the circle with filled color. See the syntax below,
fillOval
public abstract void fillOval(int x, int y, int width, int height)
Fills an oval bounded by the specified rectangle with the current color.
Parameters:
x
- the x coordinate of the upper left
corner of the oval to be filled.
y
- the y coordinate of the upper left
corner of the oval to be filled.
width
- the width of the oval to be filled.
height
- the height of the oval to be filled.
2. Applet has drawString method to print text in applet window. See the syntax below,
drawString
public abstract void drawString(String str, int x, int y)
Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.
Parameters:
str
- the string to be drawn.
x
- the x coordinate.
y
- the y coordinate.
Note: Read comments for better understanding of the code.