簡體   English   中英

在 canvas 上使用鼠標繪制油漆的有效方法

[英]Efficient way to draw paint using mouse on canvas

我有Brush class

 final class Brush
 {
  private final int size;
  private final Color color;
  private final Ellipse2D.Double blob=new Ellipse2D.Double();

  private Brush(int size,Color color)
  {
   this.size=size;
   this.color=color;
  }

  void paint(Graphics2D g2d,Point location)
  {
   g2d.setColor(color);    
   blob.setFrame(location.x-(size/2.0),location.y-(size/2.0),size,size);//Translate ecllipse so that the centre of it's bounding box is exactly at the cursor location for more accurate blobs  
   g2d.fill(blob);
  }
 }

我有一個Blob class 跟蹤用戶當前的畫筆設置以及用戶之前拖動鼠標的位置,以便記住在那里繪制一個 blob。

final class Blob
{
 final Brush brush;
 final Point location;

 private Blob(Brush brush,Point location)
 {
  this.brush=brush;
  this.location=location;
 }

 private void paint(Graphics2D g){brush.paint(g,location);}
}

最后我的繪畫邏輯非常簡單。

每當用戶拖動鼠標時,使用當前畫筆設置在該當前位置添加一個 blob,並在paint()內部循環遍歷所有 blob 並重繪它們。

final class Painter extends Canvas
{
 private Brush brush=new Brush(5,Color.red);//Can Change
 private final ArrayList<Blob> blobs=new ArrayList(); 

 private Painter(){addMouseMotionListener(new Dragger());}

 @Override
 public void paint(Graphics g)
 {
  super.paint(g);

  blobs.forEach(blob->blob.paint(g));
 }

 private final class Dragger extends MouseAdapter
 {
  @Override
  public void mouseDragged(MouseEvent m)
  {
   blobs.add(brush,m.getPoint());

   repaint();
  }    
 }
}

您已經可以在此處看到問題所在。 列表的大小呈指數增長,我的應用程序迅速變慢。 有沒有更有效的方法來做到這一點?

更有效的方法是使用 BufferedImage 進行繪圖,而不是在 paintComponent 中繪制 BufferedImage

取自PaintArea的代碼:

public void paintComponent(Graphics g) {
    if (mSizeChanged) {
        handleResize();
    }
    g.drawImage(mImg, 0, 0, null);
}

protected class MListener extends MouseAdapter implements MouseMotionListener {
    Point mLastPoint;
    public void mouseDragged(MouseEvent me) {
        Graphics g = mImg.getGraphics();
        if ((me.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
            g.setColor(mColor1);
        } else {
            g.setColor(mColor2);
            
        }
        Point p = me.getPoint();
        if (mLastPoint == null) {
            g.fillOval(p.x - mBrushSize / 2, p.y - mBrushSize / 2, mBrushSize, mBrushSize);
            //g.drawLine(p.x, p.y, p.x, p.y);
        }
        else {
            g.drawLine(mLastPoint.x, mLastPoint.y, p.x, p.y);
            //g.fillOval(p.x - mBrushSize / 2, p.y - mBrushSize / 2, mBrushSize, mBrushSize);
            double angle = MathUtils.angle(mLastPoint, p);
            if (angle < 0) {
                angle += 2 * Math.PI;
            }
            @SuppressWarnings("unused")
            double distance = MathUtils.distance(mLastPoint, p) * 1.5;
            if (angle < Math.PI / 4 || angle > 7 * Math.PI / 4 || Math.abs(Math.PI - angle) < Math.PI / 4) {
                for (int i = 0; i < mBrushSize / 2; i ++) {
                    g.drawLine(mLastPoint.x, mLastPoint.y + i, p.x, p.y + i);
                    g.drawLine(mLastPoint.x, mLastPoint.y - i, p.x, p.y - i);
                }
            }
            else {
                for (int i = 0; i < mBrushSize / 2; i ++) {
                    g.drawLine(mLastPoint.x + i, mLastPoint.y, p.x + i, p.y);
                    g.drawLine(mLastPoint.x  - i, mLastPoint.y, p.x - i, p.y);
                }                   
            }
        }
        mLastPoint = p;
        g.dispose();
        repaint();
    }
    
    public void mouseMoved(MouseEvent me) {}
    
    public void mouseReleased(MouseEvent me) {
        mLastPoint = null;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM