2 lizhizuoyongzhe lizhizuoyongzhe 于 2015.06.18 09:52 提问

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个回答

tongyi55555
tongyi55555   2015.06.18 11: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();
}
}
Csdn user default icon
上传中...
上传图片
插入图片