Untitled

 avatar
unknown
java
2 years ago
6.6 kB
7
Indexable

import java.awt.*;
import java.awt.event.*;

abstract class Shape extends Frame {
    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;

        setTitle("Shape Printer");
        setSize(400, 400);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                char keyChar = e.getKeyChar();
                if (keyChar == '+') {
                    resizeShape(1);
                    calculateVolume();
                } else if (keyChar == '-') {
                    resizeShape(-1);
                    calculateVolume();
                }
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_LEFT) {
                    moveLeft();
                } else if (keyCode == KeyEvent.VK_RIGHT) {
                    moveRight();
                } else if (keyCode == KeyEvent.VK_UP) {
                    moveUp();
                }
                repaint();
            }
        });

        addMouseListener(new MouseAdapter() {
            private int prevX;
            private int prevY;
            private boolean isDragging = false;

            public void mousePressed(MouseEvent e) {
                int mouseX = e.getX();
                int mouseY = e.getY();
                if (isInsideShape(mouseX, mouseY)) {
                    prevX = mouseX - position.x;
                    prevY = mouseY - position.y;
                    isDragging = true;
                }
            }

            public void mouseReleased(MouseEvent e) {
                isDragging = false;
            }

            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    changeColor();
                    repaint();
                } else if (e.getClickCount() == 2) {
                    repaint();
                }
                calculateVolume();
            }

            public void mouseDragged(MouseEvent e) {
                if (isDragging) {
                    int mouseX = e.getX();
                    int mouseY = e.getY();
                    position.x = mouseX - prevX;
                    position.y = mouseY - prevY;
                    repaint();
                }
            }
        });
    }

    protected abstract boolean isInsideShape(int mouseX, int mouseY);

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

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

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(color);
        drawShape(g);
    }

    protected abstract void drawShape(Graphics g);

    protected abstract void calculateVolume();

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

    protected boolean isInsideShape(int mouseX, int mouseY) {
        int centerX = position.x + size;
        int centerY = position.y + size;
        int distance = (int) Math.sqrt(Math.pow(centerX - mouseX, 2) + Math.pow(centerY - mouseY, 2));
        return distance <= size;
    }

    protected void drawShape(Graphics g) {
        g.fillOval(position.x, position.y, size * 2, size * 2);
    }

    protected void calculateVolume() {
        double area = Math.PI * Math.pow(size, 2);
        System.out.println("Volume (Circle): " + area);
    }
}

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

    protected boolean isInsideShape(int mouseX, int mouseY) {
        return mouseX >= position.x && mouseX <= position.x + size * 2 &&
                mouseY >= position.y && mouseY <= position.y + size * 2;
    }

    protected void drawShape(Graphics g) {
        g.fillRect(position.x, position.y, size * 2, size * 2);
    }

    protected void calculateVolume() {
        double area = Math.pow(size, 2);
        System.out.println("Volume (Square): " + area);
    }
}

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

    protected boolean isInsideShape(int mouseX, int mouseY) {
        int x1 = position.x + size;
        int y1 = position.y;
        int x2 = position.x;
        int y2 = position.y + 2 * size;
        int x3 = position.x + 2 * size;
        int y3 = position.y + 2 * size;

        int area = Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2);
        int a1 = Math.abs((x1 * (y2 - mouseY) + mouseX * (y3 - y1) + x3 * (mouseY - y2)) / 2);
        int a2 = Math.abs((x1 * (mouseY - y3) + mouseX * (y3 - y1) + x3 * (y1 - mouseY)) / 2);
        int a3 = Math.abs((x1 * (y2 - y3) + x2 * (mouseY - y1) + mouseX * (y1 - y2)) / 2);

        return a1 + a2 + a3 == area;
    }

    protected void drawShape(Graphics g) {
        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);
    }

    protected void calculateVolume() {
        double area = 0.5 * size * size;
        System.out.println("Volume (Triangle): " + area);
    }
}

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

        Circle circle = new Circle(color, xPosition, yPosition, size);
        Square square = new Square(color, xPosition, yPosition, size);
        Triangle triangle = new Triangle(color, xPosition, yPosition, size);
    }
}
Editor is loading...