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.
Note : In this code, you can change radius of a circle and also change axis of circle (x,y) from where circle left upper most start in Applet window, string "GO" will automatically print at the center of a circle from radius, axis(x,y) and from font size, explanation of each code in the comments block check in the code section.
If you still have any doubt feel free to ask me, or post the issue.
Shapes.java
import java.applet.*;
import java.awt.*;
public class Shapes extends Applet {
private static final long serialVersionUID = 1L;
Font f1;
String str = "GO";
public void init() {
// change background color to white of Applet screen
setBackground(Color.white);
// Initialize Font with Aerial, BOLD and Size 24
f1 = new Font("Arial", Font.BOLD, 24);
}
// Function to draw and fill shapes
public void paint(Graphics g) {
// Draw a circle and change it color to yellow
g.setColor(Color.yellow);
// upper left corner axis (x,y)
int x = 50, y = 100;
// radius of a circle
int radius = 200;
// fill oval with required axis and radius of circle
g.fillOval(x, y, radius, radius);
// get font, so to find font size
FontMetrics fm = g.getFontMetrics(f1);
// to find center of circle for applying text GO
int xText = radius / 2 - fm.stringWidth(str) / 2;
int yText = radius / 2 - fm.getHeight() / 2 + fm.getAscent();
// add axis (x,y) from where axis of circle draw for exact center
xText = xText + x;
yText = yText + y;
// font apply to text
g.setFont(f1);
g.setColor(Color.black);
g.drawString(str, xText, yText);
}
}