调整形状几乎可以工作

时间:2012-12-19 13:42:43

标签: java swing

我正在尝试完成一个形状任务的大小调整我几乎得到了我想要的结果,只有在鼠标更改为调整大小光标后拖动才会先绘制另一个相同类型的形状并调整第二个绘制形状的大小。导致这样的图片:

enter image description here

这是我的相关代码:

  @Override
public void mouseMoved(MouseEvent e) {

    if (e.isControlDown()) {
        updateShapeUnderMouse(e.getX(), e.getY());
    } // deals with drawing shape if control button held

    //deals with identifying shape to resize
        int x = e.getX();
         int y = e.getY();

    for (int i = myShapes.size() - 1; i >= 0; i--) {
        Shape s = (Shape) myShapes.get(i);
            if (s.isedgePoint(x, y)) {
            ShapetoResize = s;
            setCursor(crnw);
            prevDragX = x;
            prevDragY = y;
            return;
        }
    }

@Override
public void mouseDragged(MouseEvent event) {

         if (event.isControlDown()) {
            if (shapeUnderMouse != null) {
            shapeUnderMouse.setXPos(event.getX());
            shapeUnderMouse.setYPos(event.getY());
            repaint();
        }
    } // deals with moving the shape

  //deals with identifying and resizing shape
    int x = event.getX();
    int y = event.getY();

     if (ShapetoResize != null) {
         if (ShapetoResize instanceof Square) {
            ShapetoResize.resizeSE(x - prevDragX, y - prevDragY);
        } else if (ShapetoResize instanceof Rectangle) { // SAME CODE FOR EACH SHAPE
   }

  repaint();
    }
}

知道可能会发生什么吗?

public ArrayList<Shape> myShapes = new ArrayList();


@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    int length = myShapes.size();

    for (int i = 0; i < length; i++) {

        myShapes.get(i).paint(g);
    }

}

2 个答案:

答案 0 :(得分:3)

如果没有看到代码的这一部分,那只是猜测,但我认为你在ShapetoResize.resizeSE(int x, int y);方法中创建了一个新的形状,从而创建了两个形状。

我假设它是您自己的方法,因为resizeSE或接口Rectangle(两个内置形状)上没有Shape可用。当然,接口paint上没有Shape方法,因此您可能也使用自定义接口(由于您没有为此Shape接口提供代码,因此会令人困惑)。如果您发布这些方法的代码,我们可以确认。

以下是以工作方式(和SSCCE)将代码放在一起的示例。我仅限于矩形(因为它们是Shape中唯一内置的)并且剥离了许多与调整大小无关的东西。如果您仍然遇到问题,请尝试使用此示例重新创建问题:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

public class ShapeResizer extends Box{
    Dimension preferredSize = new Dimension(400,300);
    public ArrayList<Shape> myShapes = new ArrayList();
    //Shape that's targeted for editing
    Shape currentShape;

    public ShapeResizer(){
        super(BoxLayout.X_AXIS);

        //Shapes (because I don't want to make write code for the user to make shapes)
        myShapes.add(new Rectangle(100, 100, 20, 20));
        myShapes.add(new Rectangle(200, 200, 30, 30));

        addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(MouseEvent e) {

                //deals with identifying shape to resize
                int x = e.getX();
                int y = e.getY();

                boolean foundShape = false;
                for (int i = myShapes.size() - 1; i >= 0; i--) {
                    Shape s = (Shape) myShapes.get(i);
                    if (s.contains(e.getPoint())) {
                        //We found a shape to target
                        currentShape = s;
                        foundShape = true;
                    }
                }

                if(!foundShape){
                    //Reset the shape and cursor only if needed
                    if(currentShape != null){
                        currentShape = null;
                        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    }
                } else {
                    setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
                }

            }

            @Override
            public void mouseDragged(MouseEvent event) {
                if (currentShape != null) {
                    resizeShape(currentShape, event.getPoint());
                }
                repaint();
            }
        });
    }

    public void resizeShape(Shape s, Point p){
        if(s instanceof Rectangle){
            Rectangle r = (Rectangle)s;
            r.setSize(p.x - r.x, p.y - r.y);
        }
    }

    public void drawShape(Graphics g, Shape s){
        if(s instanceof Rectangle){
            Rectangle r = (Rectangle)s;
            g.drawRect(r.x, r.y, r.width, r.height);
        }
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        int length = myShapes.size();

        for (int i = 0; i < length; i++) {
            drawShape(g, myShapes.get(i));
        }

    }

    public Dimension getPreferredSize(){
        return preferredSize;
    }   

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new ShapeResizer());
        frame.validate();
        frame.pack();
        frame.setVisible(true);
    }
}

答案 1 :(得分:1)

添加形状背后的逻辑是什么?是否通过左键单击绘图画布添加了形状?如果是这样,你的问题可能是在点击时添加了一个形状,那就是正在调整大小的形状。您可以在拖动对象后通过中断执行来调试它。您的列表将包含两个形状而不是一个。