如何在Java Applet中立即显示图像?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java Applet中立即显示图像?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3593字,纯文字阅读大概需要6分钟。
内容图文
![如何在Java Applet中立即显示图像?](/upload/InfoBanner/zyjiaocheng/744/1a72813f3d4848d998283d5d57cb939a.jpg)
我在Java Applet中创建了一个简单的内存游戏.卡有问题.在第一次出现时,图像需要一些额外的加载时间.如何解决?我需要在卡片翻转后立即显示图像.
我显示加载屏幕,直到应用程序不在AppStates.READY或AppStates.WAIT_FOR_START状态,但它没有帮助.
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;
}
}
Model.cs – 保存图像和应用程序状态,init板
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;
//...
}
//...
}
View.cs – 显示板
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);
//...
}
}
}
}
}
}
解决方法:
来自JavaDocs ..
public Image getImage(URL url)
..
This method always returns immediately, whether or not the image exists. When this applet attempts to draw the image on the screen, the data will be loaded.
请改用ImageIO.read(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);
至..
g2d.drawImage(
img,
model.getFirstCardX() + x * model.getCardDistance() + x * model.getCardWidth(),
model.getFirstCardY() + y * model.getCardDistance() + y * model.getCardHeight(),
model.getCardWidth(),
model.getCardHeight(),
this);
如果图像是异步加载的(例如,使用Applet.getImage(URL)),GUI会在更多可用时通知重绘.
内容总结
以上是互联网集市为您收集整理的如何在Java Applet中立即显示图像?全部内容,希望文章能够帮你解决如何在Java Applet中立即显示图像?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。