Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
3.8 kB
1
Indexable
Never
import java.awt.*;
import java.awt.event.*;

class ShapeFrame extends Frame {
    private Shape[] shapes;

    public ShapeFrame(Color color, int xPosition, int yPosition, int size) {
        setTitle("Shape Printer");
        setSize(400, 400);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });

        shapes = new Shape[]{
            new Circle(color, xPosition, yPosition, size),
            new Square(color, xPosition, yPosition, size),
            new Triangle(color, xPosition, yPosition, size)
        };

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                for (Shape shape : shapes) {
                    shape.handleKeyPress(e);
                }
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        for (Shape shape : shapes) {
            shape.draw(g);
        }
    }

    public static void main(String[] args) {
        Color color = Color.RED;
        int xPosition = 100;
        int yPosition = 100;
        int size = 20;

        new ShapeFrame(color, xPosition, yPosition, size);
    }
}

abstract class Shape {
    protected Color[] availableColors = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
    protected int currentColorIndex = 0;
    protected Color color;
    protected Point position;
    protected int size;

    public Shape(Color color, int xPosition, int yPosition, int size) {
        this.color = color;
        this.position = new Point(xPosition, yPosition);
        this.size = size;
    }

    protected void resizeShape(int factor) {
        size += factor;
        if (size < 1) {
            size = 1;
        }
    }

    protected void changeColor() {
        currentColorIndex = (currentColorIndex + 1) % availableColors.length;
        color = availableColors[currentColorIndex];
    }

    public abstract void draw(Graphics g);
    public abstract void handleKeyPress(KeyEvent e);
}

class Circle extends Shape {
    public Circle(Color color, int xPosition, int yPosition, int size) {
        super(color, xPosition, yPosition, size);
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillOval(position.x, position.y, size * 2, size * 2);
    }

    public void handleKeyPress(KeyEvent e) {
        char keyChar = e.getKeyChar();
        if (keyChar == '+') {
            resizeShape(1);
        } else if (keyChar == '-') {
            resizeShape(-1);
        }
    }
}

class Square extends Shape {
    public Square(Color color, int xPosition, int yPosition, int size) {
        super(color, xPosition, yPosition, size);
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(position.x, position.y, size * 2, size * 2);
    }

    public void handleKeyPress(KeyEvent e) {
        char keyChar = e.getKeyChar();
        if (keyChar == '+') {
            resizeShape(1);
        } else if (keyChar == '-') {
            resizeShape(-1);
        }
    }
}

class Triangle extends Shape {
    public Triangle(Color color, int xPosition, int yPosition, int size) {
        super(color, xPosition, yPosition, size);
    }

    public void draw(Graphics g) {
        g.setColor(color);
        int[] xPoints = { position.x + size, position.x, position.x + 2 * size };
        int[] yPoints = { position.y, position.y + 2 * size, position.y + 2 * size };
        g.fillPolygon(xPoints, yPoints, 3);
    }

    public void handleKeyPress(KeyEvent e) {
        char keyChar = e.getKeyChar();
        if (keyChar == '+') {
            resizeShape(1);
        } else if (keyChar == '-') {
            resizeShape(-1);
        }
    }
}