在透明图形启动画面上使用drawString发出问题

时间:2015-07-24 20:44:17

标签: java swing splash-screen graphics2d

Graphics.drawString似乎没有在SplashScreen上绘制,其来源是带有透明元素的PNG。

splashscreen source with transparent elements

但是,如果我移动文本,它将覆盖PNG的可见部分,以便覆盖在" WHO ARE"位和/或底部的宠物果酱标签。

享受我的Loader课程:

package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.SplashScreen;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Loader {

    public static void main(String[] args) {        
        final SplashScreen splash = SplashScreen.getSplashScreen();
        final Graphics2D g;     
//      try {
//          GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//          ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(System.getProperty("user.dir") + "\\data\\font\\PhantElemental.ttf")));
//      } catch (IOException | FontFormatException e) {
//           System.out.println("exception found");
//      }

        if(null != splash) {
            g = splash.createGraphics();
            renderSplashFrame(g, 0, splash);
            splash.update();            
            displayUI(g, splash);
        } else {
            g = null;
        }

    }

    private static void displayUI(final Graphics2D g, final SplashScreen splash) {      
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | 
                         InstantiationException | 
                         IllegalAccessException | 
                         UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }

                for(int i = 0; i < 250; i++) {
                    renderSplashFrame(g, i, splash);
                    splash.update();
                    try {
                        Thread.sleep(10);
                    } catch(InterruptedException e) {
                        System.out.println("Thread sleep exception, on outer splashscreen timer!");
                    }

                    if(i == 249) {
                        splash.close();
                    }
                }
            }
        });
    }

    private static void renderSplashFrame(Graphics2D g, int frame, SplashScreen splash) {
        final String[] comps = {
                "Initialising", 
                "Preparing logic", 
                "Preparing UI", 
                "Finalising"};
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(30, 150, 160, 50);
        g.setPaintMode();
        g.setColor(Color.RED);
        g.setFont(new Font("Sans", Font.PLAIN, 30));
        g.drawString(comps[(frame / 63) % 4] + " . . ", 40, 160);
    }

    public class UnsupportedClassException extends Exception {

        private static final long serialVersionUID = 1L;

        public UnsupportedClassException() {
            super();    
        }       
    }

}

您可能需要使用文本的目标位置(和清除矩形)。显然,因为它使用了闪屏,你需要打包和运行,除非有一些神奇的方式来预测这个我不知道的日食!

这种方法在历史上对我来说是非透明的SplashScreen来源。

0 个答案:

没有答案