我正在开发一个具有透明缓冲图像的绘图应用程序。我需要执行的任务之一来实现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);
}
}
Color Chooser框架可以改变颜色
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);
}
}
}
在图像上,您可以看到铲斗填充适用于任何其他颜色但黑色
答案 0 :(得分:1)
您正在使用此行获取mouseClicked()
事件监听器中当前图片像素的颜色:
final Color initColor = new Color(img.getRGB(x, y));
请注意Color
的构造函数的文档:
Color(int rgb)
使用指定的颜色创建不透明的sRGB颜色 组合RGB值由位16-23中的红色分量组成 位8-15中的绿色分量,位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)
使用指定的组合RGBA值创建sRGB颜色 由位24-31中的alpha分量组成,红色分量在 位16-23,位8-15中的绿色分量和蓝色分量 在位0-7。
那是:
final Color initColor = new Color(img.getRGB(x, y), true);
这将返回一个颜色,其alpha字节设置为0(假设您的图像完全透明),这与Color.BLACK
不同。