【问题标题】:Java Graphics2D to erase to a alpha backgroundJava Graphics2D 擦除到 alpha 背景
【发布时间】:2013-02-27 16:00:54
【问题描述】:

我正在制作一个应用程序,它有一个绘图板,您可以在其中使用鼠标进行绘图,它在 BUfferedImage 中的标签上绘制。我现在要实现的是橡皮擦,问题是我找不到任何帮助将橡皮擦清除到 alpha 背景的地方。 (我无法定义颜色背景,因为用户可以将背景更改为他想要的任何图像)。总结一下:

  • 如何用 alpha 像素擦除/覆盖 Graphics2D 像素?我发现的方法是使用 clearRect 但您需要指定背景颜色。

以下是我的 DrawBoard 类,它包含要绘制的所有内容。

public class DrawBoard extends JPanel implements MouseListener, MouseMotionListener{


public JLabel status;
private JLabel imgLabel; // this is where the drawing happens
public Point pstart, pfinish;
private List<Point> points = new ArrayList<Point>();
private List<BufferedImage> lines = new ArrayList<BufferedImage>();

private static final int BI_WIDTH = 1024;
private static final int BI_HEIGHT = 800;
private static int STROKESIZE = 7;

private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT,
        BufferedImage.TYPE_INT_ARGB);

public Color currentColor;

public static boolean eraser = false;

private int xX1, yY1;

public DrawBoard(){  

   Graphics2D g2d = bImage.createGraphics();
   g2d.dispose();

    Dimension size = getPreferredSize();
    size.setSize(1024,800); //w, h
    setPreferredSize(size);
    //status =  new JLabel("default");
    //add(status, BorderLayout.SOUTH);
    addMouseListener(this);
    addMouseMotionListener(this);  


     imgLabel = new JLabel(new ImageIcon(bImage)) {
     @Override
     protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        paintInLabel(g);
     }
   };
     imgLabel.setOpaque(false);
     setOpaque(false);
      add(imgLabel, BorderLayout.CENTER);

}

private void paintInLabel(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  g2d.setColor(getColor()); // this colour is when mouse is pressed
  g2d.setStroke(new BasicStroke(STROKESIZE));
  if (points.size() < 2) {
     return;
  }
  for (int i = 1; i < points.size(); i++) {
     int x1 = points.get(i - 1).x;
     int y1 = points.get(i - 1).y;
     int x2 = points.get(i).x;
     int y2 = points.get(i).y;
     g2d.drawLine(x1, y1, x2, y2);
  }
 }


@Override
public void mouseExited(MouseEvent e){  
}

@Override
public void mouseEntered(MouseEvent e){  
}

@Override
public void mouseMoved(MouseEvent e){   
}

// Where the drawing happens
@Override
public void mousePressed(MouseEvent e) {
    //status.setText("you pressed down the mouse");
    xX1 = e.getX();
    yY1 = e.getY();


        points.add(e.getPoint());
}

@Override
public void mouseDragged(MouseEvent e) {
  //status.setText("you draged the mouse");
        points.add(e.getPoint());
        imgLabel.repaint();

}

@Override
public void mouseReleased(MouseEvent e) {
    //status.setText("you release the mouse click");
    Graphics2D g2d = bImage.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(getColor()); // this is the final colour
    g2d.setStroke(new BasicStroke(STROKESIZE));


     if (points.size() >= 2) {
        for (int i = 1; i < points.size(); i++) {
           int x1 = points.get(i - 1).x;
           int y1 = points.get(i - 1).y;
           int x2 = points.get(i).x;
           int y2 = points.get(i).y;
           g2d.drawLine(x1, y1, x2, y2);
        }
     }
     g2d.dispose();

     points.clear();
     imgLabel.repaint();

}
// End of where the drawing happens

public void clearDrawBoard() {

}

private Color getColor() {

    return ColourToolbar.selectedColor;
}

private void setColor(Color col){

    this.currentColor = col;
}

@Override
public void mouseClicked(MouseEvent e) {
    //throw new UnsupportedOperationException("Not supported yet.");
}

}

【问题讨论】:

  • 你是否已经尝试过g2d.setColor(new Color(0, 0, 0, 0)) 然后'fillRect'?

标签: java user-interface graphics2d java-2d erase


【解决方案1】:

您应该在绘制矩形之前set a custom Composite for your Graphics2D,特别是AlphaComposite.Clear。完成后不要忘记将合成重置为默认值 (SRC_OVER),因为相同的 Graphics 对象将被重用于绘制其他组件。

【讨论】:

  • 这确实成功了,自从我昨晚读到你不能用 alpha 像素覆盖,我失去了希望,但现在它就像一个魅力!非常感谢,希望寻找这个的人能从这个答案中得到帮助!
  • 答案可以使用一些示例代码,从这里开始不清楚如何重置为 SRC_OVER,因为 AlphaComposite.Clear 和 SRC_OVER 是不同的类型。您实际上想将其设置回 AlphaComposite.SrcOver。
猜你喜欢
  • 2015-05-08
  • 2013-05-01
  • 2015-11-13
  • 1970-01-01
  • 2020-09-02
  • 2011-07-09
  • 1970-01-01
  • 2010-10-01
  • 2016-08-19
相关资源
最近更新 更多