如何在Java小程序中即时显示图像?
我用Javaapplet创建了一个简单的内存游戏。我对卡片有问题。图像在第一次出现时需要一些额外的加载时间。如何解决这个问题?我需要在卡片翻转后立即显示图像 我显示加载屏幕,直到应用程序不在如何在Java小程序中即时显示图像?,java,image,swing,applet,Java,Image,Swing,Applet,我用Javaapplet创建了一个简单的内存游戏。我对卡片有问题。图像在第一次出现时需要一些额外的加载时间。如何解决这个问题?我需要在卡片翻转后立即显示图像 我显示加载屏幕,直到应用程序不在AppStates.READY或AppStates.WAIT__启动状态,但它没有帮助 Memo.cs-加载图像的主类 public class Memo extends JApplet { //... public void init() { //...
AppStates.READY
或AppStates.WAIT__启动
状态,但它没有帮助
Memo.cs-加载图像的主类
public class Memo extends JApplet {
//...
public void init() {
//...
try {
SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { createGUI(); }});
}
catch(Exception e) {
e.printStackTrace();
}
}
private void createGUI() {
final Model model = new Model(...);
final View view = new View(model);
getContentPane().add(view, BorderLayout.CENTER);
setBackground(backgroundColor);
setPreferredSize(new Dimension(width, height));
model.setLoading(loadImages(loadingPath, format, 1));
model.setCardsImages(loadImages(cardImagePath, format, 13));
//...
model.setAppState(AppStates.PROCESS);
model.startNewGame();
view.repaint();
}
private Image[] loadImages(String path1, String path2, int count) {
Image[] imgs = new Image[count];
for(int i = 0; i < count; ++i) {
imgs[i] = getImage(getCodeBase(), path1 + i + path2);
}
return imgs;
}
}
View.cs-显示板
public class Model {
//...
private AppStates appState;
private Image[] cardsImages;
public Model(...) {
//...
appState = AppStates.INIT;
}
public void startNewGame() {
setAppState(AppStates.PROCESS);
//... - init board - table with images' id
setAppState(AppStates.WAIT_FOR_START);
}
public void setCardsImages(Image[] cardsImages) {
this.cardsImages = cardsImages;
}
public Image getCardsImage(int v) {
return cardsImages[v];
}
public AppStates getAppState() {
return appState;
}
public void setAppState(AppStates appState) {
this.appState = appState;
//...
}
//...
}
public class View extends JPanel {
//...
private Model model;
public View(Model model) {
this.model = model;
//...
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(model != null) {
switch (model.getAppState()) {
//...
case WAIT_FOR_START:
case READY:
//...
drawBoard(g2d, model.getBoard(), model.getStates(), model.getFrontTypes());
break;
}
}
repaint();
}
private void drawBoard(Graphics2D g2d, int[][] board, int[][] states, int[][] frontTypes) {
if(board != null && states != null && board.length > 0 && states.length > 0) {
for(int x = 0; x < board.length; x++) {
for(int y = 0; y < board[x].length; y++) {
if(states[x][y] != Model.HIDE) {
Image img = null;
//...
img = model.getCardsImage(board[x][y]);
g2d.drawImage(
img,
model.getFirstCardX() + x * model.getCardDistance() + x * model.getCardWidth(),
model.getFirstCardY() + y * model.getCardDistance() + y * model.getCardHeight(),
model.getCardWidth(),
model.getCardHeight(),
null);
//...
}
}
}
}
}
}
公共类视图扩展了JPanel{
//...
私有模型;
公共视图(模型){
this.model=模型;
//...
}
公共组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
如果(型号!=null){
开关(model.getAppState()){
//...
案例等待启动:
案例准备:
//...
drawBoard(g2d,model.getBoard(),model.getStates(),model.getFrontTypes());
打破
}
}
重新油漆();
}
专用void绘图板(Graphics2D g2d、int[][]板、int[][]状态、int[][]前端类型){
如果(线路板!=null&&states!=null&&board.length>0&&states.length>0){
用于(int x=0;x
来自JavaDocs
公共图像获取图像(URL)
无论图像是否存在,此方法总是立即返回。当此小程序尝试在屏幕上绘制图像时,将加载数据
改用。此方法将“阻塞”(它将停止在该代码行),直到读取图像。所以当应用程序启动时。若要使用该图像,它已完全加载
另一种解决问题的方法(不是你想要的方式)是改变
g2d.drawImage(
img,
model.getFirstCardX() + x * model.getCardDistance() + x * model.getCardWidth(),
model.getFirstCardY() + y * model.getCardDistance() + y * model.getCardHeight(),
model.getCardWidth(),
model.getCardHeight(),
null);
到
如果以异步方式加载图像(例如,使用
Applet.getImage(URL)
),当更多可用图像时,GUI将收到重新绘制的通知。预取图像的一种方法是使用。使用此类可确保在给定点加载图像。以下是文档中的精彩演示:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;
public class ImageBlaster extends Applet implements Runnable {
MediaTracker tracker;
Image bg;
Image anim[] = new Image[5];
int index;
Thread animator;
// Get the images for the background (id == 0)
// and the animation frames (id == 1)
// and add them to the MediaTracker
public void init() {
tracker = new MediaTracker(this);
bg = getImage(getDocumentBase(),
"images/background.gif");
tracker.addImage(bg, 0);
for (int i = 0; i < 5; i++) {
anim[i] = getImage(getDocumentBase(),
"images/anim"+i+".gif");
tracker.addImage(anim[i], 1);
}
}
// Start the animation thread.
public void start() {
animator = new Thread(this);
animator.start();
}
// Stop the animation thread.
public void stop() {
animator = null;
}
// Run the animation thread.
// First wait for the background image to fully load
// and paint. Then wait for all of the animation
// frames to finish loading. Finally, loop and
// increment the animation frame index.
public void run() {
try {
tracker.waitForID(0);
tracker.waitForID(1);
} catch (InterruptedException e) {
return;
}
Thread me = Thread.currentThread();
while (animator == me) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
synchronized (this) {
index++;
if (index >= anim.length) {
index = 0;
}
}
repaint();
}
}
// The background image fills the frame so we
// don't need to clear the applet on repaints.
// Just call the paint method.
public void update(Graphics g) {
paint(g);
}
// Paint a large red rectangle if there are any errors
// loading the images. Otherwise always paint the
// background so that it appears incrementally as it
// is loading. Finally, only paint the current animation
// frame if all of the frames (id == 1) are done loading,
// so that we don't get partial animations.
public void paint(Graphics g) {
if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
g.setColor(Color.red);
g.fillRect(0, 0, size().width, size().height);
return;
}
g.drawImage(bg, 0, 0, this);
if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
g.drawImage(anim[index], 10, 10, this);
}
}
}
import java.applet.applet;
导入java.awt.Color;
导入java.awt.Image;
导入java.awt.Graphics;
导入java.awt.MediaTracker;
公共类ImageBlaster扩展小程序实现可运行{
媒体跟踪器;
图像背景;
图像动画[]=新图像[5];
整数指数;
线程动画;
//获取背景图像(id==0)
//以及动画帧(id==1)
//并将它们添加到MediaTracker
公共void init(){
tracker=新的MediaTracker(本);
bg=getImage(getDocumentBase(),
“images/background.gif”);
tracker.addImage(bg,0);
对于(int i=0;i<5;i++){
anim[i]=getImage(getDocumentBase(),
“图像/动画”+i+“.gif”);
tracker.addImage(anim[i],1);
}
}
//启动动画线程。
公开作废开始(){
animator=新线程(此);
animator.start();
}
//停止动画线程。
公共停车场(){
animator=null;
}
//运行动画线程。
//首先等待背景图像完全加载
//然后等待所有的动画
//框架完成加载。最后,循环并
//增加动画帧索引。
公开募捐{
试一试{
tracker.waitForID(0);
waitForID(1);
}捕捉(中断异常e){
返回;
}
线程me=Thread.currentThread();
while(animator==me){
试一试{
睡眠(100);
}捕捉(中断异常e){
打破
}
已同步(此){
索引++;
如果(索引>=动画长度){
指数=0;
}
}
重新油漆();
}
}
//背景图像填充了框架,因此我们
//不需要在重新绘制时清除小程序。
//只需调用绘画方法。
公共空间更新(图g){
油漆(g);
}
//如果有任何错误,请绘制一个大的红色矩形
//加载图像。否则,请始终绘制
//背景,使其以增量方式显示
//正在加载。最后,仅绘制当前动画
//帧如果所有帧(id==1)都已完成加载,
//这样我们就不会得到部分动画。
公共空间涂料(图g){
if((tracker.statusAll(false)和MediaTracker.ERRORED)!=0){
g、 setColor(Color.red);
g、 fillRect(0,0,size().width,size().height);
返回;
}
g、 drawImage(bg,0,0,this);
if(tracker.statusID(1,false)=MediaTracker.COMPLETE){
g、 drawImage(动画[索引],10,10,this);
}
}
}
如果您可以保证它将始终在Java 1.4或更高版本上运行,则可以使用[ImageIO]
import java.applet.Applet;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;
public class ImageBlaster extends Applet implements Runnable {
MediaTracker tracker;
Image bg;
Image anim[] = new Image[5];
int index;
Thread animator;
// Get the images for the background (id == 0)
// and the animation frames (id == 1)
// and add them to the MediaTracker
public void init() {
tracker = new MediaTracker(this);
bg = getImage(getDocumentBase(),
"images/background.gif");
tracker.addImage(bg, 0);
for (int i = 0; i < 5; i++) {
anim[i] = getImage(getDocumentBase(),
"images/anim"+i+".gif");
tracker.addImage(anim[i], 1);
}
}
// Start the animation thread.
public void start() {
animator = new Thread(this);
animator.start();
}
// Stop the animation thread.
public void stop() {
animator = null;
}
// Run the animation thread.
// First wait for the background image to fully load
// and paint. Then wait for all of the animation
// frames to finish loading. Finally, loop and
// increment the animation frame index.
public void run() {
try {
tracker.waitForID(0);
tracker.waitForID(1);
} catch (InterruptedException e) {
return;
}
Thread me = Thread.currentThread();
while (animator == me) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
synchronized (this) {
index++;
if (index >= anim.length) {
index = 0;
}
}
repaint();
}
}
// The background image fills the frame so we
// don't need to clear the applet on repaints.
// Just call the paint method.
public void update(Graphics g) {
paint(g);
}
// Paint a large red rectangle if there are any errors
// loading the images. Otherwise always paint the
// background so that it appears incrementally as it
// is loading. Finally, only paint the current animation
// frame if all of the frames (id == 1) are done loading,
// so that we don't get partial animations.
public void paint(Graphics g) {
if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
g.setColor(Color.red);
g.fillRect(0, 0, size().width, size().height);
return;
}
g.drawImage(bg, 0, 0, this);
if (tracker.statusID(1, false) == MediaTracker.COMPLETE) {
g.drawImage(anim[index], 10, 10, this);
}
}
}