0
我想用下面列出的图像显示启动屏幕十秒钟。我以前从未使用过splashScreen,所以我不确定它是如何工作的。为什么我的启动画面没有显示出来?
问:如何使SplashScreen显示.jpg?
代码:
import java.awt.*;
import java.awt.event.*;
public class SplashDemo extends JFrame {
private static final long serialVersionUID = 1L;
public SplashDemo() {
super("SplashScreen demo");
setSize(300, 200);
setLayout(new BorderLayout());
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
System.out.println("SplashScreen.getSplashScreen() returned null");
return;
}
Graphics2D g = splash.createGraphics();
if (g == null) {
System.out.println("g is null");
return;
}
for(int i=0; i<100; i++) {
renderSplashFrame(g, i);
splash.update();
try {
Thread.sleep(90);
}
catch(InterruptedException e) {
}
}
splash.close();
setVisible(true);
}
public static void main (String args[]) {
SplashDemo test = new SplashDemo();
}
}
新的代码在这里:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SplashDemo extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
static void renderSplashFrame(Graphics2D g, int frame) {
g.setComposite(AlphaComposite.Clear);
g.fillRect(120,140,200,40);
g.setPaintMode();
g.setColor(Color.BLACK);
}
public SplashDemo() throws IOException {
super("SplashScreen demo");
setSize(300, 200);
setLayout(new BorderLayout());
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
System.out.println("SplashScreen.getSplashScreen() returned null");
return;
}
Graphics2D g = splash.createGraphics();
BufferedImage img = ImageIO.read(new File("splash.gif"));
g.drawImage(img, 0, 0, null);
for(int i=0; i<100; i++) {
renderSplashFrame(g, i);
splash.update();
try {
Thread.sleep(90);
}
catch(InterruptedException e) {
}
}
splash.close();
setVisible(true);
toFront();
}
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
}
这是我的新代码。该代码运行并显示给控制台,但不会在屏幕上显示任何内容。在代码中是否有我缺少的东西?我想让启动画面显示一段时间。
新代码:这恰好是在Oracle的网站(现在还没有显示任何)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SplashDemoTest extends Frame implements ActionListener {
static void renderSplashFrame(Graphics2D g, int frame) {
final String[] comps = {"foo", "bar", "baz"};
g.setComposite(AlphaComposite.Clear);
g.fillRect(120,140,200,40);
g.setPaintMode();
g.setColor(Color.BLACK);
g.drawString("Loading "+comps[(frame/5)%3]+"...", 120, 150);
}
public SplashDemoTest() {
super("SplashScreen demo");
setSize(300, 200);
setLayout(new BorderLayout());
Menu m1 = new Menu("File");
MenuItem mi1 = new MenuItem("Exit");
m1.add(mi1);
mi1.addActionListener(this);
this.addWindowListener(closeWindow);
MenuBar mb = new MenuBar();
setMenuBar(mb);
mb.add(m1);
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
System.out.println("SplashScreen.getSplashScreen() returned null");
return;
}
Graphics2D g = splash.createGraphics();
BufferedImage img;
try {
img = ImageIO.read(new File("splash.gif"));
g.drawImage(img, 0, 0, null);
} catch (IOException e1) {
e1.printStackTrace();
}
if (g == null) {
System.out.println("g is null");
return;
}
for(int i=0; i<100; i++) {
renderSplashFrame(g, i);
splash.update();
try {
Thread.sleep(90);
}
catch(InterruptedException e) {
}
}
splash.close();
setVisible(true);
toFront();
}
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
private static WindowListener closeWindow = new WindowAdapter(){
public void windowClosing(WindowEvent e){
e.getWindow().dispose();
}
};
public static void main (String args[]) {
SplashDemoTest test = new SplashDemoTest();
}
}
http://stackoverflow.com/questions/10521192/grey-splash-screen-due-to-no-repainting也可能会帮助你看看你是否想为你的场景创建自己的类 – demongolem