Results 1 to 3 of 3

Thread: Fade In JPanel

  1. #1
    mafyou is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Fade In JPanel

    Hi guys,

    I've only just recently started doing some GUI programming at a new job and one of my tasks is to get a JPanel/box of some description, to fade in. I've had a search online for how to do this but most of it is very overwhelming and doesn't really do much to explain what is going on. I gather that you need the alpha levels to change over time, but yeah for a novice like me I have no idea where to start.

    Any hints would be greatly appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Fade In JPanel

    There's a decent article on this here: Unleash Your Creativity with Swing and the Java 2D API!

    Then you should play with it. For e.g.,

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FadeInAndOut extends JPanel {
       private static final int PREF_W = 400;
       private static final int PREF_H = 200;
       private static final int COMP_SIZE = 200;
       private static final int RULE = AlphaComposite.SRC_OVER;
       private static final int TIMER_DELAY = 40;
       private Composite[] comps = new Composite[COMP_SIZE];
       private int compsIndex = 0;
       private Composite comp;
    
       public FadeInAndOut(final JFrame frame) {
          setOpaque(false);
          for (int i = 0; i < comps.length; i++) {
             float alpha = (float) Math.cos(2 * Math.PI * i / COMP_SIZE);
             alpha += 1;
             alpha /= 2.0;
             comps[i] = AlphaComposite.getInstance(RULE, alpha );
          }
          comp = comps[compsIndex];
          setBackground(Color.red);
          
          add(new JButton("Button"));
          
          new Timer(TIMER_DELAY, new ActionListener() {
             
             @Override
             public void actionPerformed(ActionEvent arg0) {
                compsIndex++;
                compsIndex %= COMP_SIZE;
                comp = comps[compsIndex];
                frame.repaint();
             }
          }).start();
       }
       
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
       
       public void paint(Graphics g) {
          Graphics2D g2 = (Graphics2D) g.create();      
          g2.setComposite(comp);
          g2.setColor(getBackground());
          g2.fillRect(0, 0, getWidth(), getHeight());
          super.paint(g2);
          g2.dispose();
       }
       
       private static void createAndShowGui() {
          JFrame frame = new JFrame("FadeInAndOut");
    
          JLayeredPane layeredPane = new JLayeredPane();
          
          FadeInAndOut fadePanel = new FadeInAndOut(frame);
          fadePanel.setLocation(0, 0);
          fadePanel.setSize(fadePanel.getPreferredSize());
          
          JPanel bluePanel = new JPanel(new BorderLayout());
          bluePanel.setBackground(Color.blue);
          bluePanel.setSize(fadePanel.getPreferredSize());
          JLabel label = new JLabel("Fubars Rule!", SwingConstants.CENTER);
          label.setFont(label.getFont().deriveFont(Font.BOLD, 50f));
          bluePanel.add(label);
          
          layeredPane.setPreferredSize(fadePanel.getPreferredSize());
          layeredPane.add(bluePanel, JLayeredPane.DEFAULT_LAYER);
          layeredPane.add(fadePanel, JLayeredPane.PALETTE_LAYER);
          
    
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(layeredPane);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

  3. #3
    mafyou is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: Fade In JPanel

    Wow that link looks great. Thanks heaps!

Similar Threads

  1. running through jlabel fade only once
    By droidus in forum AWT / Swing
    Replies: 2
    Last Post: 01-29-2012, 02:15 PM
  2. Change JPanel text of Parent JPanel from JDialog
    By bikashlama in forum AWT / Swing
    Replies: 7
    Last Post: 12-09-2011, 03:47 AM
  3. Fade in/Fade out Class for any object
    By eLancaster in forum New To Java
    Replies: 3
    Last Post: 04-13-2011, 03:19 PM
  4. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  5. Alpha Fade Flicker
    By jamesfrize in forum Java Applets
    Replies: 3
    Last Post: 04-02-2008, 02:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •