如何在Java中撤消启动画面绘画

如何在Java中撤消启动画面绘画

本文介绍了如何在Java中撤消启动画面绘画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java GUI的初学者,问题是我使用的是具有渐变作为背景的启动屏幕,因此我想编写文本,擦除文本然后编写新的文本.我设法写了文本,甚至我也可以写新的文本,但是问题是新文本会覆盖旧文本,并且旧文本不会被擦除,因为我有渐变作为背景,所以我不能使用.clearRect()或Graphics2D Class的.fillRect(),因为它们会用纯色填充它,所以这是我的代码,可以帮助您指导我.

I am a beginner in Java GUI, The problem is that I am using a splash screen which has a gradient as a background and I want to write the text , erase it and then write the new one.I Managed to write the text and even I can write the new one but the problem is that the new text overwrites the old text, and the old text is not erased, Since I have a gradient as background so I cannot use .clearRect() or .fillRect() of Graphics2D Class since they fill it with a solid color, here is my code which can help you to guide me.

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

预先感谢!

推荐答案

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

You seem to be missing one critical element...

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

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

You basically need to "clear" the graphics context...

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中撤消启动画面绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:25