2

How can I apply a GIF image to my AWT Button?

AWT: does not work

    Icon warnIcon = new ImageIcon("/src/world.gif");
    Button button1 = new Button(warnIcon);

    Icon warnIcon = new ImageIcon("/src/world.gif");
    JButton button1 = new JButton(warnIcon);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    I still say Swing will work better than AWT when dealing with transparncy but you still haven't posted a proper SSCCE to show us what you are doing so how do you expect us to help. Instead you get all these "AWT hacks", which in the long run are more work. – camickr Jul 16 '11 at 17:49
  • @camickr: I have frames per second in main window and it cause the JButton to become invisible. I tried all but none works http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html –  Jul 16 '11 at 20:35
  • 1
    @89899.3K looks like as you have some problems with EDT, really don't understand your frequency and pixel per rate, stable Swing container is immunable against that, really what did you try, because these descriptions is far away from static Container, ??? are you tried to some animations or ... ???, I'll try find out some example that must works on every Native OS .... – mKorbel Jul 16 '11 at 20:56
  • 1
    @89899.3k, what part of post your SSCCE don't you understand????? You have never posted any code with images or animations. Your verbal description of the problem is confusing. This is your 4th question on this topic!!! You still don't have an answer because you still haven't posted code that we can actually use to see what your problem is. Post your SSCC if you want any help!!! – camickr Jul 17 '11 at 03:28

2 Answers2

3

AWT is a bit different from Swing.

There's no constructor button(image), hence your "not working".

Take a look at Learn how to extend the AWT with your own image buttons to see how to make that image on a AWT button.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
woliveirajr
  • 9,433
  • 1
  • 39
  • 49
  • And this link, does it work? e.g: http://www.geom.uiuc.edu/java/Kali/ImageButton.java –  Jul 16 '11 at 20:33
  • Aren't that addressed to mKorbel, in the other answer? I'm not sure it that works, I usually deal with swing, not AWT – woliveirajr Jul 16 '11 at 21:37
1

Back to your three days history, please try to run the code below on your native OS and by using OpenJDK (maybe it is really time to download a stable JDK from Oracle, you can only switch that in the project properties):

  1. By running from NetBeans

  2. compiled to a JAR file or class

    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.AlphaComposite;
    import javax.swing.*;
    import javax.swing.UIManager.LookAndFeelInfo;
    
    public class ButtonTest {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new ButtonTest().createAndShowGUI();
                }
            });
        }
    
        private JFrame frame;
        private JButton opaqueButton1;
        private JButton opaqueButton2;
        private SoftJButton softButton1;
        private SoftJButton softButton2;
    
        public void createAndShowGUI() {
            opaqueButton1 = new JButton("Opaque Button");
            opaqueButton2 = new JButton("Opaque Button");
            softButton1 = new SoftJButton("Transparent Button");
            softButton2 = new SoftJButton("Transparent Button");
            opaqueButton1.setBackground(Color.GREEN);
            softButton1.setBackground(Color.GREEN);
            frame = new JFrame();
            frame.getContentPane().setLayout(new java.awt.GridLayout(2, 2, 10, 10));
            frame.add(opaqueButton1);
            frame.add(softButton1);
            frame.add(opaqueButton2);
            frame.add(softButton2);
            frame.setSize(567, 350);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            Timer alphaChanger = new Timer(30, new ActionListener() {
                private float incrementer = -.03f;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    float newAlpha = softButton1.getAlpha() + incrementer;
                    if (newAlpha < 0) {
                        newAlpha = 0;
                        incrementer = -incrementer;
                    } else if (newAlpha > 1f) {
                        newAlpha = 1f;
                        incrementer = -incrementer;
                    }
                    softButton1.setAlpha(newAlpha);
                    softButton2.setAlpha(newAlpha);
                }
            });
            alphaChanger.start();
            Timer uiChanger = new Timer(3500, new ActionListener() {
                private LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
                private int index = 1;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        UIManager.setLookAndFeel(laf[index].getClassName());
                        SwingUtilities.updateComponentTreeUI(frame);
                    } catch (Exception exc) {
                        exc.printStackTrace();
                    }
                    index = (index + 1) % laf.length;
                }
            });
            uiChanger.start();
        }
    
        public static class SoftJButton extends JButton {
            private static final JButton lafDeterminer = new JButton();
            private static final long serialVersionUID = 1L;
            private boolean rectangularLAF;
            private float alpha = 1f;
    
            public SoftJButton() {
                this(null, null);
            }
    
            public SoftJButton(String text) {
                this(text, null);
            }
    
            public SoftJButton(String text, Icon icon) {
                super(text, icon);
    
                setOpaque(false);
                setFocusPainted(false);
            }
    
            public float getAlpha() {
                return alpha;
            }
    
            public void setAlpha(float alpha) {
                this.alpha = alpha;
                repaint();
            }
    
            @Override
            public void paintComponent(java.awt.Graphics g) {
                java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                if (rectangularLAF && isBackgroundSet()) {
                    Color c = getBackground();
                    g2.setColor(c);
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                super.paintComponent(g2);
            }
    
            @Override
            public void updateUI() {
                super.updateUI();
                lafDeterminer.updateUI();
                rectangularLAF = lafDeterminer.isOpaque();
            }
        }
    }
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Still not working at all. Everything its flickering like machine gun. e.g: https://gist.github.com/1086865 –  Jul 16 '11 at 22:24
  • Its flicking like perseconds black camera preview and then Swing button preview, again black camera preview and then again Swing button preview. Every seconds. –  Jul 16 '11 at 22:30
  • @89899.3K agreed huge/ large JButton pretty flickering on LCD monitor, look for 2D setting and hardware accelarations for Java, here my knowledge ends, that's really not my cup of Java – mKorbel Jul 16 '11 at 22:43
  • Still same flicking whenever i click the button shows and go back, shows and go back e.g: https://gist.github.com/1086884 –  Jul 16 '11 at 22:47
  • Thats the only reason, i am looking for Awt button because in my earlier sample you can see i used Window(), JPanel(), Button(). Where JPanel() allows me to be transparent, Button() allow me to skip this flicking problem, and Window() allow me to put pre-processing backgrounds where JWindow() does not. –  Jul 16 '11 at 22:51
  • there must be something described about that, take that patience and start to searching, nobody know :-) – mKorbel Jul 16 '11 at 22:55
  • Yes flicking issues both we tried. 1) http://mindprod.com/jgloss/flicker.html 2) http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html –  Jul 16 '11 at 23:00
  • and by setting setDoubleBuffered(false); too, no idea – mKorbel Jul 16 '11 at 23:15
  • tried just the same result, flicking on click, on mouse out, camera preview only, again click flicking but no stable button preview. –  Jul 17 '11 at 07:21
  • @89899.3K look and read something about Java openGL, video, animation, sure something is on this forum, but I can't help you with that, hight performance for video, animations is job for Assembler :-) – mKorbel Jul 17 '11 at 07:43
  • no no, its not a job for assembler, i can do it and share you the code, it can be done by a 8 year old kid too. Radvision has SDK its very simple no need assemblers. –  Jul 17 '11 at 07:45