我想使用下面列出的图像显示启动画面十秒钟。我之前从未使用过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();
}
}
答案 0 :(得分:1)
使用g.drawImage()
,我想是显示jpg文件应该是什么。
因此,在splash.createGraphics(
之后,您可以使用该方法绘制图像。