Results 1 to 4 of 4
  1. #1
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Question Need Help in Animating JButton

    I'm creating a program to animate Button.
    When Mouse is moved on JButton the Button slowly disappears and when the mouse is moved away from the Button it appears again.
    Every thing is working fine except an error which i can't understand.

    here is my code.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class AnimatedButton{
    	
    	private JFrame frame= new JFrame("Animated Button");
    	private Animate b1= new Animate("OKAY");
    	float initialAlpha;
    	Timer t;
    	float incrementAlpha = -.03f;
    	
    	AnimatedButton(){
    		frame.setSize(500,500);
    		frame.setLayout(null);
    		frame.setResizable(false);
    		frame.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseMoved(java.awt.event.MouseEvent evt) {
              	  frameMouseMoved(evt);
                }
            });
    		
    		b1.setBounds(150,200,200,50);
    		b1.setOpaque(false);
    	    b1.setFocusPainted(false);
    		b1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseMoved(java.awt.event.MouseEvent evt) {
              	  b1MouseMoved(evt);
                }
            });
    		initialAlpha=b1.getAlpha();
    		
    		t= new Timer(30,new ActionListener(){
    
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    			
    		});
    		
    		frame.setVisible(true);
    		frame.add(b1);
    	
    	}
    	
    	private void frameMouseMoved(java.awt.event.MouseEvent evt) {
    		// TODO add your handling code here:
    		t.start();
    		float newAlpha = b1.getAlpha() - incrementAlpha;
    		 b1.setAlpha(newAlpha);
    		}
    	private void b1MouseMoved(java.awt.event.MouseEvent evt) {
    		// TODO add your handling code here:
    		t.start();
    		 float newAlpha = b1.getAlpha() + incrementAlpha;
    		 b1.setAlpha(newAlpha);
    		}
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		new AnimatedButton();
    	}
    
    }
    and here is Animate Class

    Java Code:
    import java.awt.AlphaComposite;
    import java.awt.Graphics2D;
    
    import javax.swing.JButton;
    
       class Animate extends JButton {
            private float alpha = 1f;
    
            Animate(String text) {
                super(text);
    
            }
    
            public float getAlpha() {
                return alpha;
            }
    
            public void setAlpha(float alpha) {
                this.alpha = alpha;
                repaint();
            }
    
            @Override
            public void paintComponent(java.awt.Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                super.paintComponent(g2);
            }
        }
    please help me understanding the error which is in the following line:
    g2.setComposite(AlphaComposite.getInstance(AlphaCo mposite.SRC_OVER, alpha));

    The error is:

    XML Code:
    [HTML]Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: alpha value out of range
    	at java.awt.AlphaComposite.<init>(Unknown Source)
    	at java.awt.AlphaComposite.getInstance(Unknown Source)
    	at Animate.paintComponent(Animate.java:26)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager$3.run(Unknown Source)
    	at javax.swing.RepaintManager$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$1100(Unknown Source)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: alpha value out of range
    [/HTML]

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need Help in Animating JButton

    [HTML]Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: alpha value out of range
    at java.awt.AlphaComposite.<init>(Unknown Source)
    at java.awt.AlphaComposite.getInstance(Unknown Source)
    at Animate.paintComponent(Animate.java:26)
    Did you read the API doc for the getInstance() method and the reasons it will through a IllegalArgumentException?
    What are the values of the args passed to getInstance()?
    Last edited by Norm; 01-24-2014 at 02:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help in Animating JButton

    I've tried but i can't understand that thing.. i've tried putting some manual value like "1" but the animation part don't work then.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need Help in Animating JButton

    i can't understand that thing
    Please copy here the text of the API doc you don't understand and ask some questions about it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: 09-05-2012, 06:13 AM
  2. Ball Move to mouse not animating.
    By Jossos in forum New To Java
    Replies: 6
    Last Post: 01-05-2012, 01:42 PM
  3. [Slick] Animating a GIF?
    By SX16 in forum Java 2D
    Replies: 0
    Last Post: 11-27-2011, 02:52 AM
  4. Standard Way of Animating Images?
    By TacoManStan in forum New To Java
    Replies: 1
    Last Post: 09-11-2011, 04:57 PM
  5. trouble animating
    By rippon in forum AWT / Swing
    Replies: 3
    Last Post: 11-30-2010, 03:22 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
  •