简单问题(极品菜鸟问题)下午就揭贴

snake_eye 2003-06-02 02:58:45
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.image.ImageObserver;
import java.net.URL;

public class MyApplet extends Applet implements ImageObserver{
Image img;
String imgPath = "";
int xpoint = 100, ypoint = 100;

public void init() {

setBackground(Color.white); //设置背景色
setForeground(Color.blue); //设置前景色
imgPath ="test.jpg"; //设置图象文件路径
}

public void paint(Graphics g) {
URL imgURL = getDocumentBase();
//Returns an absolute URL naming the directory of the document in which the applet is embedded.
img = getImage(imgURL, imgPath);}
}
怎么在html里显示不出图象来啊?我的test,jpg和html都在一个文件加里
希望指点指点,我是菜鸟
...全文
33 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
snake_eye 2003-06-02
  • 打赏
  • 举报
回复
给分了!
mymoto 2003-06-02
  • 打赏
  • 举报
回复
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.image.ImageObserver;
import java.net.URL;

public class MyApplet extends Applet implements ImageObserver{
Image img;
String imgPath = "";
int xpoint = 100, ypoint = 100;

public void init() {

setBackground(Color.white); //设置背景色
setForeground(Color.blue); //设置前景色
Container cp = getContentPane();
cp.add(new JLabel("test.jpg"));

}
不就可以了,为什么要用paint

zhjjava 2003-06-02
  • 打赏
  • 举报
回复
你的URL创建错误!还需要捕获的!
try {
URL url = new URL(getDocumentBase()+imgPath);

} catch (MalformedURLException e) {
textField1.setText(e.toString());
}
mymoto 2003-06-02
  • 打赏
  • 举报
回复
你在init()里面并没有调用Image啊
snake_eye 2003-06-02
  • 打赏
  • 举报
回复
还是不行啊
我的html是这样写的:
<html>
<head>
<title>"MyApplet text"
</title>
<body>
<applet code="MyApplet.class" width=500 height=500 /applet>
</body>
</head>
</html>
wanglh2000 2003-06-02
  • 打赏
  • 举报
回复
有两点问题:
1.img为空。
2.没有绘制这个图像。
img = getImage(imgURL, imgPath);
if(img ==null){//判断是不是为空
System.out.println("Image is null!");
}else{//不为空则显示图像
g.drawImage(img,xpoint,ypoint,img.getWidth(this),img.getHeight(this),this);
}


}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧