title: layer transparency does not work java
body: <p>I am trying to write a paint app that has a use of layers. I currently have 4 layers, the bottom one is white color and all other are transparent. Meanwhile I have only the 3rd(top) layer drawing to the buffered image, others for some reason only show preview but do not commit to the image. Also even though I implemented the transparency of the layers, it does not take affect. All the layers are black. The good part is that is shows the top most layer at all times. Please help</p>
  
  <pre><code>    public class Canvas extends JComponent implements MouseWheelListener {
  
  private static final long serialVersionUID = 1L;
  
  private final List&lt;BufferedImage&gt; 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&lt;BufferedImage&gt;();
      this.activeLayer = 0;
      this.color = Color.red;
      for (int i = 0; i &lt; 4; i++) {
          images.add(new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB));
  
          setLayers(i);
      }
  
      this.addMouseWheelListener(this);
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>    public class Canvas extends JComponent implements MouseWheelListener {
  
  private static final long serialVersionUID = 1L;
  
  private final List&lt;BufferedImage&gt; 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&lt;BufferedImage&gt;();
      this.activeLayer = 0;
      this.color = Color.red;
      for (int i = 0; i &lt; 4; i++) {
          images.add(new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB));
  
          setLayers(i);
      }
  
      this.addMouseWheelListener(this);
  
  }
  
  public void setListeners(DrawListenerInterface listener) {
  
      if (listenerInterface != null) {
          this.removeMouseListener(listenerInterface);
          this.removeMouseMotionListener(listenerInterface);
  
      }
      this.listenerInterface = listener;
      this.addMouseMotionListener(listenerInterface);
      this.addMouseListener(listenerInterface);
  }
  
  @Override
  protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      // defaultSettings((Graphics2D) g);
  
      final Iterator&lt;BufferedImage&gt; iter = images.iterator();
      while (iter.hasNext()) {
  
          // g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
          g.drawImage(iter.next(), 0, 0, null);
          this.repaint();
  
      }
      listenerInterface.preview((Graphics2D) g);
      this.repaint();
      // g2.drawImage(images.get(activeLayer), 0, 0, null);
  
      // }
      // g2.setColor(color);
  
  }
  
  public BufferedImage getImage() {
      return images.get(activeLayer);
  }
  
  public int getActiveLayer() {
      return activeLayer;
  }
  
  public void setActiveLayer(int activeLayer) {
      this.activeLayer = activeLayer;
  }
  
  public void setLayers(int layerNumber) {
      // System.out.println(layer);
      if (layerNumber == 0) {
          final Graphics2D g = (Graphics2D) images.get(0).getGraphics();
          g.setColor(Color.WHITE);
          g.fillRect(0, 0, 800, 600);
      } else {
          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));
  
      }
  
      repaint();
  }
  
  @Override
  public void mouseWheelMoved(MouseWheelEvent arg0) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>    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
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code> 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);
          }
  
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface DrawListenerInterface extends MouseListener, MouseMotionListener {
  
  public void draw(Graphics2D g2);
  
  public void preview(Graphics2D g2);
  </code></pre>