图片平铺

来源:互联网 发布:雨滴美化软件 编辑:程序博客网 时间:2024/06/02 12:18

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;
}

原创粉丝点击