使用鼠标拖动绘制三角形(如何使用鼠标拖动移动先前绘制的三角形) [英] Draw a triangle using mouse drag (how can I move the previous drawn triangle using mouse drag)

查看:142
本文介绍了使用鼠标拖动绘制三角形(如何使用鼠标拖动移动先前绘制的三角形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to move a triangle to a new location using mouse drag (which was previous drawn using mouse drag)?

...
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方法仅使用鼠标拖动绘制一个新三角形:-( */

/* I dont know how to move (drag) the whole triangle to new location and later delete the previous drawn triangle. The mouseDragged method only draw a new triangle using mouse drag :-( */

    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

推荐答案

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

when you start to drag you have to detect if your current mouse location is on one of the existing Polygons, also mark the starting location

在这种情况下,您不添加新的多边形,而是将移动的数量添加到现有多边形的不同点并重新粉刷

When it is you dont add a new polygon, but you add the amount moved to the different points of the existing polygon and repaint

这篇关于使用鼠标拖动绘制三角形(如何使用鼠标拖动移动先前绘制的三角形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆