1

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

.....
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);        
     }
}
4

3 回答 3

3

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

您需要存储一个三角形列表,并在 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);
     ...
   }
}
于 2009-09-26T11:06:30.737 回答
1
Polygon triangle;

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

几个解决方案:

  • 创建一个Polygon对象列表并在调用时将它们全部绘制出来paint。您的鼠标侦听器会在列表中添加一个新条目。
  • 将多边形绘制到BufferedImage上,然后在 paint 方法中绘制图像。如果您计划绘制许多三角形,这可能是更好的选择。
于 2009-09-26T11:07:21.733 回答
1

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

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

我的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());        
       }
  }
  }
于 2009-09-26T11:07:36.797 回答