开发者

JFrame animated resize Mac OS X style

开发者 https://www.devze.com 2023-02-18 20:45 出处:网络
In Mac OS X, some windows change their size with gradual animation (growing/shrinking) when switching between different tabs (e.g. System Preferences window).

In Mac OS X, some windows change their size with gradual animation (growing/shrinking) when switching between different tabs (e.g. System Preferences window). What would be the best way to achieve such开发者_如何学编程 effect using JFrame (Java) without visual flickering or performance hit.

Thank you.


to shrink a little bit

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 bit

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();


I use a single JFrame and resize it after replacing the JPanels. It gives that Mac OS X style of animation (A little parameter changing might be required in the LauncherTask class though to make it animate more perfectly)

I put this line of code in the main JFrame class i.e. Launcher.java in my case. Configuration class extends JPanel class and I use this.setPreferredSize() to set it's 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

精彩评论

暂无评论...
验证码 换一张
取 消