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: -
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 );
-
}
-
}
-
//==================================================================================
-
0 1507 Sign in to post your reply or Sign up for a free account.
Similar topics |
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 using G++, OpenGL and QT as
programming resources from KDevelop as the development platform.
One year later of production, it is a pleasure for Toonka Films
announces to the Free Software community the release of the KToon
|
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 I go onMouseOver on a
button in a nav bar, I want that button to switch to a different image
while at the same time another blank image changes as an animation to
an associated image. When I go onMouseOut, the bar image reverts back
to the...
|
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 of the dependencies.
I am designing an animation tool.
|
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 would an
experienced c# programmer do it another way?
This part defines the screen and the graphics pad I'm drawing to:
Graphics screen = CreateGraphics();
Graphics drawpadg = Graphics.FromImage(drawpad);
|
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 values.
Works in both ie and firefox.
Parameters
styleType = top | left | width | height
toNumber = the new value of the style
then you pass in as many ids as you would like.
| |
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 thinking was:
(a) create a div for the slider / scroll nub to move within
(b) attach event handlers which, onmousedown, specify the slider/nub is
moveable, and onmouseup, specify it's not
(c) attach an event handler to the contaning div which,...
|
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 standards based world is so far behind
the Flash based stuff in some areas (animation and server push in
particular)
|
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 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script...
|
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 something, it
shows a small dialog with a moving flashlight. I examined the
explorer.exe and found that that was an avi file.
Now that, I would like to do similar thing in my C# application. But
what is the most efficient way to do so?
1)Manually...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |