Java中图形图像得到了很好的支持,gif,jpg,png都可以当作直接的资源导入设备,不再像VC图形编程一样,高于256色的图形导入就显得有些繁琐。
JAVA中的图像导入分很多方向,也有很多方法,本文简单地介绍一下
1.首先是applet应用小程序的图像导入:
看代码 :
private Image[] img;//define a array of Images
private MediaTracker mt;//define a tracker of these images
<img none';="" document.getelementbyid('_124_632_open_text').style.display="none" ;="" document.getelementbyid('_124_632_closed_image').style.display="inline" document.getelementbyid('_124_632_closed_text').style.display="inline" ;"="" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 100%; ">public void Init(){
Current=0
} catch (InterruptedException e) {
e.printStackTrace();
}
}//make these images"T1.gir" to "T10.gir" ready for our Graphics
<img none';="" document.getelementbyid('_548_631_open_text').style.display="none" ;="" document.getelementbyid('_548_631_closed_image').style.display="inline" document.getelementbyid('_548_631_closed_text').style.display="inline" ;"="" alt="" src="https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif" align="top" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 100%; display: inline; ">public void paint(Graphics g){
g.drawImage(img[CurrentNumber], 0, 0,this);
CurrentNumeber=++Current%10;
private MediaTracker mt;//define a tracker of these images
<img none';="" document.getelementbyid('_124_632_open_text').style.display="none" ;="" document.getelementbyid('_124_632_closed_image').style.display="inline" document.getelementbyid('_124_632_closed_text').style.display="inline" ;"="" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 100%; ">public void Init(){
Current=0
} catch (InterruptedException e) {
e.printStackTrace();
}
}//make these images"T1.gir" to "T10.gir" ready for our Graphics
<img none';="" document.getelementbyid('_548_631_open_text').style.display="none" ;="" document.getelementbyid('_548_631_closed_image').style.display="inline" document.getelementbyid('_548_631_closed_text').style.display="inline" ;"="" alt="" src="https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif" align="top" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 100%; display: inline; ">public void paint(Graphics g){
g.drawImage(img[CurrentNumber], 0, 0,this);
CurrentNumeber=++Current%10;
repaint();
}
}
上面的代码利用媒体跟踪器保证每幅图片的就绪状态,然后一一绘图,其实这段代码在applet中实现了一系列动画效果。
2.再看在Application中利用swing包,构建带有图片的窗体
代码:
JFrame jf=new JFrame();
Image m=new Image();
m=Toolkit.getDefaultToolkit().getImage("top.jpg");//get the image you want
JLabel lb=new JLabel();
lb.setIcon(new ImageIcon(m)); //add a label contains the image which have to change to ImageIcon;
jf.getContentPane().add(lb); //add the label to our Frame;
Image m=new Image();
m=Toolkit.getDefaultToolkit().getImage("top.jpg");//get the image you want
JLabel lb=new JLabel();
lb.setIcon(new ImageIcon(m)); //add a label contains the image which have to change to ImageIcon;
jf.getContentPane().add(lb); //add the label to our Frame;
当然,也可以自己写一个窗口类,然后重载其中的paint方法
代码:
Image m=new Image();
m=Toolkit.getDefaultToolkit().getImage("top.jpg");
<img none';="" document.getelementbyid('_102_151_open_text').style.display="none" ;="" document.getelementbyid('_102_151_closed_image').style.display="inline" document.getelementbyid('_102_151_closed_text').style.display="inline" ;"="" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 100%; ">public void paint(Graphics g){
g.drawImage(m,insets.left,insets.top,this);
}
m=Toolkit.getDefaultToolkit().getImage("top.jpg");
<img none';="" document.getelementbyid('_102_151_open_text').style.display="none" ;="" document.getelementbyid('_102_151_closed_image').style.display="inline" document.getelementbyid('_102_151_closed_text').style.display="inline" ;"="" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align="top" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 100%; ">public void paint(Graphics g){
g.drawImage(m,insets.left,insets.top,this);
}