鼠标拖动和绘图

发布于 2024-08-06 02:02:18 字数 1850 浏览 3 评论 0原文

我在显示所有三角形时遇到问题。我使用鼠标拖动绘制一个三角形。每次我画一个新的三角形,以前的三角形就会消失。怎么才能让三角形留下来,这样绘图面板上就会有很多三角形呢?

.....
private class PaintSurface extends JComponent {
   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();
      }

      public void mouseReleased(MouseEvent e) {
         Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());          
         shapes.add(r); 
         if (startDrag.x > endDrag.x)
            midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
         else 
            midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
         int[] xs = { startDrag.x, endDrag.x, midPoint.x };
         int[] ys = { startDrag.y, startDrag.y, midPoint.y };          
         triangle = new Polygon(xs, ys, 3);               
         startDrag = null;
         endDrag = null;
         repaint();
        }
     });

     this.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
           endDrag = new Point(e.getX(), e.getY());
           repaint();
        }
     });
  }

  public void paint(Graphics g) {
     Graphics2D g2 = (Graphics2D) g;
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     paintBackground(g2);
     Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
     int colorIndex = 0;
     g2.setStroke(new BasicStroke(1));
     g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
     g2.fillPolygon(triangle);        
     }
}

I have problem with displaying all the triangles. I draw a triangle using mouse drag. Everytimes I draw a new triangle, the previous triangle disappeared. How can I make the triangles stay, so there will be many triangles on the drawing panel?

.....
private class PaintSurface extends JComponent {
   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();
      }

      public void mouseReleased(MouseEvent e) {
         Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());          
         shapes.add(r); 
         if (startDrag.x > endDrag.x)
            midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
         else 
            midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
         int[] xs = { startDrag.x, endDrag.x, midPoint.x };
         int[] ys = { startDrag.y, startDrag.y, midPoint.y };          
         triangle = new Polygon(xs, ys, 3);               
         startDrag = null;
         endDrag = null;
         repaint();
        }
     });

     this.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
           endDrag = new Point(e.getX(), e.getY());
           repaint();
        }
     });
  }

  public void paint(Graphics g) {
     Graphics2D g2 = (Graphics2D) g;
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     paintBackground(g2);
     Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
     int colorIndex = 0;
     g2.setStroke(new BasicStroke(1));
     g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
     g2.fillPolygon(triangle);        
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无所的.畏惧 2024-08-13 02:02:18

您在屏幕上看到的就是您在绘图功能中绘制的。目前,您只在三角形变量中存储一个三角形,不断替换和绘制它。

您需要的是存储三角形列表,并每次将一个新三角形添加到 mouseReleased 的列表中。以下是要更改的内容:

private class PaintSurface extends JComponent {
   ...
   //Polygon triangle;
   List<Polygon> triangles = new LinkedList<Polygon>();
   ...

   public PaintSurface() {

      public void mouseReleased(MouseEvent e) {
         ...
         //triangle = new Polygon(xs, ys, 3);
         triangles.add( new Polygon(xs, ys, 3); );
         ...
        }
     });
     ...
  }

  public void paint(Graphics g) {
     ...
     //g2.fillPolygon(triangle);
     for (Polygon triangle : triangles) g2.fillPolygon(triangle);
     ...
   }
}

What you see on the screen is what you draw in your paint function. Currently you only store one triangle in your triangle variable constantly replacing and drawing it.

What you need is to store a list of triangles, and add a new one each time to the list in mouseReleased. Here is what to be changed:

private class PaintSurface extends JComponent {
   ...
   //Polygon triangle;
   List<Polygon> triangles = new LinkedList<Polygon>();
   ...

   public PaintSurface() {

      public void mouseReleased(MouseEvent e) {
         ...
         //triangle = new Polygon(xs, ys, 3);
         triangles.add( new Polygon(xs, ys, 3); );
         ...
        }
     });
     ...
  }

  public void paint(Graphics g) {
     ...
     //g2.fillPolygon(triangle);
     for (Polygon triangle : triangles) g2.fillPolygon(triangle);
     ...
   }
}
永不分离 2024-08-13 02:02:18
Polygon triangle;

您保留对总共一个三角形对象的引用,因此每次重新绘制画布时,您都会绘制这个多边形。

几个解决方案:

  • 创建一个 Polygon 对象列表,并在调用 paint 时绘制它们。您的鼠标侦听器将向列表中添加一个新条目。
  • 将多边形绘制到 BufferedImage然后 在paint方法中绘制图像。如果您计划绘制许多三角形,这可能是更好的选择。
Polygon triangle;

You keep a reference to a grand total of one triangle objects, so every time you repaint the canvas you'll draw this one polygon.

A couple of solutions:

  • Create a list of Polygon objects and draw them all when paint is called. Your mouse listener would add a new entry to the list.
  • Draw your polygons onto a BufferedImage and then draw the image in the paint method. This would probably be the better option if you plan on drawing many triangles.
疯到世界奔溃 2024-08-13 02:02:18

不是很奇怪。您有一个三角形对象,一旦开始绘制新三角形,就会覆盖该对象。

创建一个三角形数组/列表,并让您的绘图例程绘制它。

我的java修改:

.....
  private class PaintSurface extends JComponent {
     Point startDrag, endDrag, midPoint;
     List triangles = new ArrayList();
     public PaintSurface() {
        this.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
           startDrag = new Point(e.getX(), e.getY());
           endDrag = startDrag;
           repaint();
  }

  public void mouseReleased(MouseEvent e) {
     Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());          
     shapes.add(r); 
     if (startDrag.x > endDrag.x)
        midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
     else 
        midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
     int[] xs = { startDrag.x, endDrag.x, midPoint.x };
     int[] ys = { startDrag.y, startDrag.y, midPoint.y };          
     Polygon triangle = new Polygon(xs, ys, 3);            
     traingles.add(triangle);
     startDrag = null;
     endDrag = null;
     repaint();
    }
 });

 this.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
       endDrag = new Point(e.getX(), e.getY());
       repaint();
    }
 });
  }

  public void paint(Graphics g) {
       Graphics2D g2 = (Graphics2D) g;
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       paintBackground(g2);
       Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
       int colorIndex = 0;
       g2.setStroke(new BasicStroke(1));
       g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
       Iterator iter = triangles.iterator();
       while( iter.hasNext() ) {
          g2.fillPolygon((Polygon)iter.next());        
       }
  }
  }

Not very strange. You have one triangle object which you overwrite as soon as you start drawing a new triangle.

Create an array/list of triangles and have your drawing routine draw that.

My java modification:

.....
  private class PaintSurface extends JComponent {
     Point startDrag, endDrag, midPoint;
     List triangles = new ArrayList();
     public PaintSurface() {
        this.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
           startDrag = new Point(e.getX(), e.getY());
           endDrag = startDrag;
           repaint();
  }

  public void mouseReleased(MouseEvent e) {
     Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());          
     shapes.add(r); 
     if (startDrag.x > endDrag.x)
        midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY());
     else 
        midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
     int[] xs = { startDrag.x, endDrag.x, midPoint.x };
     int[] ys = { startDrag.y, startDrag.y, midPoint.y };          
     Polygon triangle = new Polygon(xs, ys, 3);            
     traingles.add(triangle);
     startDrag = null;
     endDrag = null;
     repaint();
    }
 });

 this.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
       endDrag = new Point(e.getX(), e.getY());
       repaint();
    }
 });
  }

  public void paint(Graphics g) {
       Graphics2D g2 = (Graphics2D) g;
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       paintBackground(g2);
       Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
       int colorIndex = 0;
       g2.setStroke(new BasicStroke(1));
       g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
       Iterator iter = triangles.iterator();
       while( iter.hasNext() ) {
          g2.fillPolygon((Polygon)iter.next());        
       }
  }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文