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

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

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