JFrame animated resizing Mac OS X

In Mac OS X, some windows change size with gradual animation (increase / decrease) when switching between different tabs (for example, the "System Preferences" window). What would be the best way to achieve this effect with JFrame (Java) without visual flicker or performance hit.

Thanks.

+4
source share
2 answers

shrink a little

new Thread(new Runnable() { public void run() { Dimension dim = jframe.getSize(); while (dim.getHeight() >= someheightintegeryouwanttoshrintto) { try { jframe.setSize((int)(dim.getWidth()), (int)(dim.getHeight()-1)); Thread.sleep(1); } catch(InterruptedException ex) { } dim = jframe.getSize(); } } }).start(); 

to grow a little

 new Thread(new Runnable() { public void run() { Dimension dim = jframe.getSize(); while (dim.getHeight() <= sometheightyouwanttoincreaseto) { try { jframe.setSize((int)(dim.getWidth()), (int)(dim.getHeight()+1)); Thread.sleep(1); } catch(InterruptedException ex) { } dim = jframe.getSize(); } } }).start(); 
+2
source

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; } 

}

0
source

Source: https://habr.com/ru/post/1344819/


All Articles