gpt4 book ai didi

java - AlphaComposite.Clear 的错误,它认为其背景是黑色

转载 作者:太空宇宙 更新时间:2023-11-04 06:16:28 25 4
gpt4 key购买 nike

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

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

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

}

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

最佳答案

您正在使用此行来获取 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);

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

关于java - AlphaComposite.Clear 的错误,它认为其背景是黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28033909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com