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