2

I was using swing to create a program/start menu for my java program. What I want is for my program to play an animated GIF in the background, while an invisible button over a certain section of the gif (which only reveals its presence if you mouse over it). My problems are thus:

A) I am not sure how to get the animated gif to play while to program waits for a button press. B) How do I make a JButton invisible?

Any help would be appreciated, thanks.

-JXP

mKorbel
  • 109,525
  • 20
  • 134
  • 319
JXPheonix
  • 2,618
  • 3
  • 16
  • 14
  • *"My problems are thus:"* ..Complaints from the users about the 'unusable GUI'? 'Invisible buttons' - blech! Make them visible or don't put them in any GUI you expect me to use. What is this GUI? A children's game? ( And if it is, I reserve the right to change my mind and quite like it. ;) – Andrew Thompson Mar 05 '12 at 15:22
  • @AndrewThompson The buttons aren't invisible, see, but what i want the buttons to do is the animated gif contains the button...for example, the gif is a gif and i want a section of the gif (it has the word start on it) to be the button, not one of the default java buttons cause then it just wouldn't fit. – JXPheonix Mar 24 '12 at 14:07

1 Answers1

4

I never used gif or animated gif in the Swing, but you can use for that this code

enter image description here enter image description here

import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class MyToggleButton extends JFrame {

    private static final long serialVersionUID = 1L;
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");

    public MyToggleButton() {
        final JButton toggleButton = new JButton();
        toggleButton.setBorderPainted(false);
        toggleButton.setBorder(null);
        toggleButton.setFocusable(false);
        toggleButton.setMargin(new Insets(0, 0, 0, 0));
        toggleButton.setContentAreaFilled(false);
        toggleButton.setIcon((errorIcon));
        toggleButton.setSelectedIcon(infoIcon);
        toggleButton.setRolloverIcon((infoIcon));
        toggleButton.setPressedIcon(warnIcon);
        toggleButton.setDisabledIcon(warnIcon);
        add(toggleButton);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyToggleButton t = new MyToggleButton();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319