2013-03-01 59 views
-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; 
    } 
} 
+0

你在哪裏設置的行動,'model.getAction()'將返回? – RudolphEst 2013-03-01 15:22:47

+0

爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-03-02 01:58:40

回答

相關問題