Untitled

 avatar
unknown
java
a year ago
3.0 kB
6
Indexable
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)
        };
    }

    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);

    protected void moveLeft() {
        position.x -= 5;
    }

    protected void moveRight() {
        position.x += 5;
    }

    protected void moveUp() {
        position.y -= 5;
    }
}

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);
    }
}

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);
    }
}

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);
    }
}
Editor is loading...