lizhizuoyongzhe 2015-06-18 01:52 采纳率: 20%
浏览 2083

java swing中怎么让面板的背景图片缩放

public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;

super.paintComponent(g2);

//背景图片
int x=0,y=0;
java.net.URL imgURL=getClass().getResource("t0156b516cea6edd501.jpg");
ImageIcon icon=new ImageIcon(imgURL);
g2.drawImage(icon.getImage(),this.getX(),this.getY(),this.getWidth(),this.getHeight(),this);
while(true){
g2.drawImage(icon.getImage(),x,y,this);
if(x>this.getWidth() && y>this.getHeight())break;
if(x>this.getHeight()){
x=0;
y+=icon.getIconHeight();
}else{
x+=icon.getIconWidth();
}
}

}

由于面板没图片大 所以图片只显示了一部分 哪位大神知道在这代码的基础上讲图片匹配上面板的大小

  • 写回答

1条回答 默认 最新

  • JonsonJiao 2015-06-18 03:36
    关注

    你加这个while干什么啊?把他去掉不就是将图片铺满面板么?

    while(true){
    g2.drawImage(icon.getImage(),x,y,this);
    if(x>this.getWidth() && y>this.getHeight())break;
    if(x>this.getHeight()){
    x=0;
    y+=icon.getIconHeight();
    }else{
    x+=icon.getIconWidth();
    }
    }
    
    评论

报告相同问题?