Applet的图像丢失 [英] Applet image missing
本文介绍了Applet的图像丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好再次成员#1,
的CatchTheCreature Applet类应该显示的图像中的由时间延迟的不同位置被重画,但由于某些原因没有被显示的图象。
进口java.awt.Color中;
进口java.awt.Graphics;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseListener;
进口了java.util.Random; 进口javax.swing.ImageIcon中;
进口javax.swing.JApplet中;
进口javax.swing.Timer中; 公共类CatchTheCreature扩展JApplet的{私人诠释高度= 300;
私人诠释宽度= 600;
私人最终诠释延迟= 1001;私人ImageIcon的形象;
私人定时器定时器;
私人诠释的x,y;
私人INT计数器= 0;
随机GN =新的随机();公共无效的init(){
DotListener点=新DotListener();
addMouseListener将(点); 图像=新的ImageIcon(Monster.png); 定时器=新定时器(延迟,新timerListener());
X = 40;
Y = 40;
。的getContentPane()的setBackground(Color.black);}//动作监听器方法
私有类timerListener实现的ActionListener {
公共无效的actionPerformed(ActionEvent的五){ X = gn.nextInt(宽度);
Y = gn.nextInt(高度); 重绘();
}}私有类DotListener实现的MouseListener { 公共无效鼠标pressed(事件的MouseEvent){ } @覆盖
公共无效的mouseClicked(事件的MouseEvent){
如果(event.getX()>(X)及与放大器; event.getX()≤(X + 60)
&功放;&安培; event.getY()&下; (γ+ 60)及与放大器; event.getY()> (y))为{
X = gn.nextInt(宽度);
Y = gn.nextInt(高度);
计数器=计数器+ 1;
重绘();
}
} @覆盖
公共无效的mouseEntered(事件的MouseEvent){ } @覆盖
公共无效的mouseExited(事件的MouseEvent){ } @覆盖
公共无效的mouseReleased(事件的MouseEvent){ }}公共无效漆(图形G){
super.paint(G);
g.setColor(Color.yellow);
image.paintIcon(此,G,X,Y);
g.drawString(accuratly点击了:+专柜,5,15);
}公共无效的start(){
timer.start();}公共无效停止(){
timer.stop();
}
}
这是我的HTML文件
<小程序code = CatchTheCreature宽度= 250 HEIGHT = 300> < /小程序>
如果有人能告诉我怎样可以显示applet的图像图标我将非常感激。
感谢
解决方案
..形象=新的ImageIcon(Monster.png);
块引用>
- 基于
字符串
构造器的ImageIcon
presumes的字符串
重新present一个文件
。- 一个沙箱小程序不能访问
文件
的对象,但可以访问网址
■从同一$ C到来$ C碱基/文档基。- 使用
getDocumentBase()
/得到codeBase的()
与该图像的相对路径,小应用程序的可移植性(假设图像也上传到同一个地方)。Hello again Stackoverflow members,
The CatchTheCreature Applet class is supposed to display an image being repainted in different locations by a time delay, but for some reason the image is not being displayed.
import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.Timer; public class CatchTheCreature extends JApplet { private int height = 300; private int width = 600; private final int delay = 1001; private ImageIcon image; private Timer timer; private int x, y; private int counter = 0; Random gn = new Random(); public void init() { DotListener dot = new DotListener(); addMouseListener(dot); image = new ImageIcon("Monster.png"); timer = new Timer(delay, new timerListener()); x = 40; y = 40; getContentPane().setBackground(Color.black); } // Action Listener Methods private class timerListener implements ActionListener { public void actionPerformed(ActionEvent e) { x = gn.nextInt(width); y = gn.nextInt(height); repaint(); } } private class DotListener implements MouseListener { public void mousePressed(MouseEvent event) { } @Override public void mouseClicked(MouseEvent event) { if (event.getX() > (x) && event.getX() < (x + 60) && event.getY() < (y + 60) && event.getY() > (y)) { x = gn.nextInt(width); y = gn.nextInt(height); counter = counter + 1; repaint(); } } @Override public void mouseEntered(MouseEvent event) { } @Override public void mouseExited(MouseEvent event) { } @Override public void mouseReleased(MouseEvent event) { } } public void paint(Graphics g) { super.paint(g); g.setColor(Color.yellow); image.paintIcon(this, g, x, y); g.drawString("Clicked accuratly: " + counter, 5, 15); } public void start() { timer.start(); } public void stop() { timer.stop(); }
}
This is my html file
<applet code = CatchTheCreature width = 250 height = 300> </applet>
If someone can tell me how i can display the image icon on the applet I would be very grateful.
Thanks
解决方案..image = new ImageIcon("Monster.png");
- The
String
based constructor toImageIcon
presumes theString
to represent aFile
.- A sand-boxed applet cannot access
File
objects, but can accessURL
s coming from the same code base/document base.- Use
getDocumentBase()
/getCodeBase()
with an relative path to the image, and the applet will be portable (assuming the image is also uploaded to the same place).这篇关于Applet的图像丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文