Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
4.8 kB
2
Indexable
Never
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(); 
                }
                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();
}

import java.awt.*;

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

import java.awt.*;

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

import java.awt.Color;


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