Untitled

 avatar
unknown
plain_text
a year ago
3.2 kB
3
Indexable
import java.awt.*;
import java.awt.event.*;

class ShapePrinter extends Frame {
    private String shape;
    private int size;
    private Color color;
    private int xPosition;
    private int yPosition;

    public ShapePrinter(String shape, int size, Color color) {
        this.shape = shape;
        this.size = size;
        this.color = color;
        this.xPosition = 100; // Initial X position
        this.yPosition = 100; // Initial Y position

        setTitle("Shape Printer");
        setSize(400, 400);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });
        
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int mouseX = e.getX();
                int mouseY = e.getY();
                
                if (isInsideShape(mouseX, mouseY)) {
                    // Change the shape's position and color
                    shiftShape();
                    changeColor();
                }
            }
        });
    }

    private boolean isInsideShape(int mouseX, int mouseY) {
        // Check if the mouse click is inside the shape
        if (shape.equalsIgnoreCase("circle")) {
            int centerX = xPosition + size * 5;
            int centerY = yPosition + size * 5;
            int distance = (int) Math.sqrt(Math.pow(centerX - mouseX, 2) + Math.pow(centerY - mouseY, 2));
            return distance <= size * 5;
        } else if (shape.equalsIgnoreCase("square")) {
            return mouseX >= xPosition && mouseX <= xPosition + size * 10 &&
                   mouseY >= yPosition && mouseY <= yPosition + size * 10;
        } else if (shape.equalsIgnoreCase("triangle")) {
            // Implement triangle click detection if needed
        }
        return false;
    }

    private void shiftShape() {
        // Shift the shape to a new position
        xPosition += 20; // You can adjust the shift amount as needed
        yPosition += 20; // You can adjust the shift amount as needed
        repaint();
    }

    private void changeColor() {
        // Change the shape's color
        if (color == Color.RED) {
            color = Color.BLUE;
        } else {
            color = Color.RED;
        }
        repaint();
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(color);
        if (shape.equalsIgnoreCase("circle")) {
            g.fillOval(xPosition, yPosition, size * 10, size * 10);
        } else if (shape.equalsIgnoreCase("square")) {
            g.fillRect(xPosition, yPosition, size * 10, size * 10);
        } else if (shape.equalsIgnoreCase("triangle")) {
            // Implement triangle drawing if needed
        } else {
            g.drawString("Unknown shape", 150, 200);
        }
    }
}

public class Main {
    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);
    }
}