Linux - 用于Java应用程序的Splash屏幕

时间:2011-01-19 08:04:22

标签: java splash-screen

在Windows上,我使用Exe4J启动Java应用程序。这具有以下优点:

  • 在Java启动之前显示的启动画面
  • 可以在启动期间更新启动画面上的标签,直到应用程序被愚蠢加载(“初始化磁通补偿器”,“重新排序高半字节”等)。

现在我想在Linux上实现它。我使用Shell脚本启动我的应用程序,但是想要一种简单的方法来显示某种带有标签的启动GUI,我可以在启动时从我的shell脚本更新。

是否有一种简单的方法可以从shell脚本启动最小的GUI,并从shell脚本更新其中的标签,而无需为linux编译单独的可执行文件(甚至可能是Mac OS X) ?

PS:我不想在启动画面中使用Java版本,因为必须动态更新Label。

3 个答案:

答案 0 :(得分:3)

用于屏幕启动的标准Java 1.6+解决方案支持在启动Screensplash Tutorial上自定义绘图。

以下是启动控制器的示例,该控制器具有main方法 - 使用splash参数运行它以查看结果。

package somepackage;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.SplashScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

public class SplashController implements ActionListener {
    private static final int X = 20, W = 300;
    private static final int TEXT_H = 10, BAR_H = 10;

    private int textY, barY;
    private int barPos = 0;

    private final SplashScreen splash;
    private Graphics2D graph;

    public SplashController(final int msgXOffset, final int msgYOffset) {
    splash = SplashScreen.getSplashScreen();
    if (splash == null) {
         System.out.println("Error: no splash image specified on the command line");
         return;
    }

   // compute base positions for text and progress bar
   final Dimension splashSize = splash.getSize();
   textY = splashSize.height - msgYOffset;
   barY = splashSize.height - msgYOffset;

   graph = splash.createGraphics();
   //timer.start();
}

public SplashController() {
    this(30, 30);
}

@Override
public void actionPerformed(final ActionEvent e) {
    drawSplashProgress(msg);
}

public void closeSplash() {
    if (splash != null) {
        splash.close();
    }
}

private String msg;

public void drawSplashProgress(final String msg) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            SplashController.this.msg = msg;
            graph.setRenderingHint(RenderingHints.KEY_INTERPOLATION,                              RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graph.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graph.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            // clear what we don't need from previous state
            graph.setComposite(AlphaComposite.Clear);

            final FontMetrics fm = graph.getFontMetrics();
            final Rectangle2D textsize = fm.getStringBounds(msg, graph);

            graph.fillRect(X, textY - 1, W, (int) textsize.getHeight() + 5);
            if (barPos == 0) {
                graph.fillRect(X - 3, barY, W + 10, BAR_H);
            }

            // draw new state
            graph.setPaintMode();

            // draw message
            graph.setColor(Color.BLACK);
            graph.drawString(msg, X, textY + TEXT_H);
            try {
                splash.update();
            } catch (final IllegalStateException e) {
               // can be ignored
            }
        }
     });
}

public static void main(final String args[]) throws Exception {
    final SplashController test = new SplashController();
    for (int i = 0; i < 50; i++) {
       test.drawSplashProgress("Progress step number " + i);
       Thread.sleep(250);
    }
    test.closeSplash();
}  
}

答案 1 :(得分:1)

Eclipse启动程序在所有平台上都支持此功能,但转移到SWT可能会走得太远?有详细信息here

答案 2 :(得分:0)

您可以从脚本中使用Python(或其他一些公开GUI功能的脚本语言)。

http://docs.python.org/faq/gui.html