Untitled
unknown
plain_text
2 years ago
1.3 kB
9
Indexable
import java.awt.*;
import java.awt.event.*;
class ShapePrinter extends Frame {
private String shape;
private int size;
private Color color;
public ShapePrinter(String shape, int size, Color color) {
this.shape = shape;
this.size = size;
this.color = color;
setTitle("Shape Printer");
setSize(400, 400);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
});
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(color);
if (shape.equalsIgnoreCase("circle")) {
g.fillOval(100, 100, size * 10, size * 10);
} else if (shape.equalsIgnoreCase("square")) {
g.fillRect(100, 100, size * 10, size * 10);
} else if (shape.equalsIgnoreCase("triangle")) {
int[] xPoints = { 100, 100 + size * 5, 100 - size * 5 };
int[] yPoints = { 100 + size * 10, 100, 100 };
g.fillPolygon(xPoints, yPoints, 3);
} else {
g.drawString("Unknown shape", 150, 200);
}
}
}
public static void main(String[] args) {
String shape = "square"; //("circle", "square", "triangle")
int size = 5;
Color color = Color.RED;
ShapePrinter shapePrinter = new ShapePrinter(shape, size, color);
}
Editor is loading...