简体   繁体   中英

Showing Image with extendend JFrame

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

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.

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