简体   繁体   中英

java loop displaying an image in JFrame

Hello i'm trying to make an image fill the screen. The image is 20x20 pixels. i don't really know how to explain it. I think the code might explain it better thank you for your help. and the screen is 800x600 pixels. I have looked for a few days and found nothing.i want it to fill the whole screen with this image i just cant get it and to fill it i need it to repeat and not just use one square.

        private int GLW = 0;
        private int GLH = 0;
        private int add = 20;
        private int redo1 = 0;

        while(redo1 < 24000){
        int redo = 0;
        if(redo < 1){

            //image here
            java.net.URL imageURL = getClass().getResource("grass.png");
            img = new JLabel(new ImageIcon(imageURL));
            JPanel pan1 = new JPanel();
            pan1.add(img);
            add(img, BorderLayout.CENTER);
            img.setVisible(true);
            img.setBounds(GLH,GLW,20 ,20 );

            GLW = GLW + add;
            GLH = GLH + add;

        }
        GLW=GLW+20;
    }

i'm trying to make an image fill the screen. The image is 20x20 pixels

Then you need to tile the image.

Check out Background Panel which allows you to add an image to a panel and support tiling. The basic code would be:

BackgroundPanel panel = new BackgroundPanel(image, BackgroundPanel.TILED);
frame.add( panel );

You need to create a custom class that extends JPanel, then overwrite the paintComponent method. This works for me:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;

public class BackgroundPanel extends JPanel {

    private Image backgroundImage;

    public BackgroundPanel(String fileName) {
        backgroundImage = Toolkit.getDefaultToolkit().createImage(fileName);
    }

    @Override
    public void paintComponent(Graphics g) {
        int width = this.getWidth();
        int height = this.getHeight();
        for(int y = 0; y < height; y += backgroundImage.getHeight(null)) {
            for(int x = 0; x < width; x += backgroundImage.getWidth(null)) {
                g.drawImage(backgroundImage,x,y,null);
            }
        }
    }
}

Then just add this panel to your frame instead of a regular JPanel:

import java.awt.EventQueue;
import java.io.IOException;
import javax.swing.JFrame;

public class Main {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Background Image Example");
                String imageUrl = "path/to/image.extension";

                BackgroundPanel panel = new BackgroundPanel(imageUrl);
                //add any other elements and customize panel
                frame.add(panel);
                //add any other elements and customize jframe

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM