图层透明度在java中不起作用
我正在尝试编写一个使用图层的绘画应用程序。我目前有4层,底部一层是白色,其他所有层都是透明的。同时,我只有第三层(顶部)绘制到缓冲图像,其他人出于某种原因只显示预览,但不提交到图像。而且,即使我实现了层的透明度,它也不会生效。所有的图层都是黑色的。好的方面是,它始终显示最顶层。请帮忙图层透明度在java中不起作用,java,swing,Java,Swing,我正在尝试编写一个使用图层的绘画应用程序。我目前有4层,底部一层是白色,其他所有层都是透明的。同时,我只有第三层(顶部)绘制到缓冲图像,其他人出于某种原因只显示预览,但不提交到图像。而且,即使我实现了层的透明度,它也不会生效。所有的图层都是黑色的。好的方面是,它始终显示最顶层。请帮忙 public class Canvas extends JComponent implements MouseWheelListener { private static final long seria
public class Canvas extends JComponent implements MouseWheelListener {
private static final long serialVersionUID = 1L;
private final List<BufferedImage> images;
private DrawListenerInterface listenerInterface;
private int activeLayer;
private final Color color;
private final Paint frame;
public Canvas(Paint frame) {
this.frame = frame;
this.images = new ArrayList<BufferedImage>();
this.activeLayer = 0;
this.color = Color.red;
for (int i = 0; i < 4; i++) {
images.add(new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB));
setLayers(i);
}
this.addMouseWheelListener(this);
}
在
Canvas
中调用super.paintComponent(g)
间接调用ComponentUI.update(…)
:
因此,除非您在某个时候对组件调用setOpaque(false)
,否则您总是使用背景色填充g
我会尝试将不透明设置为false
或删除super.paintComponent(g)
调用
代码似乎还有其他问题。
您在画布
构造函数中创建的图像都是类型_INT_RGB
,即它们不支持alpha通道/透明度。
但随后在setLayers()
中,对于0以外的层,您检索图像img
,然后用一个新的支持透明度的图像覆盖它,该图像从未存储在任何地方:
img
在您离开相应的区块后立即丢失。很难理解您的实际要求,但是:
BufferedImage img = images.get(layerNumber);
img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
final Graphics2D graphics = (Graphics2D) img.getGraphics();
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
graphics.fillRect(0, 0, 800, 600);
// reset composite
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
您没有将
img
存储在任何地方,即使您为它创建了一个新实例。同样,从图像中获取它,然后在设置为其他内容之后立即获取它也没有意义。您的回答没有直接帮助我,但是我的整个问题是使用类型为_INT_RGB的BuffereImage,一个类型为_INT_ARGB_的图像在开始工作之前被更改。所以你应该得到一个大+1
public class DrawListener implements DrawListenerInterface {
private final Canvas canvas;
private int x, y;
private int x2;
private int y2;
private final Paint frame;
public DrawListener(Canvas canvas, Paint frame) {
this.canvas = canvas;
this.frame = frame;
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
this.x2 = e.getX();
this.y2 = e.getY();
final Graphics2D g2 = (Graphics2D) canvas.getImage().getGraphics();
draw(g2);
canvas.repaint();
x = x2;
y = y2;
}
@Override
public void draw(Graphics2D g2) {
// TODO Auto-generated method stub
g2.drawLine(x, y, x2, y2);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x = e.getX();
x2 = e.getX();
y = e.getY();
y2 = e.getY();
final Graphics2D g2 = (Graphics2D) canvas.getImage().getGraphics();
draw(g2);
canvas.repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void preview(Graphics2D g2) {
// TODO Auto-generated method stub
}
public class LayerPanel extends JPanel {
private final JButton layer0, layer1, layer2, layer3;
private final Canvas canvas;
public LayerPanel(Canvas canvas, Paint paint) {
this.canvas = canvas;
this.setSize(new Dimension(200, 400));
this.layer0 = new JButton("Layer 0");
this.layer1 = new JButton("Layer 1");
this.layer2 = new JButton("Layer 2");
this.layer3 = new JButton("Layer 3");
this.layer0.addActionListener(new ButtonListener());
this.layer1.addActionListener(new ButtonListener());
this.layer2.addActionListener(new ButtonListener());
this.layer3.addActionListener(new ButtonListener());
this.setLayout(new GridLayout(4, 1));
this.add(layer3);
this.add(layer2);
this.add(layer1);
this.add(layer0);
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
layer0.setBackground(Color.gray);
layer1.setBackground(Color.gray);
layer2.setBackground(Color.gray);
layer3.setBackground(Color.gray);
if (e.getSource().equals(layer0)) {
canvas.setActiveLayer(0);
final JButton b = (JButton) e.getSource();
b.setBackground(Color.yellow);
} else if (e.getSource().equals(layer1)) {
canvas.setActiveLayer(1);
final JButton b = (JButton) e.getSource();
b.setBackground(Color.yellow);
} else if (e.getSource().equals(layer2)) {
canvas.setActiveLayer(2);
final JButton b = (JButton) e.getSource();
b.setBackground(Color.yellow);
} else if (e.getSource().equals(layer3)) {
canvas.setActiveLayer(3);
final JButton b = (JButton) e.getSource();
b.setBackground(Color.yellow);
}
}
}
public interface DrawListenerInterface extends MouseListener, MouseMotionListener {
public void draw(Graphics2D g2);
public void preview(Graphics2D g2);
public void update(Graphics g, JComponent c) {
if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);
}
BufferedImage img = images.get(layerNumber);
img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
BufferedImage img = images.get(layerNumber);
img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
final Graphics2D graphics = (Graphics2D) img.getGraphics();
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
graphics.fillRect(0, 0, 800, 600);
// reset composite
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));