-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 に答える