日期:2014-05-20 浏览次数:20797 次
import java.awt.*;
import java.net.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;
public class ImageTest2
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
MyFrame frame=new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
setSize(1200,600);
setTitle("ImageTest2");
setLocationByPlatform(true);
ImageComponent component=new ImageComponent();
add(component);
}
}
class ImageComponent extends JComponent
{
// public ImageComponent()
// {
// try
// {
// image=ImageIO.read(new File("D:\\demo\\戴眼镜的猫2.jpg"));
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
// }
public void paintComponent(Graphics g)
{
Toolkit kit=Toolkit.getDefaultToolkit(); //*1
image=kit.getImage("d:\\demo\\戴眼镜的猫2.jpg"); //*2
if(image==null)return ;
int imageWidth=image.getWidth(this);
int imageHeight=image.getHeight(this);
g.drawImage(image,0,0,null);
for(int i=0;i*imageWidth<=getWidth();i++)
for(int j=0;j*imageHeight<=getHeight();j++)
if(i+j>0)g.copyArea(0,0,imageWidth,imageHeight,i*imageWidth,j*imageHeight);//*3
}
private Image image;
}