自学JAVA求助 在JFrame中显示图片!!

「已注销」 2010-03-12 08:22:09
*************************************************
import java.awt.Container;

import javax.swing.JFrame;




public class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("显示图片");

setSize(600,400);

//将 panel加入到Frame中
ImagePanel panel=new ImagePanel();


Container contentPane=getContentPane();
contentPane.add(panel);
}
}

*****************************************************

import java.awt.*;
import javax.swing.*;

public class ImageShow
{
public static void main(String arg[])
{
ImageFrame frame=new ImageFrame();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


frame.setVisible(true);
}
}

*********************************************************
以上是两个辅助的类
*********************************************************
import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel
{

private Image image[];
String[] filename={"E:\\eclipse\\workspace\\com\\01.JPG","E:\\eclipse\\workspace\\com\\02.JPG"
,"E:\\eclipse\\workspace\\com\\03.JPG","E:\\eclipse\\workspace\\com\\hudie.GIF"};
ImageIcon[] images;
int i=0; //循环变量
MediaTracker tracker=new MediaTracker(this);

public void paintComponent(Graphics g)
{
super.paintComponents(g);
for(i=0;i<4;i++){
images[i]=new ImageIcon(filename[i]);

image[i]=images[i].getImage();
System.out.print(1);
tracker.addImage(image[i],i);
System.out.print(1);
try
{
tracker.waitForID(1);

}catch(InterruptedException exception)
{System.out.print(222222221);}
int imageWidth=image[i].getWidth(this);
int imageHeight=image[i].getHeight(this);
int frameWidth=getWidth();
int frameHeight=getHeight();
int x=(frameWidth-imageWidth)/2;
int y=(frameHeight- imageHeight)/2;
g.drawImage(image[i], x, y, null);
}

}

}
*******************************************************************

问题出在类 ImagePanel 中,我想在JFrame中连续的显示这4张图片不知道为什么不可以,就是把public void paintComponent(Graphics g)中的for{i=0;i++;i<4}去掉了,还是不可以
把 ImagePanel 类改成以下方法 显示一张图片才可以,希望高人帮忙解答,怎么让它可以连续的显示几张图片!!

*********************************************************************
public class ImagePanel extends JPanel
{

private Image image;
ImageIcon images=new ImageIcon("E:\\eclipse\\workspace\\com\\01.JPG");
public ImagePanel()
{


}
public void paintComponent(Graphics g)

{
image=images.getImage();

MediaTracker tracker=new MediaTracker(this);

tracker.addImage(image, 1);
try
{
tracker.waitForID(1);

}catch(InterruptedException exception)
{}
super.paintComponents(g);

int imageWidth=image.getWidth(this);
int imageHeight=image.getHeight(this);

int frameWidth=getWidth();
int frameHeight=getHeight();

int x=(frameWidth-imageWidth)/2;
int y=(frameHeight- imageHeight)/2;


g.drawImage(image, x, y, null);


}

}
...全文
414 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
残雪龙骑 2010-03-13
  • 打赏
  • 举报
回复

import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

private Image image[]=new Image[4];
String[] filename = { "resource/01.JPG",
"resource/02.JPG",
"resource/03.gif",
"resource/hudie.GIF" };
public static Toolkit tk = Toolkit.getDefaultToolkit();
public static Image img = tk.getImage(ImagePanel.class.getClassLoader().getResource("resource/03.gif"));
ImageIcon[] images = new ImageIcon[4];
int i = 0; // 循环变量
MediaTracker tracker = new MediaTracker(this);
public void paintComponent(Graphics g) {

for (i = 0; i < 4; i++) {
images[i] = new ImageIcon(tk.getImage(ImagePanel.class.getClassLoader().getResource(filename[i])));
image[i] = images[i].getImage();

System.out.print(1);
tracker.addImage(image[i], i);
System.out.print(1);
try {
tracker.waitForID(1);

} catch (InterruptedException exception) {
System.out.print(222222221);
}
int imageWidth = image[i].getWidth(this);
int imageHeight = image[i].getHeight(this);
int frameWidth = getWidth();
int frameHeight = getHeight();
int x = (frameWidth - imageWidth) / 2;
int y = (frameHeight - imageHeight) / 2;
g.drawImage(image[i], x, y, null);
}

}

}


由于图片位置问题,我加了一个package叫resource,里面放图片,用相对路径来读取图片,并引用了Toolkit对象,经测试。没有问题。。其它两个类没有改动
残雪龙骑 2010-03-13
  • 打赏
  • 举报
回复
用Thread.sleep(1000);
代替for(aa=0;aa<55555555;aa++){
//for(bb=0;bb<6666666;bb++){}

}
试一试
「已注销」 2010-03-13
  • 打赏
  • 举报
回复
import java.awt.*;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

//private Image image[]=new Image[4];
String[] filename = { "D:\\workspace\\myfirst\\com\\resource\\01.JPG",
"D:\\workspace\\myfirst\\com\\resource\\02.JPG",
"D:\\workspace\\myfirst\\com\\resource\\03.JPG",
"D:\\workspace\\myfirst\\com\\hudie.GIF" };
public static Toolkit tk = Toolkit.getDefaultToolkit();
public static Image imge;
ImageIcon[] images = new ImageIcon[4];
int i = 0,aa,bb; // 循环变量

MediaTracker tracker = new MediaTracker(this);
public void paintComponent(Graphics g) {
System.out.println("paintComponen方法执行");
for (i = 0; i < 4; i++) {
images[i] = new ImageIcon(filename[i]);
imge = images[i].getImage();

tracker.addImage(imge, i);

try {
tracker.waitForID(1);

} catch (InterruptedException exception) {
;
}
int imageWidth = imge.getWidth(this);
int imageHeight = imge.getHeight(this);
int frameWidth = getWidth();
int frameHeight = getHeight();
int x = (frameWidth - imageWidth) / 2;
int y = (frameHeight - imageHeight) / 2;
for(aa=0;aa<55555555;aa++){
//for(bb=0;bb<6666666;bb++){}

}
g.drawImage(imge, x, y, null);
}

}

}
相对路径老是出错,直接该成了绝对路径,不过没达到我想要的效果,
我想是让它先显示第一张图片,然后过一会显示第二张,然后第三,第四。。。
直接慢慢研究
「已注销」 2010-03-13
  • 打赏
  • 举报
回复
抛出了异常。。。

我在paintComponent(Graphics g)方法for循环的前面加了一个输出内容输出了但是接下来就是这个异常

好像是
images[i] = new ImageIcon(tk.getImage(ImagePanel.class.getClassLoader().getResource(filename[i])));这个的问题!


Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
「已注销」 2010-03-13
  • 打赏
  • 举报
回复
虽然还是没有解决问题不知道什么原因。。。。但是分还是给你了

62,614

社区成员

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

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