AlphaComposite.Clear 的错误认为它的背景是黑色的

Bug with AlphaComposite.Clear it thinks its background is black

我正在开发一个具有透明缓冲图像的绘画应用程序。我需要执行的任务之一是实现 bucketFill 函数。我已经成功地这样做了但是我不能用黑色做桶填充操作,因为 onClick 事件 returns 一个点,当你检查这个点的颜色时它的黑色......程序认为它已经全黑并且 returns。它适用于所有其他颜色。知道如何绕过这个问题。

在它下面是简化的代码以演示我遇到的问题。你实际上可以 运行 它并且它会起作用。

Paint Main 就在这里

public class Paint extends JFrame {
private final int WIDTH = 200;
private final int HEIGHT = 200;

private final Canvas canvas;
private final RightPanel rightPanel;

public Paint() throws UnknownHostException, IOException {
    this.setSize(WIDTH, HEIGHT);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("Paint");
    this.setLocationRelativeTo(null);

    this.canvas = new Canvas(this);
    this.rightPanel = new RightPanel(canvas);

    this.canvas.setListeners(new BucketFillListener(canvas));
    this.add(canvas, BorderLayout.CENTER);
    this.add(rightPanel, BorderLayout.WEST);

    this.setVisible(true);
}

public static void main(String[] args) throws UnknownHostException, IOException {
    new Paint();
}

public int getWIDTH() {
    return WIDTH;
}

public int getHEIGHT() {
    return HEIGHT;
}

public Canvas getCanvas() {
    return canvas;
}

public RightPanel getRightPanel() {
    return rightPanel;
}

}

Canvas 保存缓冲图像

public class Canvas extends JComponent {

private final BufferedImage image;
private BucketFillListener listener;

private final Settings settings;

private final int imageWidth = 800;
private final int imageHeight = 800;
private final Paint paint;

public Canvas(Paint paint) {
    this.paint = paint;

    image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB_PRE);

    setClearBackground(image);

    this.setSize(imageWidth, imageHeight);
    this.settings = new Settings();

}

public BucketFillListener getListener() {
    return listener;
}

public Settings getSettings() {
    return settings;
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawImage(image, 0, 0, null);

    paint.repaint();
}

public void setClearBackground(BufferedImage image) {
    final BufferedImage img = image;
    final Graphics2D g2 = (Graphics2D) img.getGraphics();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
    // g2.setColor(Color.white); // sets white background
    g2.fillRect(0, 0, this.getWidth(), this.getHeight());
}

public void setListeners(BucketFillListener listener) {
    if (this.listener != null) {
        this.removeMouseListener(this.listener);
        this.removeMouseMotionListener(this.listener);
    }

    this.listener = listener;
    this.addMouseMotionListener(this.listener);
    this.addMouseListener(this.listener);
}

public int getImageWidth() {
    return imageWidth;
}

public int getImageHeight() {
    return imageHeight;
}

public Paint getPaint() {
    return paint;
}

public BufferedImage getImage() {
    // TODO Auto-generated method stub
    return image;
}

} 桶填充侦听器执行实际的桶填充

public class BucketFillListener implements MouseListener, MouseMotionListener {

private final Stack<Point> stack;
private final Canvas canvas;
private int x, y;

public BucketFillListener(Canvas canvas) {
    // TODO Auto-generated constructor stub
    this.stack = new Stack<Point>();
    this.canvas = canvas;
}

@Override
public void mouseClicked(MouseEvent e) {
    final BufferedImage img = canvas.getImage();
    final Graphics2D g2 = (Graphics2D) img.getGraphics();
    g2.setColor(canvas.getSettings().getColor());
    x = e.getX();
    y = e.getY();

    final Color initColor = new Color(img.getRGB(x, y));
        System.out.println(initColor);
if (initColor.equals(g2.getColor())) {
            return;
        }
    boolean left, right;
    int tempY;

    stack.push(new Point(x, y));
    while (!stack.isEmpty()) {
        final Point p = stack.pop();
        final int activeX = (int) p.getX();
        final int activeY = (int) p.getY();

        tempY = activeY;

        while (tempY >= 0 && initColor.equals((new Color(img.getRGB(activeX, tempY))))) {
            --tempY;

        }
        tempY++;

        left = right = false;
        final int width = img.getWidth();
        final int height = img.getHeight();

        while (tempY < height && initColor.equals(new Color(img.getRGB(activeX, tempY)))) {
            g2.drawLine(activeX, tempY, activeX, tempY);

            canvas.repaint();

            if (!left && activeX > 0 && initColor.equals(new Color(img.getRGB(activeX - 1, tempY)))) {
                stack.add(new Point(activeX - 1, tempY));
                left = true;
                // System.out.println("LEFT " + initColor + "==" + new
                // Color(img.getRGB(activeX - 1, tempY)));
            } else if (left && activeX > 0 && !initColor.equals(new Color(img.getRGB(activeX - 1, tempY)))) {
                left = false;
            }

            if (!right && activeX < width - 1 && initColor.equals(new Color(img.getRGB(activeX + 1, tempY)))) {
                stack.add(new Point(activeX + 1, tempY));
                right = true;
                // System.out.println("RIGHT " + initColor + "==" + new
                // Color(img.getRGB(activeX + 1, tempY)));
            } else if (right && activeX < width - 1 && !initColor.equals(new Color(img.getRGB(activeX + 1, tempY)))) {
                right = false;
            }
            tempY++;
        }
        // System.out.println(i++);
    }
    canvas.repaint();
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
    // System.out.println("entered");
}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
    // System.out.println("ex5ted");
}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
    // System.out.println("pressed");
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
    // System.out.println("released");

}

@Override
public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub
    // System.out.println("dragged");
}

@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub
    // System.out.println("moved");
}

}

具有颜色、桶填充和清除选项的右手工具面板

public class RightPanel extends JPanel {

private final JButton bucketFill;

private final JButton colorButton, clear;

private final Canvas canvas;

public RightPanel(Canvas canvas) {
    this.canvas = canvas;
    this.setBackground(Color.white);
    this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    this.clear = new JButton("Clear");

    this.bucketFill = new JButton(new ImageIcon("src/charnetskaya/paint/BucketFill.jpg"));
    this.bucketFill.setSize(new Dimension(30, 30));
    this.bucketFill.setBorder(null);
    this.bucketFill.setBorderPainted(false);
    this.bucketFill.setMargin(new Insets(0, 0, 0, 0));
    this.bucketFill.setCursor(new Cursor(Cursor.HAND_CURSOR));

    this.colorButton = new JButton("   ");
    this.colorButton.setBackground(Color.black);
    this.add(colorButton);

    this.add(bucketFill);
    this.add(clear);

    this.colorButton.addActionListener(new ButtonListener(this));
    this.clear.addActionListener(new ButtonListener(this));
}

private class ButtonListener implements ActionListener {

    private final RightPanel rightPanel;

    private ButtonListener(RightPanel rightPanel) {
        this.rightPanel = rightPanel;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub

        if (event.getSource() == colorButton) {
            new ColorChooser(canvas, colorButton);
        } else if (event.getSource() == clear) {
            canvas.setClearBackground(canvas.getImage());
        }
    }
}

}

下面不是那么重要的代码 保存整个应用设置的设置

public class Settings {
private Color color;
private Stroke stroke;
private int strokeSize;

public Settings() {
    this.strokeSize = 3;
    this.color = Color.BLACK;
    this.stroke = new BasicStroke(strokeSize, BasicStroke.CAP_ROUND, 0);
}

public void applySettings(Graphics2D g) {

    g.setColor(color);
    g.setStroke(stroke);
}

public Color getColor() {
    return color;
}

public void setColor(Color color) {
    this.color = color;
}

public Stroke getStroke() {
    return stroke;
}

public void setStroke(Stroke stroke) {
    this.stroke = stroke;
}

public int getStrokeSize() {
    return strokeSize;
}

public void setStrokeSize(int strokeSize) {
    this.strokeSize = strokeSize;
    this.stroke = new BasicStroke(strokeSize, BasicStroke.CAP_ROUND, 0);
}

}

用于更改颜色的颜色选择器框架

public class ColorChooser extends JFrame {

private final Canvas canvas;
private final JButton colorButton;

private final JButton selectButton;

private final JColorChooser chooser;

private Color color;

public ColorChooser(Canvas canvas, JButton colorButton) {
    this.canvas = canvas;
    this.colorButton = colorButton;

    this.setLocationRelativeTo(null);
    this.setSize(300, 300);

    this.selectButton = new JButton("Select");
    this.selectButton.addActionListener(new SelectButtonListener());

    this.chooser = new JColorChooser();
    this.chooser.add(selectButton, BorderLayout.SOUTH);

    this.add(chooser);
    this.setVisible(true);
}

private class SelectButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent arg0) {

        color = chooser.getColor();
        canvas.getSettings().setColor(color);
        colorButton.setBackground(color);
    }
}

}

在图像上,您可以看到桶填充适用于除黑色以外的任何其他颜色

您正在使用此行获取 mouseClicked() 事件侦听器中当前图像像素的颜色:

final Color initColor = new Color(img.getRGB(x, y));

注意Color的这个构造函数的文档:

Color(int rgb)

Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

所以它获取图像,删除 alpha 分量,并为您提供不透明的颜色值。所以,如果像素有 alpha=0,r=0,g=0,b=0,你会得到 alpha=255,r=0,g=0,b=0 - 这恰好是黑色。

为了避免这种情况,您应该使用允许您获取 alpha 值的构造函数:

Color(int rgba, boolean hasalpha)

Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.

即:

final Color initColor = new Color(img.getRGB(x, y), true);

这将 return alpha 字节设置为 0 的颜色(假设您的图像是完全透明的),这与 Color.BLACK 不同。