Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    35

    Immagine in dissolvenza

    è possibile creare in java un'immagine in dissolvenza?? La cosa che vorrei fare è creare uno spalshscreen e dargli l'effetto dissolvenza....in altri termini vorrei che l'immagine si apra e si chiuda gradualmente....è possibile farlo? se si come?

  2. #2

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    35
    Originariamente inviato da alde90
    http://javaboutique.internet.com/Fade/Fade.java
    Ho visto un pò il link che mi hai passato ma funziona tramite applet. Cmq ho trovato questa classe su internet che ti permette di fare lo splashscreen con dissolvenza in entrata. Se io volessi mettere la dissolvenza anche in uscita come potrei modificare questo codice??

    codice:
    import java.awt.AWTException;
    import java.awt.AlphaComposite;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    import javax.swing.JWindow;
    import javax.swing.Timer;
     
    public class FadeImmagePanel extends JPanel  {
            /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    		private Image image;   
            private Image bgImage;
            private float alpha = 0.0f;
            private Dimension PREF_SIZE;
            
            FadeImmagePanel(URL imageLocation) throws IOException {
                this.image = ImageIO.read(imageLocation);
                initialize();
            }
            
            private void initialize() {
                captureBackground();
                startAnimation();
            }
     
            private void startAnimation() {
                Thread t = new Thread(new Runnable() {
                    public void run() {
                        while (alpha < 1.0f) {
                            try {
                                Thread.sleep(40);
                                repaint();
                                alpha += 0.05f;
                                if (alpha > 1.0f) {
                                    alpha = 1.0f;
                                }
                            } catch (InterruptedException ie) {
                                ie.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
            }
            
            public Point getPreferredSplashLocation() {
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                int w = screenSize.width;
                int h = screenSize.height;
                int x = (w - image.getWidth(this))/2;
                int y = (h - image.getHeight(this))/2;
                return new Point(x,y);
                
            }
     
            private void captureBackground() {
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                int w = screenSize.width;
                int h = screenSize.height;
                int x = (w - this.getPreferredSize().width)/2;
                int y = (h - this.getPreferredSize().height)/2;
                Rectangle splashRectangle = new Rectangle(x,y,w,h);
                try {
                    Robot r = new Robot();
                    bgImage = r.createScreenCapture(splashRectangle);
                } catch (AWTException awte) 
                {
                    awte.printStackTrace();
                }
            }
            
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D)g;
                g2d.drawImage(bgImage, 0, 0, this);
                AlphaComposite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
                g2d.setComposite(comp);
                g.drawImage(image, 0, 0, this);
            }
            
            public Dimension getPreferredSize() {
                if (PREF_SIZE == null) {
                    PREF_SIZE = new Dimension(image.getWidth(this), image.getHeight(this));
                }
                return PREF_SIZE;
            }
            
            public static void main(String[] args) throws IOException {
                final JWindow window = new JWindow();
                URL imageURL = FadeImmagePanel.class.getResource("prova.jpg");
                FadeImmagePanel fip = new FadeImmagePanel(imageURL);
                window.add(fip);
                window.pack();
                
                window.setLocation(fip.getPreferredSplashLocation());
                window.setVisible(true);
                window.addMouseListener(new MouseAdapter()
                {
                    public void mousePressed(MouseEvent e)
                    {
                        window.setVisible(false);
                        window.dispose();
                        System.exit(0);
                    }
                });
                new Timer(8000, new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                    	System.exit(0);
                        
                    }
                }).start();
            }
        }

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    157
    Lo so che è un'applet, ma basta che guardi la classe Thoughts per capire come questo funziona. Quella classe che hai visto fa la dissolvenza, come avevi chiesto.

    Comunque, penso (così a primo impatto, non ho letto il codice) che la soluzione sia questa

    codice:
    private void startAnimation() {
                Thread t = new Thread(new Runnable() {
                    public void run() {
                        while (alpha < 1.0f) {
                            try {
                                Thread.sleep(40);
                                repaint();
                                alpha += 0.05f;
                                if (alpha > 1.0f) {
                                    alpha = 1.0f;
                                }
                            } catch (InterruptedException ie) {
                                ie.printStackTrace();
                            }
                        }
                        
                        while (alpha > 0.0f) {
                            try {
                                Thread.sleep(40);
                                repaint();
                                alpha -= 0.05f;
                                if (alpha < 0.0f) {
                                    alpha = 0.0f;
                                }
                            } catch (InterruptedException ie) {
                                ie.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
            }
    questo la mostra, e poi immediatamente la fa svanire. Se vuoi due cose separate, devi fare così



    codice:
    private void startAnimationIn() {
                Thread t = new Thread(new Runnable() {
                    public void run() {
                        while (alpha < 1.0f) {
                            try {
                                Thread.sleep(40);
                                repaint();
                                alpha += 0.05f;
                                if (alpha > 1.0f) {
                                    alpha = 1.0f;
                                }
                            } catch (InterruptedException ie) {
                                ie.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
            }
    
            private void startAnimationOut() {
                Thread t = new Thread(new Runnable() {
                    public void run() {
                        while (alpha > 0.0f) {
                            try {
                                Thread.sleep(40);
                                repaint();
                                alpha -= 0.05f;
                                if (alpha < 0.0f) {
                                    alpha = 0.0f;
                                }
                            } catch (InterruptedException ie) {
                                ie.printStackTrace();
                            }
                        }
                    }
                });
                t.start();
            }
    lo startAnimationIn dissolvenza in entrata, startAnimationOut in uscita

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    35
    Grazie mille ho risolto

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    157
    Dì anche come, così se qualcuno ha dei problemi qui c'è scritto la soluzione..

  7. #7
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    35
    Originariamente inviato da alde90
    Dì anche come, così se qualcuno ha dei problemi qui c'è scritto la soluzione..

    Ho risolto così:

    codice:
    import java.awt.AWTException;
    import java.awt.AlphaComposite;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JWindow;
    import javax.swing.Timer;
    
     
    public class SplashScreen extends JPanel  
    {
         
    	
    		private static final long serialVersionUID = 1L;
    		private Image image;   
            private Image bgImage;
            private float alpha = 0.0f;
            private Dimension PREF_SIZE;
          
            private Timer t3;
          public SplashScreen(){}
            
            public void carica() 
            {
            	final JWindow window=new JWindow();
                URL imageURL = SplashScreen.class.getResource("prova.jpg");
                SplashScreen fip = new SplashScreen();
                try {
    				fip.addImmage(imageURL);
    			} catch (IOException e1) {}
                window.add(fip);
                window.pack();
                
                window.setLocation(fip.getPreferredSplashLocation());
                window.setVisible(true);
                window.addMouseListener(new MouseAdapter()
                {
                    public void mousePressed(MouseEvent e)
                    {	t3.stop();
                        window.setVisible(false);
                        window.dispose();
                        new ActionGui();
                    }
                });
               t3 =new Timer(5200, new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                    	t3.stop();
                    	window.setVisible(false);
                        window.dispose();
                        new ActionGui();
                        
                    }
                });
                t3.start();
                
                
    
                
            }
              
    
            
            
            public void addImmage(URL imageLocation)throws IOException
            {
            	this.image = ImageIO.read(imageLocation);
                initialize();
            }
            
            private void initialize() 
            {
                captureBackground();
                startAnimationIn();
            }
            
            private void startAnimationIn() 
            {
                Thread t = new Thread(new Runnable()
                {
                    public void run() 
                    {
                        while (alpha < 1.0f) 
                        {
                            try 
                            {
                                Thread.sleep(30);
                                repaint();
                                alpha += 0.05f;
                                if (alpha > 1.0f) 
                                {
                                    alpha = 1.0f;
                                }
                                
                            } 
                            catch (InterruptedException ie) 
                            {
                                ie.printStackTrace();
                            }
                           
                          }
                        
                        try 
                        {
    						Thread.sleep(4000);
    					} catch (InterruptedException e) 
    					{
    						
    						JOptionPane.showMessageDialog( null,"Errore Thread", "Errore",
    					        JOptionPane.ERROR_MESSAGE);}
    					
    					while (alpha > 0.0f) 
                        {
                            try {
                                Thread.sleep(30);
                                repaint();
                                alpha -= 0.05f;
                                if (alpha < 0.0f) 
                                {
                                    alpha = 0.0f;
                                }
                            	} 
                            	catch (InterruptedException ie) 
                            	{
                            		ie.printStackTrace();
                            	}
                            	
                        }
    					
                }});
                t.start();
            
            
            
            }
    
            
            
                public Point getPreferredSplashLocation() 
                {
                	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                	int w = screenSize.width;
                	int h = screenSize.height;
                	int x = (w - image.getWidth(this))/2;
                	int y = (h - image.getHeight(this))/2;
                	return new Point(x,y);
                
                }
     
                private void captureBackground() 
                {
                	
                	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                	int w = screenSize.width;
                	int h = screenSize.height;
                	int x = (w - this.getPreferredSize().width)/2;
                	int y = (h - this.getPreferredSize().height)/2;
                	Rectangle splashRectangle = new Rectangle(x,y,w,h);
                	try {
                		Robot r = new Robot();
                		bgImage = r.createScreenCapture(splashRectangle);
                	} catch (AWTException awte) 
                	{
                    awte.printStackTrace();
                	}
                }
            
                protected void paintComponent(Graphics g)
                {
                	Graphics2D g2d = (Graphics2D)g;
                	g2d.drawImage(bgImage, 0, 0, this);
                	AlphaComposite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
                	g2d.setComposite(comp);
                	g.drawImage(image, 0, 0, this);
                }
            
            public Dimension getPreferredSize() 
            {
                if (PREF_SIZE == null) 
                {
                    PREF_SIZE = new Dimension(image.getWidth(this), image.getHeight(this));
                }
                return PREF_SIZE;
            }
            
            
            
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.