Trouble with animation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HxRLxY
    New Member
    • Sep 2008
    • 23

    Trouble with animation

    I am currently working on a GUI. The idea is that when the user clicks a given button, that portion of the interface will slide into view from the main JFrame. The code I have written and included constitutes an initial test at the basic mechanics of animation. Everything works alright with my code, but the animation seems choppy. Any tips or tricks regarding good Java animation are very much appreciated.

    Here is my code:
    Code:
     import java.awt.*;
       import java.awt.geom.*;
       import java.awt.event.*;
       import java.awt.image.*;
       import javax.swing.*;
    
        public class AniTest
       {
           public static void main( String[] args )
          {
             AniFrame frame = new AniFrame();
             frame.display();
          }
       }
       
        class AniFrame extends JFrame implements ActionListener
       {
          private JButton btnOpen;
          private JButton btnClose;
          private JButton btnExit;
          private JPanel panelButton;
          private JPanel aniPanel;
          private OpenThread openThread;
          private CloseThread closeThread;
       
           public AniFrame()
          {
             btnOpen = new JButton( "Open" );
             btnOpen.addActionListener( this );
                   
             btnClose = new JButton( "Close" );
             btnClose.addActionListener( this );
                 
             btnExit = new JButton( "Exit" );
             btnExit.addActionListener( 
                    new ActionListener(){
                       public void actionPerformed( ActionEvent e )
                      {
                         System.exit( 0 );
                      }
                   });
                   
          	//set up panel
             panelButton = new JPanel();
             panelButton.add( btnOpen );
             panelButton.add( btnClose );
             panelButton.add( btnExit );
             panelButton.setBorder( BorderFactory.createMatteBorder( 3, 3, 3, 3, Color.BLACK ) );
             
             aniPanel = new JPanel();
             aniPanel.setBorder( BorderFactory.createMatteBorder( 0, 1, 1, 1, Color.RED ) );
                
          	//set up this
             setUndecorated( true );
             setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
             setLayout( new BorderLayout() );
             add( panelButton, BorderLayout.NORTH );
             add( aniPanel, BorderLayout.CENTER );
             pack();
             setLocationRelativeTo( null );
          }
       //==================================================================================
           public void display()
          {
             setVisible( true );
          }
       //==================================================================================
           private BufferedImage getDialogAsImage( JComponent source )
          {
             BufferedImage offscreenImage;
          	
             GraphicsConfiguration gfxConfig =
                GraphicsEnvironment.getLocalGraphicsEnvironment( )
                	   .getDefaultScreenDevice( )
                	   .getDefaultConfiguration( );
             offscreenImage =
                gfxConfig.createCompatibleImage(source.getWidth( ),
                					source.getHeight( )); 
             Graphics2D offscreenGraphics =
                (Graphics2D) offscreenImage.getGraphics( );
             source.paint (offscreenGraphics);
             
             return offscreenImage;
          }
       //==================================================================================
           public void actionPerformed( ActionEvent e )
          {
             JButton btn = (JButton)e.getSource();
             if( btn == btnOpen )
                (new OpenThread( this, aniPanel, getDialogAsImage(
                   	(JComponent)((new MyOpenDialog()).getOpenDialog().getContentPane()) ) )).start();
             else
                (new CloseThread( this, aniPanel, getDialogAsImage(
                   	(JComponent)((new MyOpenDialog()).getOpenDialog().getContentPane()) ) )).start();
             repaint();
          }
       //==================================================================================   
           class OpenThread extends Thread
          {
             private JFrame frame;
             private JPanel panel, panelTemp;
             private BufferedImage image, imageTemp;
             
              public OpenThread( JFrame frame, JPanel panel, BufferedImage image )
             {
                this.frame = frame;
                this.panel = panel;
                this.image = image;
             }
              public void run()
             {
                int width = image.getWidth();
                int height = image.getHeight();
                int heightStep = (int)( height / 20 );
                int currentHeight = 0;
                
                for( int count = 0; count < 20; count++ )
                {
                   panel.removeAll();
                   currentHeight += heightStep;
                   imageTemp = image.getSubimage( 0, (height - currentHeight), width, currentHeight );
                   panelTemp = 
                       new JPanel(){
                          public void paintComponent( Graphics g )
                         {
                            super.paintComponent( g );
                            Graphics2D g2d = (Graphics2D)g;
                            g.drawImage( imageTemp, 0, 0, null );
                         }
                      };
                   panelTemp.setPreferredSize( new Dimension( 
                      		imageTemp.getWidth(), imageTemp.getHeight() ) );
                   panel.add( panelTemp );
                   frame.pack();
                   try{ Thread.sleep( 5 ); }
                       catch( InterruptedException ie ) {}
                }
                
                panelTemp.add( (new MyOpenDialog()).getOpenDialog().getContentPane() );
                panel.revalidate();
                frame.pack();
             }
          }
       //==================================================================================
           class CloseThread extends Thread
          {
             private JFrame frame;
             private JPanel panel, panelTemp;
             private BufferedImage image, imageTemp;
             
              public CloseThread( JFrame frame, JPanel panel, BufferedImage image )
             {
                this.frame = frame;
                this.panel = panel;
                this.image = image;
             }
              public void run()
             {   
                int width = image.getWidth();
                int height = image.getHeight();
                int heightStep = (int)( height / 20 );
                int currentHeight = 0;
                
                for( int count = 0; count < 20; count++ )
                {
                   panel.removeAll();
                   currentHeight += heightStep;
                   imageTemp = image.getSubimage( 0, currentHeight, width, (height - currentHeight) );
                   panelTemp = 
                       new JPanel(){
                          public void paintComponent( Graphics g )
                         {
                            super.paintComponent( g );
                            Graphics2D g2d = (Graphics2D)g;
                            g.drawImage( imageTemp, 0, 0, null );
                         }
                      };
                   panelTemp.setPreferredSize( new Dimension( 
                      		imageTemp.getWidth(), imageTemp.getHeight() ) );
                   panel.add( panelTemp );
                   frame.pack();
                   try{ Thread.sleep( 5 ); }
                       catch( InterruptedException ie ) {}
                }
                
                panelTemp.add( (new MyOpenDialog()).getOpenDialog().getContentPane() );
                panel.revalidate();
                frame.pack();  
             }
          }
       }
    //==================================================================================
        class MyOpenDialog extends JFileChooser
       {
           public JDialog getOpenDialog()
          {
             setDialogType( JFileChooser.OPEN_DIALOG );
             return createDialog( null );
          }
       }
    //==================================================================================
Working...