【问题标题】:Draw a triangle using mouse drag (how can I move the previous drawn triangle using mouse drag)使用鼠标拖动绘制三角形(如何使用鼠标拖动移动先前绘制的三角形)
【发布时间】:2009-09-28 03:58:44
【问题描述】:

如何使用鼠标拖动(之前使用鼠标拖动绘制)将三角形移动到新位置?

...
java.util.List<Polygon> triangles = new LinkedList<Polygon>();
Point startDrag, endDrag, midPoint;
Polygon triangle;
...
public PaintSurface() {     
  this.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent e) {
    startDrag = new Point(e.getX(), e.getY());
    endDrag = startDrag;
    repaint();
  }//end mousePressed   

public void mouseReleased(MouseEvent e) {
...
  int[] xs = { startDrag.x, endDrag.x, midPoint.x };
  int[] ys = { startDrag.y, startDrag.y, midPoint.y };      
  triangles.add( new Polygon(xs, ys,3));                    
  startDrag = null;
  endDrag   = null;
  repaint();
 }//end mouseReleased   
...


 });//end addMouseListener

  this.addMouseMotionListener(new MouseMotionAdapter() {

/* 我不知道如何将整个三角形移动(拖动)到新位置,然后删除之前绘制的三角形。 mouseDragged 方法仅使用鼠标拖动绘制一个新三角形:-( */

    public void mouseDragged(MouseEvent e) {
        endDrag = new Point(e.getX(), e.getY());
        repaint();
     }//end mouseDragged
        }//end paintSurface       

         //Draw triangles
         public void paint(Graphics g) {
           Graphics2D g2 = (Graphics2D) g;
           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

           //draw the thickness of the line
           g2.setStroke(new BasicStroke(1));
           g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));        
           g2.setPaint(Color.black);//set the triangle color 
           for (Polygon triangle : triangles)  g2.drawPolygon(triangle);
             if (startDrag != null && endDrag != null) {
                g2.setPaint(Color.red);
                g2.drawPolygon(triangle);   
             }   
          }//end paint       

              }//end private class PaintSurface

【问题讨论】:

    标签: java mouse drag


    【解决方案1】:

    当您开始拖动时,您必须检测当前鼠标位置是否在现有多边形之一上,同时标记起始位置

    当你不添加新多边形时,你添加移动到现有多边形不同点的数量并重新绘制

    【讨论】:

    • 不,它的拖动起点和拖动终点之间的差异说你从 5,10 拖动到 15,25 你已经移动了 10 和 15 所以你将 10 添加到所有 x三角形 en 15 到所有 y 的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2016-03-25
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    相关资源
    最近更新 更多