-1

I've made some code for my tile, where I draw 2 objects, but I can't load a background in the tile as it extends JFrame. What am I doing wrong? I used buffered image to read the picture, made it suitable for any screen size, set it as a JLabel, but still isn't working. Square1 and obstacle are graphics of rectangles imported, one acting as the player moving and the other as an obstacle respectively.

public class BasicTwoPlayer extends JFrame implements Runnable {   

    static Square1 p1 = new Square1();
    static Square2 p2 = new Square2();
    static obstacle o1 = new obstacle();
    static Thread p1t;
    static Thread p2t;
    static Thread o1t;
    KeyADAPT a = new KeyADAPT();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final int widthScreen = screenSize.width;
        final int heightScreen = screenSize.height;

    public BasicTwoPlayer() {
        try {
            BufferedImage backgroundImage = ImageIO.read(new File("P://My Pictures//background1.jpg"));
            JLabel background = new JLabel(new ImageIcon(backgroundImage));
            Image scaleBackground = backgroundImage.getScaledInstance(widthScreen, heightScreen, Image.SCALE_SMOOTH);
            ImageIcon imageIcon = new ImageIcon(scaleBackground);
            setContentPane(new JLabel(imageIcon));

            addKeyListener(a);
            setSize(new Dimension(widthScreen, heightScreen));
            setUndecorated(true);
            setBackground(Color.WHITE);
            setLayout(null);
            setLocationRelativeTo(null);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);


            p1t = new Thread(p1);
            p2t = new Thread(p2);
            o1t = new Thread(o1);
            p1t.start();
            p2t.start();
        } catch (IOException ex) {
            Logger.getLogger(BasicTwoPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void paint(Graphics g) {
        background.paint(g);
    }

    public void draw(Graphics g) {

        p1.draw(g);
        o1.draw(g);
        repaint();
    }

    public static void main(String[] args) {
        BasicTwoPlayer sf = new BasicTwoPlayer();
        Thread mt = new Thread(sf);
        mt.start();
        t1.start();
    }

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DennyS
  • 1
  • 1
  • You will want to take a look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [Painting in AWT and Swing](https://www.oracle.com/technetwork/java/painting-140037.html) to get a better understanding of how painting works in Swing. You will also want to take a look at [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) as Swing is NOT thread safe and is single threaded. This is going to cause you issue if you continue with current thought process – MadProgrammer Jan 23 '20 at 10:01
  • I just want an answer like everyone else :/ – DennyS Jan 23 '20 at 10:02
  • I would highly recommend that you stop extending directly from `JFrame`, this is going to make your life harder. Start with a `JPanel` and then add it to what ever container you want. For background images, I'd recommend against using `JLabel`, it's not well suited to the job and lose the control over how to manage the resizing of the component. Consider using a `JPanel` and paint the image yourself - [for example](https://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430) – MadProgrammer Jan 23 '20 at 10:05

1 Answers1

0

I just want an answer like everyone else :/

You'd be surprised how much I'd prefer to learn how to answer my own questions ;)

You will need to understand:

It wouldn't hurt to understand:

"How to add a background image" ...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new BackgroundPane());

                frame.add(new OverlayPane());

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class OverlayPane extends JPanel {

        public OverlayPane() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            JLabel label = new JLabel("You'd be suprised who far a little knowledge will take you");
            label.setForeground(Color.WHITE);
            add(label);
        }

    }

    public class BackgroundPane extends JPanel {

        private BufferedImage backgroundImage;

        public BackgroundPane() {
            setLayout(new BorderLayout());
            try {
                backgroundImage = ImageIO.read(BackgroundPane.class.getResource("background.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return backgroundImage == null ? new Dimension(200, 200) : new Dimension(backgroundImage.getWidth(), backgroundImage.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (backgroundImage == null) { return; }

            int x = (getWidth() - backgroundImage.getWidth()) / 2;
            int y = (getHeight() - backgroundImage.getHeight()) / 2;

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(backgroundImage, x, y, this);
            g2d.dispose();
        }

    }

}

You are going to want to learn about:

based on you the direction of this and all your other questions to date. They will help you avoid common pitfalls.

I don't doubt you'll have issues coming to grips with these concepts and I encourage you to have a play and if and when, post a question out them, specifically.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366