import
java.applet.Applet;
import
java.awt.Button;
import
java.awt.Color;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.TextField;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
public
class
ShapeApplet
extends
Applet
implements
ActionListener {
private
String shapeName;
private
int
centerX, centerY;
private
boolean
drawShape =
false
;
private
TextField shapeInput;
private
Button drawButton;
public
void
init() {
setLayout(
null
);
setBackground(
new
Color(
230
,
230
,
230
));
shapeInput =
new
TextField(
30
);
shapeInput.setFont(
new
Font(
"Arial"
, Font.PLAIN,
20
));
shapeInput.setBounds(
50
,
20
,
300
,
40
);
add(shapeInput);
drawButton =
new
Button(
"Draw Shape"
);
drawButton.setFont(
new
Font(
"Arial"
, Font.BOLD,
20
));
drawButton.setBackground(
new
Color(
50
,
100
,
150
));
drawButton.setForeground(Color.WHITE);
drawButton.setBounds(
380
,
20
,
150
,
40
);
drawButton.addActionListener(
this
);
add(drawButton);
}
public
void
actionPerformed(ActionEvent e) {
if
(e.getSource() == drawButton) {
shapeName = shapeInput.getText().toLowerCase();
drawShape =
true
;
repaint();
}
}
public
void
paint(Graphics g) {
centerX = getWidth() /
2
;
centerY = getHeight() /
2
;
g.setFont(
new
Font(
"Arial"
, Font.BOLD,
20
));
g.setColor(Color.BLACK);
g.drawString(
"Enter a shape name and click 'Draw Shape':"
,
10
,
90
);
g.drawString(
"Available shapes: 'circle', 'rectangle', 'triangle', 'oval', 'square', 'pentagon'"
,
10
,
120
);
if
(drawShape) {
switch
(shapeName) {
case
"circle"
:
g.fillOval(centerX -
75
, centerY -
75
,
150
,
150
);
break
;
case
"rectangle"
:
g.fillRect(centerX -
100
, centerY -
50
,
200
,
100
);
break
;
case
"triangle"
:
int
[] xPoints = { centerX, centerX -
75
, centerX +
75
};
int
[] yPoints = { centerY -
75
, centerY +
75
, centerY +
75
};
g.fillPolygon(xPoints, yPoints,
3
);
break
;
case
"oval"
:
g.fillOval(centerX -
100
, centerY -
50
,
200
,
100
);
break
;
case
"square"
:
g.fillRect(centerX -
75
, centerY -
75
,
150
,
150
);
break
;
case
"pentagon"
:
int
[] pentagonX = {centerX, centerX +
75
, centerX +
47
, centerX -
47
, centerX -
75
};
int
[] pentagonY = {centerY -
75
, centerY -
25
, centerY +
58
, centerY +
58
, centerY -
25
};
g.fillPolygon(pentagonX, pentagonY,
5
);
break
;
default
:
g.drawString(
"Invalid shape input. Try 'circle', 'rectangle', 'triangle', 'oval', 'square', or 'pentagon'."
,
10
,
240
);
}
}
}
}