-1

我正在制作一个绘图小程序,但我无法重新调整线条的大小。矩形和椭圆形重新调整大小就好了,但不是线。我相信我对重新调整大小的方法有正确的代码,以允许重新调整行的大小,但它就是行不通。我希望有人能帮我解决这个问题。

这是课程:

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.ArrayList;
import model.Model;
import shapes.Rectangle;
import shapes.Shape;
import shapes.Line;
import ui.panels.ActionPanel;
import ui.panels.ButtonPanel.ColorPanel;
import ui.panels.MainPanel;

public class ShapeMouseHandler extends MouseAdapter { 

    private Model model;
    private int startX;
    private int startY;
    private int dragStartX;
    private int dragStartY;
    private String oldColor;
    private MainPanel.ColorPanel colorPanel;
    private ActionPanel actionPanel;
    private int i = -1, selectedInstance = 0;

    private ArrayList<Shape> shape = new ArrayList<Shape>();

    public ShapeMouseHandler(Model model) {
        this.model = model;
    }

    public void mousePressed(MouseEvent e) {           
           dragStartX = e.getX();
           dragStartY = e.getY();
        if (model.getAction() == Model.Actions.DRAW) {
            startX = e.getX();
            startY = e.getY();
            i++;
            shape.add(model.createShape(i));
            if (shape.get(i) != null) {
                shape.get(i).setFillColor(colorPanel.getFillColorSelection());
                shape.get(i).setLineColor(colorPanel.getLineColorSelection());
                shape.get(i).setX(e.getX());
                shape.get(i).setY(e.getY());
                if (shape.get(i) instanceof Line) { 
                    ((Line)shape.get(i)).setX2(e.getX());
                    ((Line)shape.get(i)).setY2(e.getY());
                }
            }
        }

        if (model.getAction() == Model.Actions.MOVE) {
            selectedInstance = model.getSelectedInstance(e.getX(), e.getY());
            System.out.println("Selected Index :-------------  " + selectedInstance);

            if (selectedInstance != -1) {
                oldColor = shape.get(selectedInstance).getLineColorType();
                shape.get(selectedInstance).setLineColor(Model.MyColors.RED.toString());
                startX = shape.get(selectedInstance).getX() - e.getX();
                startY = shape.get(selectedInstance).getY() - e.getY();

            }

        }
        if (model.getAction() == Model.Actions.RESIZE) {
            selectedInstance = model.getSelectedInstance(e.getX(), e.getY());
            if (selectedInstance != -1) {
                startX=shape.get(selectedInstance).getX();
                startY=shape.get(selectedInstance).getY();
                oldColor = shape.get(selectedInstance).getLineColorType();
                shape.get(selectedInstance).setLineColor(Model.MyColors.RED.toString());
            }
        }
        if (model.getAction() == Model.Actions.REMOVE) {

            selectedInstance = model.getSelectedInstance(e.getX(), e.getY());
            if (selectedInstance != -1) {
                model.removeShape(selectedInstance);
                shape.remove(selectedInstance);
            }

        }
        if (model.getAction() == Model.Actions.CHANGE) {
            selectedInstance = model.getSelectedInstance(e.getX(), e.getY());
            if (selectedInstance != -1) {
                Shape selected = model.getShapes().get(selectedInstance);
                startX = selected.getX();
                startY = selected.getY();
                int width = selected.getWidth();
                int height = selected.getHeight();
                if (shape.get(selectedInstance) != null) {
                     shape.get(selectedInstance).setLineColor(colorPanel.getLineColorSelection());
                    shape.get(selectedInstance).setFillColor(colorPanel.getFillColorSelection());
                    if (shape.get(selectedInstance) instanceof Rectangle) {
                        ((Rectangle) shape.get(selectedInstance)).setFill(actionPanel.isFillChecked());
                    }
                }
            }
        }
        model.getContainer().repaint();
    }

    public void mouseDragged(MouseEvent e) {        

            if (shape != null) {
                if (model.getAction() == Model.Actions.DRAW) {
                    if (shape.get(i) instanceof Line) {
                        ((Line) shape.get(i)).setX2(e.getX());
                        ((Line) shape.get(i)).setY2(e.getY());
                    } else {
                        shape.get(i).setX(Math.min(startX, e.getX()));
                        shape.get(i).setY(Math.min(startY, e.getY()));

                        if (true) {

                            (shape.get(i)).setWidth(Math.abs(startX - e.getX()));
                            (shape.get(i)).setHeight(Math.abs(startY - e.getY()));
                        }
                    }
                }

            if (model.getAction() == Model.Actions.MOVE) {
                if (selectedInstance != -1) {
                    shape.get(selectedInstance).setX(e.getX() + startX);
                    shape.get(selectedInstance).setY(e.getY() + startY);
                }
                if (shape.get(i) instanceof Line) {

                    int diffX = e.getX() - dragStartX;
                    dragStartX = e.getX();
                    int diffY = e.getY() - dragStartY;
                    dragStartY = e.getY();

                    ((Line) shape.get(i)).moveLineX(diffX);
                    ((Line) shape.get(i)).moveLineY(diffY);

                }
                if (model.getAction() == Model.Actions.RESIZE) {
                    if (shape.get(i) instanceof Line) { 
                        System.out.println("Resizing line");
                        ((Line) shape.get(i)).setX2(e.getX());
                        ((Line) shape.get(i)).setY2(e.getY());
                    } else {
                        shape.get(selectedInstance).setX(Math.min(startX, e.getX()));
                        shape.get(selectedInstance).setY(Math.min(startY, e.getY()));


                        shape.get(selectedInstance).setWidth(Math.abs(startX - e.getX()));
                        shape.get(selectedInstance).setHeight(Math.abs(startY - e.getY()));
                    }
                    //}
                }
            if (model.getAction() == Model.Actions.CHANGE && selectedInstance != -1) {

            }
        }

        model.getContainer().repaint();

    }
    }

    public void mouseReleased(MouseEvent e) {
        if (selectedInstance != -1 && (model.getAction() == Model.Actions.MOVE || model.getAction() == Model.Actions.RESIZE)) {
            shape.get(selectedInstance).setLineColor(oldColor);
        }
    }

    public void reset() {                      

        i = -1;
        shape.clear();
    }

    public void setPanels(MainPanel.ColorPanel colorPanel, ActionPanel actionPanel) {
        this.colorPanel = colorPanel;
        this.actionPanel = actionPanel;
    }
}
4

1 回答 1

0

您的代码过于复杂。您可以使用AffineTransform 的 createTransformedShape 方法以通用方式调整任何形状的大小

请参阅此示例:如何在 Java 中旋转椭圆形状?

于 2013-03-02T21:34:10.947 回答