Java JPanel tiled background image

前端 未结 3 1448
野性不改
野性不改 2020-12-18 08:57

I currently have the code for creating a JOptionPane that tiles an image to the background no matter the size I set it to :)

package test;
import java.awt.*;         


        
3条回答
  •  盖世英雄少女心
    2020-12-18 09:24

    An instance of java.awt.TexturePaint provides a convenient way to tile a BufferedImage. Related examples may be seen here. Given a TexturePaint, you can fill a component's background fairly easily, as shown here.

        private TexturePaint paint;
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(paint);
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }
    

    image

提交回复
热议问题