无法弄清楚为什么我的线条形状不会重新调整大小

时间:2013-03-01 15:15:44

标签: java drawing awt java-2d

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

这是班级:

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

1 个答案:

答案 0 :(得分:0)

您的代码过于复杂。您可以使用createTransformedShape method of an AffineTransform

以通用方式调整任何形状的大小

请参阅此示例:How to have rotated Ellipse shape in Java?