java swing background image

前端 未结 3 622
北荒
北荒 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()));
        }
    }
    
    0 讨论(0)
  • 2020-11-30 13:46

    You want something like the background image of the windows desktop, when using the background image multiple times instead of resizing it or just display it centered?

    You only have to keep the image once and just paint it multiple times in the paintComponent method.

    0 讨论(0)
  • 2020-11-30 13:50

    It sounds as though you are talking about tiling vs. stretching, though it's not clear which behaviour you want.

    This program has examples of both:

    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.io.IOException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.AbstractAction;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            final Image image = ImageIO.read(new URL("http://sstatic.net/so/img/logo.png"));
            final JFrame frame = new JFrame();
            frame.add(new ImagePanel(image));
            frame.setSize(800, 600);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    
    @SuppressWarnings("serial")
    class ImagePanel extends JPanel {
        private Image image;
        private boolean tile;
    
        ImagePanel(Image image) {
            this.image = image;
            this.tile = false;
            final JCheckBox checkBox = new JCheckBox();
            checkBox.setAction(new AbstractAction("Tile") {
                public void actionPerformed(ActionEvent e) {
                    tile = checkBox.isSelected();
                    repaint();
                }
            });
            add(checkBox, BorderLayout.SOUTH);
        };
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (tile) {
                int iw = image.getWidth(this);
                int ih = image.getHeight(this);
                if (iw > 0 && ih > 0) {
                    for (int x = 0; x < getWidth(); x += iw) {
                        for (int y = 0; y < getHeight(); y += ih) {
                            g.drawImage(image, x, y, iw, ih, this);
                        }
                    }
                }
            } else {
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题