且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的图片没有加载到JFrame上?

更新时间:2023-02-22 09:26:51

你正在运行你的您的Mac上的应用程序,但您的图像路径是一个Windows路径。
 ImageIcon i = new ImageIcon(C:/character.png); 

。 Mac上的路径应为/User/xxx/your_projects/background.jpg。这是一条绝对的道路。您可以通过键盘快捷键command + i检查图像路径。如果项目中有图像文件,也可以使用项目的相对路径。

 ImageIcon i = new ImageIcon(background.jpg); 


Guys why is my image not loading on my JFrame??
BTW I am using eclipse on my mac.

Code for Board :

package ourgame;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Board extends JPanel implements ActionListener {
        Dude p;
        public Image img;
        Timer time;
 
        public Board() {
                p = new Dude();
                addKeyListener(new AL());
                setFocusable(true);
                ImageIcon i = new ImageIcon("C:/character.png");
                img = i.getImage();
                time = new Timer(5, this);
                time.start();
        }
 
        public void actionPerformed(ActionEvent e) {
                p.move();
                repaint();
        }
 
        public void paint(Graphics g) {
                super.paint(g);
                Graphics2D g2d = (Graphics2D) g;
 
                g2d.drawImage(img, 0, 0, null);
                g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
        }
 
        private class AL extends KeyAdapter {
                public void keyReleased(KeyEvent e) {
                        p.keyReleased(e);
                }
 
                public void keyPressed(KeyEvent e) {
                        p.keyPressed(e);
                }
        }
}



Code for Dude :

package ourgame;
 
import java.awt.*;
import java.awt.event.KeyEvent;
 
import javax.swing.ImageIcon;
 
public class Dude {
        int x, dx, y;
        Image still;
 
        public Dude() {
                ImageIcon i = new ImageIcon("C:/background.jpg");
                still = i.getImage();
                x = 10;
                y = 172;
        }
 
        public void move() {
                x = x + dx;
        }
 
        public int getX() {
                return x;
        }
 
        public int getY() {
                return y;
        }
 
        public Image getImage() {
                return still;
        }
 
        public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_LEFT)
                        dx = -1;
 
                if (key == KeyEvent.VK_RIGHT)
                        dx = 1;
        }
 
        public void keyReleased(KeyEvent e) {
                int key = e.getKeyCode();
 
                if (key == KeyEvent.VK_LEFT)
                        dx = 0;
 
                if (key == KeyEvent.VK_RIGHT)
                        dx = 0;
        }
 
}



Code for Frame:

package ourgame;
 
import javax.swing.*;
 
public class Frame {
 
        public Frame(){
                JFrame frame = new JFrame();
                frame.add(new Board());
                frame.setTitle("2-D Test Game");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(700,365);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
        }
        public static void main(String[] args){
                new Frame();
        }
}


Thanks in advance!

you are running your app on your mac, but your image path is a windows path.
ImageIcon i = new ImageIcon("C:/character.png");

. The path on Mac should be "/User/xxx/your_projects/background.jpg". This is an absolute path. You can check the image path by keyboard shortcut "command + i". And you can also use relative path to your project if you have the image file in your project.

ImageIcon i = new ImageIcon("background.jpg");