473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trouble with animation

23
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:
Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*;
  2.    import java.awt.geom.*;
  3.    import java.awt.event.*;
  4.    import java.awt.image.*;
  5.    import javax.swing.*;
  6.  
  7.     public class AniTest
  8.    {
  9.        public static void main( String[] args )
  10.       {
  11.          AniFrame frame = new AniFrame();
  12.          frame.display();
  13.       }
  14.    }
  15.  
  16.     class AniFrame extends JFrame implements ActionListener
  17.    {
  18.       private JButton btnOpen;
  19.       private JButton btnClose;
  20.       private JButton btnExit;
  21.       private JPanel panelButton;
  22.       private JPanel aniPanel;
  23.       private OpenThread openThread;
  24.       private CloseThread closeThread;
  25.  
  26.        public AniFrame()
  27.       {
  28.          btnOpen = new JButton( "Open" );
  29.          btnOpen.addActionListener( this );
  30.  
  31.          btnClose = new JButton( "Close" );
  32.          btnClose.addActionListener( this );
  33.  
  34.          btnExit = new JButton( "Exit" );
  35.          btnExit.addActionListener( 
  36.                 new ActionListener(){
  37.                    public void actionPerformed( ActionEvent e )
  38.                   {
  39.                      System.exit( 0 );
  40.                   }
  41.                });
  42.  
  43.           //set up panel
  44.          panelButton = new JPanel();
  45.          panelButton.add( btnOpen );
  46.          panelButton.add( btnClose );
  47.          panelButton.add( btnExit );
  48.          panelButton.setBorder( BorderFactory.createMatteBorder( 3, 3, 3, 3, Color.BLACK ) );
  49.  
  50.          aniPanel = new JPanel();
  51.          aniPanel.setBorder( BorderFactory.createMatteBorder( 0, 1, 1, 1, Color.RED ) );
  52.  
  53.           //set up this
  54.          setUndecorated( true );
  55.          setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  56.          setLayout( new BorderLayout() );
  57.          add( panelButton, BorderLayout.NORTH );
  58.          add( aniPanel, BorderLayout.CENTER );
  59.          pack();
  60.          setLocationRelativeTo( null );
  61.       }
  62.    //==================================================================================
  63.        public void display()
  64.       {
  65.          setVisible( true );
  66.       }
  67.    //==================================================================================
  68.        private BufferedImage getDialogAsImage( JComponent source )
  69.       {
  70.          BufferedImage offscreenImage;
  71.  
  72.          GraphicsConfiguration gfxConfig =
  73.             GraphicsEnvironment.getLocalGraphicsEnvironment( )
  74.                    .getDefaultScreenDevice( )
  75.                    .getDefaultConfiguration( );
  76.          offscreenImage =
  77.             gfxConfig.createCompatibleImage(source.getWidth( ),
  78.                                 source.getHeight( )); 
  79.          Graphics2D offscreenGraphics =
  80.             (Graphics2D) offscreenImage.getGraphics( );
  81.          source.paint (offscreenGraphics);
  82.  
  83.          return offscreenImage;
  84.       }
  85.    //==================================================================================
  86.        public void actionPerformed( ActionEvent e )
  87.       {
  88.          JButton btn = (JButton)e.getSource();
  89.          if( btn == btnOpen )
  90.             (new OpenThread( this, aniPanel, getDialogAsImage(
  91.                    (JComponent)((new MyOpenDialog()).getOpenDialog().getContentPane()) ) )).start();
  92.          else
  93.             (new CloseThread( this, aniPanel, getDialogAsImage(
  94.                    (JComponent)((new MyOpenDialog()).getOpenDialog().getContentPane()) ) )).start();
  95.          repaint();
  96.       }
  97.    //==================================================================================   
  98.        class OpenThread extends Thread
  99.       {
  100.          private JFrame frame;
  101.          private JPanel panel, panelTemp;
  102.          private BufferedImage image, imageTemp;
  103.  
  104.           public OpenThread( JFrame frame, JPanel panel, BufferedImage image )
  105.          {
  106.             this.frame = frame;
  107.             this.panel = panel;
  108.             this.image = image;
  109.          }
  110.           public void run()
  111.          {
  112.             int width = image.getWidth();
  113.             int height = image.getHeight();
  114.             int heightStep = (int)( height / 20 );
  115.             int currentHeight = 0;
  116.  
  117.             for( int count = 0; count < 20; count++ )
  118.             {
  119.                panel.removeAll();
  120.                currentHeight += heightStep;
  121.                imageTemp = image.getSubimage( 0, (height - currentHeight), width, currentHeight );
  122.                panelTemp = 
  123.                    new JPanel(){
  124.                       public void paintComponent( Graphics g )
  125.                      {
  126.                         super.paintComponent( g );
  127.                         Graphics2D g2d = (Graphics2D)g;
  128.                         g.drawImage( imageTemp, 0, 0, null );
  129.                      }
  130.                   };
  131.                panelTemp.setPreferredSize( new Dimension( 
  132.                           imageTemp.getWidth(), imageTemp.getHeight() ) );
  133.                panel.add( panelTemp );
  134.                frame.pack();
  135.                try{ Thread.sleep( 5 ); }
  136.                    catch( InterruptedException ie ) {}
  137.             }
  138.  
  139.             panelTemp.add( (new MyOpenDialog()).getOpenDialog().getContentPane() );
  140.             panel.revalidate();
  141.             frame.pack();
  142.          }
  143.       }
  144.    //==================================================================================
  145.        class CloseThread extends Thread
  146.       {
  147.          private JFrame frame;
  148.          private JPanel panel, panelTemp;
  149.          private BufferedImage image, imageTemp;
  150.  
  151.           public CloseThread( JFrame frame, JPanel panel, BufferedImage image )
  152.          {
  153.             this.frame = frame;
  154.             this.panel = panel;
  155.             this.image = image;
  156.          }
  157.           public void run()
  158.          {   
  159.             int width = image.getWidth();
  160.             int height = image.getHeight();
  161.             int heightStep = (int)( height / 20 );
  162.             int currentHeight = 0;
  163.  
  164.             for( int count = 0; count < 20; count++ )
  165.             {
  166.                panel.removeAll();
  167.                currentHeight += heightStep;
  168.                imageTemp = image.getSubimage( 0, currentHeight, width, (height - currentHeight) );
  169.                panelTemp = 
  170.                    new JPanel(){
  171.                       public void paintComponent( Graphics g )
  172.                      {
  173.                         super.paintComponent( g );
  174.                         Graphics2D g2d = (Graphics2D)g;
  175.                         g.drawImage( imageTemp, 0, 0, null );
  176.                      }
  177.                   };
  178.                panelTemp.setPreferredSize( new Dimension( 
  179.                           imageTemp.getWidth(), imageTemp.getHeight() ) );
  180.                panel.add( panelTemp );
  181.                frame.pack();
  182.                try{ Thread.sleep( 5 ); }
  183.                    catch( InterruptedException ie ) {}
  184.             }
  185.  
  186.             panelTemp.add( (new MyOpenDialog()).getOpenDialog().getContentPane() );
  187.             panel.revalidate();
  188.             frame.pack();  
  189.          }
  190.       }
  191.    }
  192. //==================================================================================
  193.     class MyOpenDialog extends JFileChooser
  194.    {
  195.        public JDialog getOpenDialog()
  196.       {
  197.          setDialogType( JFileChooser.OPEN_DIALOG );
  198.          return createDialog( null );
  199.       }
  200.    }
  201. //==================================================================================
  202.  
Mar 5 '09 #1
0 1492

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simena Dinas | last post by:
KToon: 2D Animation Toolkit KToon is a 2D Animation Toolkit designed by animators (Toonka Films) for animators, focused to the Cartoon's industry. This project is covered by the GPL License...
5
by: Brian Angliss | last post by:
I'm relatively new to scripting in JavaScript, so I'm not too surprised I'm having difficulty scripting up an animation effect for my personal site. What I'm trying to do is the following: When...
6
by: Darren | last post by:
X-No-Archive Hi all, Can anyone help me with structuring the data in a small tool I have to build? I'm trying to work out if there's a design pattern or data structure that would remove some...
3
by: James | last post by:
Hello, I'm new to c# and have created an animation which draws directly to the window, it uses the code below which all works. I would like to know if this is the 'correct' way to do it - or...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
4
by: petermichaux | last post by:
Hi, Is there any way to make DOM scripted animation smoother? Flash is far superior in this area. Any one here know what makes Flash so smooth by comparison? I don't like the fact that the...
2
by: jasonchan | last post by:
I am trying to have a function start with a timer and then switch to another function. However, it is not working at all. Anyone know whats goin on? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0...
4
by: Sin Jeong-hun | last post by:
Most applications, including Windows Explorer, show some sort of 'wait' dialog with animation when a lengthy operation is going on. For example, When the Windows Explorer is searching for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.