import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
public class FirstSample{
public static void main(String[] args){
ImageFrame frame=new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("Michael");
setSize(WIDTH,HEIGHT);
ImagePanel panel=new ImagePanel();
add(panel);
}
public static final int WIDTH=800;
public static final int HEIGHT=600;
}
class ImagePanel extends JPanel{
public ImagePanel(){
try{
image=ImageIO.read(new File("b.gif"));
}
catch(IOException e){
e.printStackTrace();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
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);
}
private Image image;
}
图片平铺
最新推荐文章于 2021-03-02 06:16:11 发布