0

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;
    }
1
  • No, actually, the code doesn't explain much. What do you want it to do, and what does it do that you don't like? Edit your question, and I suggest adding the java tag while you're at it. Commented Dec 13, 2014 at 20:05

2 Answers 2

2

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 );
Sign up to request clarification or add additional context in comments.

Comments

0

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);
            }
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.