3

I want to make a panel which extends JPanel and when it's going to be visible, it starts to get more transparent and more transparent and finally gets disappeared. what is the problem of my code?

public class BaloonPanel extends JPanel
{

private float transparency = 1f;
Timer timer;

public BaloonPanel()
{

    setBackground(Color.white);
    ActionListener action = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            transparency = transparency - 0.01f;

            if (transparency < 0.0f)
            {
                timer.stop();
            }
            repaint();
        }
    };

    timer = new Timer(100, action);
    timer.start();
}

@Override
public void paint(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
    super.paint(g2);
    g2.dispose();
}

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Soheil
  • 1,676
  • 7
  • 34
  • 65
  • 1
    What do you want your code to do? What does your code actually do? Describe the mismatch between the two. Help us to help you. – rossum Jan 21 '13 at 22:33
  • @rossum as I described above I want my panel gets more transparent and more and finally disappears. my code doesn't continue to the point I want, it causes the panel gets a bit more transparent (only)... – Soheil Jan 22 '13 at 07:23

1 Answers1

3

Because the BallonPanel is opaque, the repaint manager isn't bothering to paint underneath it. This is an optimization of the paint process, why paint that which doesn't need to be painted.

You need to "persuade" the repaint manger to paint underneath your component, while still painting its background.

Set the BallonPanel to transparent (setOpaque(false)) and update the paint method to fill the background.

public class FadePane {

    public static void main(String[] args) {
        new FadePane();
    }

    public FadePane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setBackground(Color.BLUE);
                frame.setBackground(Color.BLUE);
                frame.add(new BaloonPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public class BaloonPanel extends JPanel {

        private float transparency = 1f;
        Timer timer;

        public BaloonPanel() {

            setBackground(Color.white);
            ActionListener action = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    transparency = transparency - 0.1f;

                    if (transparency < 0.1f) {
                        transparency = 0;
                        timer.stop();
                    }
                    invalidate();
                    repaint();
                }
            };

            timer = new Timer(100, action);
            timer.setRepeats(true);

            setOpaque(false);

            final JButton fade = new JButton("Fade");
            fade.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.start();
                    fade.setEnabled(false);
                }
            });

            setLayout(new GridBagLayout());
            add(fade);
        }

        @Override
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g.create();
            System.out.println(transparency);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
            g2.setColor(getBackground());
            g2.fillRect(0, 0, getWidth(), getHeight());
            super.paint(g2);
            g2.dispose();
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • +1 for `AlphaComposite`; see laso this [example](http://stackoverflow.com/a/2124507/230513) that varies _saturation_. – trashgod Jan 22 '13 at 10:31