java swing background image

前端 未结 3 623
北荒
北荒 2020-11-30 12:52

I am using JFrame and I have kept a background image on my frame. Now the problem is that the size of image is smaller then the size of the frame so i have to keep the same

3条回答
  •  粉色の甜心
    2020-11-30 13:41

    Another way to tile an image is with TexturePaint:

    public class TexturePanel extends JPanel {
    
        private TexturePaint paint;
    
        public TexturePanel(BufferedImage bi) {
            super();
            this.paint = new TexturePaint(bi, new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(paint);
            g2.fill(new Rectangle(0, 0, getWidth(), getHeight()));
        }
    }
    

提交回复
热议问题