【发布时间】:2010-04-13 16:37:59
【问题描述】:
我正在尝试在基于 Web 的应用程序上使用的 Java 小程序上显示 JPEG 图像和移动点。但是,当我运行小程序时它工作正常,但是当我从 JSP 页面显示小程序时,我得到了移动的点,但没有 JPEG 图像。
是否存在需要 JPEG 的特定文件夹?
这是我在屏幕上绘制图片和移动点的两种方法。
public class mapplet extends Applet implements Runnable {
int x_pos = 10;
int y_pos = 100;
int radius = 20;
Image img, img2;
Graphics gr;
URL base;
MediaTracker m;
@Override
public void init() {
mt = new MediaTracker(this);
try {
//getDocumentbase gets the applet path.
base = getCodeBase();
img = getImage(base, "picture.jpg");
m.addImage(img, 1);
m.waitForAll();
} catch (InterruptedException ex) {
Logger.getLogger(movement.class.getName()).log(Level.SEVERE, null, ex);
}
public void paint (Graphics g) {
g.drawImage(img, 0, 0, this);
// set color
g.setColor (Color.red);
// paint a filled colored circle
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
下面的代码一是来自jsp页面的调用
<applet archive="mapplet.jar" code="myapplets/mapplet.class" width=350 height=200>
</applet>
jar文件和图片和jsp页面在同一个文件夹下,在应用的web部分也有一个文件夹,里面有applet的class和image的内容。小程序加载正常,但图片不显示。我认为导致问题的不是代码,而是图片的位置。
谢谢
【问题讨论】:
-
你现在如何加载图像?请给我们看一些代码
-
看起来是路径问题。你能提供更多细节吗?您的小程序代码和图像在服务器上的位置会有所帮助
-
@Suresh:这是一个很好的建议,我不得不在我的(几乎接近一个)答案中重复它。