0

所以我真的不明白为什么我不能将图像加载到这个 JPanel 中......代码有点长,但我已经评论了它,所以推断发生了什么应该相当简单。有人可以帮忙吗?我已经尝试删除背景颜色,但这似乎并没有做太多(除了删除背景颜色)

编辑

我设法修复了在未添加到的类中调用 drawScene 方法的错误,现在它会显示加载图像时出错......我不完全确定为什么。如果有人想尝试自己运行程序,这就是我正在使用的图像。我将它添加到我的 Window.java 文件下方的包中。


在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
///

///WINDOW CLASS BEGINS

///

public class Window extends JPanel{

    //Instance Variables
    Tools tool = new Tools();
    GameScreen game = new GameScreen();
    Scene scene = new Scene();

    //All other variable initialization things
    private static final long serialVersionUID = 1L;


    public Window(){
        //Sets the layout to the class
        this.setLayout(new BorderLayout());

        //adding things to the class
        this.add(game,BorderLayout.CENTER);


        //Sets background Colors to the panels
        this.game.setBackground(Color.black);

    }

    public static void main(String args[]){
        JFrame frame = new JFrame();
        Window window = new Window();
        frame.setSize(new Dimension(800,600));
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setJMenuBar(Tools.bar);
        frame.add(window);
        frame.pack();
        frame.setLocationRelativeTo(null);
    }

    public void paintComponent(Graphics g){



    }

    ///

// TOOLBAR CLASS BEGINS

    ////

    public static class Tools extends JPanel implements ActionListener{

        private static final long serialVersionUID = 1L;

        JPanel panel = new JPanel();
        public static JMenuBar bar = new JMenuBar();
        JMenu file = new JMenu("file");
        JMenuItem exit = new JMenuItem("Exit");

        @SuppressWarnings("static-access")
        public Tools(){

            //Set layouts
            this.panel.setLayout(new BorderLayout());

            //Add to the bar 
            this.bar.add(file,BorderLayout.WEST);


            //add things to the inside of the bar
            this.file.add(this.exit);
            this.panel.add(this.bar);

            //add panels
            this.add(panel);

            //action Commands

            this.exit.setActionCommand("exit");

            //add action Listeners
            this.exit.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            if (e.getActionCommand() == "exit"){
                System.out.println("Closed");
                System.exit(0);
            }

        }

    }

    /////

    //GAMESCREEN CLASS BEGINS

    ////
    public class GameScreen extends JPanel implements ActionListener{

        private static final long serialVersionUID = 1L;

        //Instance Variables

        Scene scene = new Scene();


        //JPanel variables

        JPanel screen =  new JPanel(new BorderLayout());

        public GameScreen(){

            //Sets the class layout


            //Adding panels
            this.add(screen);

            //Setting the color
            this.screen.setBackground(new Color(0,0,0));

            //Adding things to the panels
            this.screen.add(scene);


        }


        @Override
        public void actionPerformed(ActionEvent e) {


        }

        public void paintComponent(Graphics g){
            scene.drawScence(g, "Backgound.jepg", 0, 0, 600, 600);
        }

    }


    public class Scene extends JPanel{


        public Scene(){

        }

        public void drawScence(Graphics g,String location,int x,int y,int width,int height){
            try{
                 URL url = this.getClass().getResource(location);
                 Image image = ImageIO.read(url);
                 g.drawImage(image, x, y, width, height, null);
            }catch (Exception e){
                System.out.println("Unable to load image.");
            }

        }
    }
}
4

0 回答 0