java - 如何撤消 java 中的启动画面绘制

标签 java splash-screen graphics2d

我是 Java GUI 的初学者,问题是我正在使用一个具有渐变背景的启动画面,我想写文本,删除它然后写新的。 我设法写了文本,甚至我可以写新的,但问题是新文本覆盖了旧文本,旧文本没有被删除,因为我有渐变作为背景所以我不能使用 .clearRect()或 Graphics2D 类的 .fillRect(),因为它们用纯色填充,这是我的代码,可以帮助您指导我。

SplashScreen splash = SplashScreen.getSplashScreen();
int wpro = 0, strx = 491, stry = 414, prox = 491, proy = 389;    
Graphics2D str = splash.createGraphics();
str.setComposite(AlphaComposite.Clear);
str.setPaintMode();
str.setColor(Color.WHITE);
Font font = new Font("", 0, 13); 
str.setFont(font);
str.drawString("Loading Login Class ...", strx, stry);
splash.update();
try{
    Thread.sleep(5000);
}catch(InterruptedException ex){
}
str.drawString("Loading Main Class ...", strx, stry);
splash.update();
try{
    Thread.sleep(5000);
}catch(InterruptedException ex){
}
splash.close();

提前致谢!

最佳答案

您似乎缺少一个关键要素...

/******************************************************************/
str.setComposite(AlphaComposite.Clear);
str.fillRect(0, 0, width, height);
/******************************************************************/

您基本上需要“清除”图形上下文...

enter image description here enter image description here

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import sun.font.FontManager;

public class TestSplashScreen02 {

    public static void main(String[] args) {
        new TestSplashScreen02();
    }

    public TestSplashScreen02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                SwingWorker worker = new SwingWorker() {
                    @Override
                    protected Object doInBackground() throws Exception {

                        SplashScreen splash = SplashScreen.getSplashScreen();
                        int width = 400;
                        int height = 300;
                        Graphics2D str = splash.createGraphics();
                        /******************************************************************/
                        str.setComposite(AlphaComposite.Clear);
                        str.fillRect(0, 0, width, height);
                        /******************************************************************/
                        str.setPaintMode();
                        str.setColor(Color.WHITE);
                        Font font = str.getFont().deriveFont(Font.BOLD, 24);
                        FontMetrics fm = str.getFontMetrics(font);
                        str.setFont(font);

                        String text = "Loading Login Class ...";

                        int x = (width - fm.stringWidth(text)) / 2;
                        int y = (height - fm.getHeight()) / 2;
                        str.drawString(text, x, y + fm.getAscent());

                        splash.update();
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                        }
                        str.setComposite(AlphaComposite.Clear);
                        str.fillRect(0,0,400,300);
                        str.setPaintMode();

                        text = "Loading Main Class ...";

                        x = (width - fm.stringWidth(text)) / 2;
                        y = (height - fm.getHeight()) / 2;
                        str.drawString(text, x, y + fm.getAscent());
                        System.out.println("Update...");
                        splash.update();
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                        }
                        splash.close();
                        return null;
                    }
                };

                worker.execute();
                try {
                    worker.get();
                } catch (InterruptedException | ExecutionException ex) {
                }

            }
        });
    }
}

关于java - 如何撤消 java 中的启动画面绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17436920/

相关文章:

java - 如何在 ubuntu 上设置 Pycharm 和 JDK

java - 升级 Android 应用程序时的 ClassNotFoundException 错误报告

java - 如果 JTable 包含字符串,如何在 JTable 上使用 RowFilter.dateFilter

Java SplashScreen 透明图像

java - 如何在拖动光标时画出没有间隙的细线?

java - 在 JNLP 文件中使用 JAR 参数

javascript - 如何创建一个简单的启动画面/页面?

excel - VBA 启动画面

java - 使用 CMYK 颜色空间在 java 中写入图像

java - 在 JPanel 中生成包含多个点的图