使用鼠标监听器移动绘制的形状

时间:2014-11-06 15:56:00

标签: java mouseevent graphics2d mouselistener

处理一个简单的Paint java项目,它需要绘制形状,例如(line,circle,rectangle,........) 我正在尝试编写可以移动,删除和调整所选形状大小的方法。 我的代码应该是正确的,因为我已经在另一个有一个绘制矩形的项目中测试它。 我的paintComponent方法选择形状并将它们添加到ArrayList

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;


        for (ShapeItem item :shapes) {
            if (!(item.getShape() instanceof Line2D)){
                g2d.setColor(item.getColor());
                g2d.fill(item.getShape());
            }
            else {
//              g2d.setStroke(new BasicStroke(10));
                g2d.setColor(item.getColor());
                g2d.draw(item.getShape());
            }

        }

        if (selectedShape != null){
              g2d.setStroke ( new BasicStroke(1f, 
                       BasicStroke.CAP_ROUND, 
                       BasicStroke.JOIN_ROUND, 
                       1f, 
                       new float[] {2f}, 
                       0f) );
              if (selectedShape.getShape() instanceof Ellipse2D) {
                  Rectangle rect =  selectedShape.getShape().getBounds();

                  g2d.draw(rect);
              }

              else if (selectedShape.getShape() instanceof Rectangle2D) {
                  Rectangle2D dotted = new Rectangle2D.Double((int)((Rectangle2D) selectedShape.getShape()).getX() - 6, 
                                    (int)((Rectangle2D) selectedShape.getShape()).getY() - 6, 
                                    (int)((Rectangle2D) selectedShape.getShape()).getWidth() + 12, 
                                    (int)((Rectangle2D) selectedShape.getShape()).getHeight() + 12);  
                  g2d.draw(dotted);
              }

              else if (selectedShape.getShape() instanceof Line2D) {
                  Rectangle rect =  selectedShape.getShape().getBounds();

                  g2d.draw(rect); 
              }

              else if (selectedShape.getShape() instanceof Polygon) {
                  Rectangle rect =  selectedShape.getShape().getBounds();

                  g2d.draw(rect); 
              }
        }        
    }

移动形状的类

package draw;

import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
//import java.awt.event.MouseListener;
//import java.awt.event.MouseMotionListener;
//import java.awt.geom.Ellipse2D;
//import java.util.ArrayList;

import javax.swing.JComponent;

public class DynamicShapes extends JComponent{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    MoveShapes move = new MoveShapes();
    public DynamicShapes(){
        addMouseListener(move);
        addMouseMotionListener(move);
    }

    class MoveShapes extends MouseAdapter{
        private int x;
        private int y;
        boolean pressed;
        boolean dragged;
        DrawingPanel d = new DrawingPanel();
        @Override
        public void mousePressed(MouseEvent e){
            super.mousePressed(e);
             pressed = false;
            for (ShapeItem s : d.getShapes()){
                if(s.getShape().contains(e.getX() , e.getY()) ){
                    pressed = true;
                    System.out.println("shape is pressed");
                }
            }
        }
        @Override
        public void mouseDragged(MouseEvent e){
            super.mouseDragged(e);

            if(pressed){

                int dx = e.getX()-x;
                int dy = e.getY()-y;
                for (ShapeItem s : d.getShapes()){
                    Rectangle rect = s.getShape().getBounds();
                    if(s.getShape().getBounds2D().contains(x, y)){
                        rect.x += dx;
                        rect.y += dy;
                        repaint();
                    }
                    x += dx;
                    y += dy;
                }
            }
        }
    }


}

如果有人可以提供帮助,我不会对洞图形和鼠标事件有很深的了解,因为这是我的第一次:)

0 个答案:

没有答案