I use one JFrame and resize it after replacing JPanels. This gives a Mac OS X animation style (you may need to make a slight change to the parameter in the LauncherTask class to make it more enjoyable)
I put this line of code in the main JFrame class, i.e. Launcher.java in my case. The configuration class extends the JPanel class, and I use this.setPreferredSize () to set its size (this will be used in the LauncherTask class to resize accordingly)
(new Thread(new Configuration(this))).start();
This is a class file for resizing Jpanels.
package com.imoz; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.SwingWorker; public class LauncherTask extends SwingWorker<Void, Void> { float x,y,dx,dy,pixel; private JFrame parent; private Dimension target; public LauncherTask(JFrame parent, Dimension target) { this.parent = parent; this.target = target; } @Override protected Void doInBackground() throws Exception { dx = (float) Math.abs(parent.getWidth() - target.getWidth()); dy = (float) Math.abs(parent.getHeight() - target.getHeight()); if( dx >= dy ) pixel=dx; else pixel=dy; pixel /= 7.5; dx=dx/pixel; dy=dy/pixel; x=parent.getWidth(); y=parent.getHeight(); boolean dxGrow = true, dyGrow = true; if(parent.getWidth() < target.getWidth()) dxGrow = false; if(parent.getHeight() < target.getHeight()) dyGrow = false; float i = 1f; while(i <= pixel) { if(dxGrow) x -= dx; else x += dx; if(dyGrow) y -= dy; else y += dy; parent.setSize((int)x, (int)y); parent.setLocationRelativeTo(null); parent.invalidate(); i+=1; } parent.setSize((int)target.getWidth(), (int)target.getHeight()); parent.setLocationRelativeTo(null); return null; }
}
source share