以JPanel为基础实现一个图相框

时间:2014-04-28 09:55:53   收藏:0   阅读:903

代码:

mamicode.com,码迷
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Picture extends JPanel {
    private static final long serialVersionUID = -4437881316229152596L;

    private ImageIcon icon;

    public Picture(java.net.URL imgURL) {
        icon = new ImageIcon(imgURL);
    }

    public void paintComponent(Graphics g) {
        int x = 0, y = 0;

        g.drawImage(icon.getImage(), x, y, getSize().width, getSize().height,
                this);
        
        while (true) {
            g.drawImage(icon.getImage(), x, y, this);
            
            if (x > getSize().width && y > getSize().height){
                break;
            }

            if (x > getSize().width) {
                x = 0;
                y += icon.getIconHeight();
            } else{
                x += icon.getIconWidth();
            }
        }
    }
}
mamicode.com,码迷

使用实例:

Picture picture=new Picture(getClass().getResource("/welcome.jpg"));

效果图:

mamicode.com,码迷

 

以JPanel为基础实现一个图相框,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!