Topic: 如何获得Image对象的问题! |
Print this page |
1.如何获得Image对象的问题! | Copy to clipboard |
Posted by: wqq0712 Posted on: 2006-09-06 22:53 import java.awt.event.*; import java.awt.*; import java.awt.image.*; import javax.swing.*; public class PictureMove extends JFrame { public PictureMove() { this.getContentPane().add(new MyPanel()); setTitle("用鼠标拖动图象"); setSize(400,400); setVisible(true); } public static void main(String[] args) { new PictureMove(); } } class MyPanel extends JPanel implements MouseMotionListener,MouseListener { int x=0,y=0; int dx=0,dy=0; BufferedImage bimage1,bimage2; boolean downState=false; public MyPanel() { this.setBackground(Color.WHITE); this.addMouseMotionListener(this); this.addMouseListener(this); //Image images=getToolkit().getImage("yyyy.jpg"); //问题所在!!!!!!!! Image images=new ImageIcon("yyyy.jpg").getImage(); //我自己添加的!!!!!! if(images.getWidth(this)==-1) { System.out.println("不能打开图象"); System.exit(-1); } bimage1=new BufferedImage(images.getWidth(this),images.getHeight(this),BufferedImage.TYPE_INT_ARG; bimage2=new BufferedImage(images.getWidth(this),images.getHeight(this),BufferedImage.TYPE_INT_ARG; Graphics2D g2D1=bimage1.createGraphics(); Graphics2D g2D2=bimage2.createGraphics(); g2D1.drawImage(images,0,0,this); g2D2.drawImage(images,0,0,this); g2D2.drawRect(1,1,images.getWidth(this)-3,images.getHeight(this)-3); } public void paintComponent(Graphics g) { super.paintComponent; Graphics2D g2D=(Graphics2D)g; if(!downState) g2D.drawImage(bimage1,x,y,this); else g2D.drawImage(bimage2,x,y,this); } public void mousePressed(MouseEvent e) { if(e.getX()>=x&&e.getX()<x+bimage1.getWidth(this)&&e.getY()>=y&&e.getY()<y+bimage1.getHeight(this)) { downState=true; setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); dx=e.getX()-x; dy=e.getY()-y; repaint(); } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseReleased(MouseEvent e) { this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); downState=false; repaint(); } public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e) { if(downState) { x=e.getX()-dx;y=e.getY()-dy; repaint(); } } } 上面这个程序 注释掉的那句是课本的原样~~可是我怎么运行都是:不能打开图象! 我自己写的那句 可以显示图片!! 我想知道具体那2句有什么区别呢?是否跟缓冲区有关系?希望高手帮忙详细解说~~或者指出我的错误之处!~谢谢~~ 而下面这段程序 用Image im=new ImageIcon("yyyy.jpg").getImage(); 却有问题!为什么呢! import java.awt.*; import java.awt.event.*; public class Pic extends Frame { Image im; static public void main(String args[]) { Pic p = new Pic(); p.show(); } public Pic() { super("Image Test"); //im = Toolkit.getDefaultToolkit().getImage("yyyy.jpg"); //im=this.getToolkit().getImage("yyyy.jpg"); im=new ImageIcon("yyyy.jpg").getImage(); setSize(400,400); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { dispose(); System.exit(0); } }); } public void paint(Graphics g) { g.drawImage(im, 0, 0, this); } } |
2.Re:如何获得Image对象的问题! [Re: wqq0712] | Copy to clipboard |
Posted by: 肩膀上的阳光 Posted on: 2006-09-07 10:32 有何异常否? |
3.Re:如何获得Image对象的问题! [Re: wqq0712] | Copy to clipboard |
Posted by: wqq0712 Posted on: 2006-09-09 19:29 知道的高手麻烦帮忙解释下!谢谢!! |
4.Re:如何获得Image对象的问题! [Re: wqq0712] | Copy to clipboard |
Posted by: lisliefor Posted on: 2006-09-11 10:56 wqq0712 wrote: "yyyy.jpg",确定它的路径么?如果你使用IDE,那么图像应该与类放在同一个包下;如果没,建议使用绝对路径。 |
5.Re:如何获得Image对象的问题! [Re: wqq0712] | Copy to clipboard |
Posted by: lisliefor Posted on: 2006-09-11 11:18 我猜原因可能在此: getToolkit()方法是获取该组件的工具包,而当你将JPanel加到JFrame上时,这个工具包已经发生了变化,所以无法获取到JPanel上Toolkit子类所返回的图像。 (LZ的问题很有意思,看得出,你很细心!) |
6.Re:如何获得Image对象的问题! [Re: wqq0712] | Copy to clipboard |
Posted by: lisliefor Posted on: 2006-09-11 11:21 后面那段代码问题,我也看不出来 查API,发现这么一段,不知道是否如此: javax.swing 类 ImageIcon 一个 Icon 接口的实现,它根据 Image 绘制 Icon。可使用 MediaTracker 预载根据 URL、文件名或字节数组创建的图像,以监视该图像的加载状态。 警告:此类的序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同 Swing 版本的应用程序之间进行 RMI(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。 |
7.Re:如何获得Image对象的问题! [Re: wqq0712] | Copy to clipboard |
Posted by: wqq0712 Posted on: 2006-09-12 14:30 非常感谢楼上的热心帮助! 1.应该不是相对绝对路径的关系~~我也尝试了用绝对路径~~结果一样! 2.至于你说的: 我猜原因可能在此: getToolkit()方法是获取该组件的工具包,而当你将JPanel加到JFrame上时,这个工具包已经发生了变化,所以无法获取到JPanel上Toolkit子类所返回的图像。 我觉的 也不是这个问题!因为书上有个程序 : 也是用该方法getToolkit()来获取对象~~把这个JPanel放到一个JScrollPane上 再把这个JScrollPane放到一个JPane上 最后把这个JPanel放到JFrame上~~结果依然有图象显示~~ 3. 后面代码你摘抄的那段 看不出来能解释什么呢!! 总之 谢谢你的帮助! |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |