java添加背景图片

来源:互联网 发布:python开源代码下载 编辑:程序博客网 时间:2024/06/06 15:43
JPANEL 类 , 重写 paintCompontent 类,在paintCompontent()方法中添加图片

 

 
class Welcome extends JPanel{
    private static final long serialVersionUID = 1L;

    intwidth=0, height=0;

   Welcome(int x, int y, int width, int height){

        this.width= width;

       this.height = height;

       this.setBounds(x, y, width, height);

}

 
@Override
public void paintComponent(Graphics g){
   Graphics2D g2d = (Graphics2D) g;
   ImageIcon icon = null;
    try{
       icon = new ImageIcon("filepath\\filename");
    } catch(Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
   g2d.drawImage(icon.getImage(), 0, 0,this.getSize().width,this.getSize().height, this);
   g2d.setColor(new Color(150, 150, 180));
   g2d.drawRect(0, 0, width-1, height-1);
   g2d.setColor(Color.RED);
   g2d.setFont(g.getFont().deriveFont(Font.BOLD|Font.ITALIC,28f));
   g2d.drawString("演示", 230, 60);
   g2d.setFont(g.getFont().deriveFont(Font.BOLD|Font.ITALIC,22f));
   g2d.drawString("WINLAB", 600, 90);
   //改变颜色和字体
   g2d.setColor(Color.BLUE);
   g2d.drawLine(590, 92, 690, 92);
  }
}
0 0
原创粉丝点击