Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中撤消初始屏幕绘制_Java_Splash Screen_Graphics2d - Fatal编程技术网

如何在java中撤消初始屏幕绘制

如何在java中撤消初始屏幕绘制,java,splash-screen,graphics2d,Java,Splash Screen,Graphics2d,我是JavaGUI的初学者,问题是我正在使用一个渐变背景的启动屏幕,我想写文本,擦除它,然后写新的。 我成功地编写了文本,甚至我可以编写新的文本,但问题是新文本覆盖了旧文本,而旧文本没有被删除,因为我有一个渐变作为背景,所以我不能使用Graphics2D类的.clearRect()或.fillRect(),因为它们用纯色填充,下面是我的代码,可以帮助您指导我 SplashScreen splash = SplashScreen.getSplashScreen(); int wpro = 0, s

我是JavaGUI的初学者,问题是我正在使用一个渐变背景的启动屏幕,我想写文本,擦除它,然后写新的。 我成功地编写了文本,甚至我可以编写新的文本,但问题是新文本覆盖了旧文本,而旧文本没有被删除,因为我有一个渐变作为背景,所以我不能使用Graphics2D类的.clearRect()或.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);
/******************************************************************/
您基本上需要“清除”图形上下文

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) {
                }

            }
        });
    }
}


您在IDE Netbeans或Eclipse中使用了什么?这个问题如何取决于IDE,我不这么认为,它甚至不重要?我没有看到任何渐变绘制代码。您的帖子演示了这个问题。正如您所知,我的问题是因为Netbeans附带了一个运行时容器,它可以创建自己的启动屏幕,所以整个开发过程变得更加简单。搞定它。我不是在画渐变,它是以前在我的启动屏幕图像中定义的manifest.mf文件。我真的很感谢你!很好,我遇到了一些问题,我将在这里与您共享链接,并请您回答这个问题,@user1703281别忘了向上投票是非常感谢的;)对不起,我的名声还不够