我的java程序中没有显示图像
最近我决定开始学习如何用JAVA(eclipse)制作2D游戏,所以我在网上找到了一个教程,展示了如何用JAVA制作superMari游戏,我写了和他写的相同的代码,我一步一步地跟着他做了什么,这并不是什么大问题,不幸的是,在执行之后,他的代码展示,一个有两个图像的窗口,而我的窗口只显示没有图像的窗口,我向您保证,我导入了这两个图像,并将它们放在一个包中,以避免各种问题,但它仍然没有显示任何内容 我的代码有两个类,“main”和“Scene”,在这里,希望有人能为我找到解决方案,谢谢大家 Main.java:我的java程序中没有显示图像,java,eclipse,java-2d,Java,Eclipse,Java 2d,最近我决定开始学习如何用JAVA(eclipse)制作2D游戏,所以我在网上找到了一个教程,展示了如何用JAVA制作superMari游戏,我写了和他写的相同的代码,我一步一步地跟着他做了什么,这并不是什么大问题,不幸的是,在执行之后,他的代码展示,一个有两个图像的窗口,而我的窗口只显示没有图像的窗口,我向您保证,我导入了这两个图像,并将它们放在一个包中,以避免各种问题,但它仍然没有显示任何内容 我的代码有两个类,“main”和“Scene”,在这里,希望有人能为我找到解决方案,谢谢大家 Mai
package AiMEUR.AMiN.jeu;
import javax.swing.JFrame;
public class Main {
public static Scene scene;
public static void main(String[] args) {
JFrame fenetre = new JFrame("Naruto in mario World!!");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(700, 360);
fenetre.setLocationRelativeTo(null);
fenetre.setResizable(false);
fenetre.setAlwaysOnTop(true);
scene = new Scene();
fenetre.setContentPane(scene);
fenetre.setVisible(true);
}
}
package AiMEUR.AMiN.jeu;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Scene extends JPanel{
private ImageIcon icoFond;
private Image imgFond1;
private ImageIcon icoMario;
private Image imgMario;
private int xFond1;
public Scene(){
super();
this.xFond1 = -50;
icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
this.imgFond1 = this.icoFond.getImage();
icoMario = new ImageIcon(getClass().getResource("/Images/1.png"));
this.imgMario = this.icoMario.getImage();
// paintComponent(this.getGraphics());
}
public void paintCompenent(Graphics g){
super.paintComponent(g);
Graphics g2 = (Graphics2D)g;
g2.drawImage(this.imgFond1, this.xFond1, 0, null);
g2.drawImage(imgMario, 300, 245, null);
}
}
Scene.java:
package AiMEUR.AMiN.jeu;
import javax.swing.JFrame;
public class Main {
public static Scene scene;
public static void main(String[] args) {
JFrame fenetre = new JFrame("Naruto in mario World!!");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(700, 360);
fenetre.setLocationRelativeTo(null);
fenetre.setResizable(false);
fenetre.setAlwaysOnTop(true);
scene = new Scene();
fenetre.setContentPane(scene);
fenetre.setVisible(true);
}
}
package AiMEUR.AMiN.jeu;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Scene extends JPanel{
private ImageIcon icoFond;
private Image imgFond1;
private ImageIcon icoMario;
private Image imgMario;
private int xFond1;
public Scene(){
super();
this.xFond1 = -50;
icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
this.imgFond1 = this.icoFond.getImage();
icoMario = new ImageIcon(getClass().getResource("/Images/1.png"));
this.imgMario = this.icoMario.getImage();
// paintComponent(this.getGraphics());
}
public void paintCompenent(Graphics g){
super.paintComponent(g);
Graphics g2 = (Graphics2D)g;
g2.drawImage(this.imgFond1, this.xFond1, 0, null);
g2.drawImage(imgMario, 300, 245, null);
}
}
您没有正确命名
paintComponent
方法,因此它没有被覆盖
正确的名称是paintComponent
而不是paintComponent
:
public class Example extends JPanel {
@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
}
}
您可以通过执行以下操作来确定
图像图标的加载状态:
public Scene(){
super();
this.xFond1 = -50;
icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
int status = icoFond.getImageLoadStatus();
switch (status) {
case (MediaTracker.COMPLETE): {
System.out.println("icoFond image has successfully loaded");
}
case (MediaTracker.ERRORED): {
System.out.println("The icoFond image didn't load successfully");
// probably because the image isn't actually at "/Images/fond.gif"
}
}
this.imgFond1 = this.icoFond.getImage();
icoMario = new ImageIcon(getClass().getResource("/Images/1.png"));
this.imgMario = this.icoMario.getImage();
// paintComponent(this.getGraphics());
}
我会检查icoFond=newimageicon(getClass().getResource(“/Images/fond.gif”)
和其他类似的行实际上正在加载您的图像。对不起!但我不明白你的意思!你认为我应该改变你提到的那一行代码吗?不,那是你应该检查的那一行。请稍后再试:int status=icogond.getImageLoadStatus()
-这应该是MediaTracker.ABORTED
、MediaTracker.ERRORED
或MediaTracker.COMPLETE
中的一个。查看API,这将有助于查看目录结构。例如,您可能有目录src/aimur/AMiN/jeu
。您是否在项目中有src/images
中的图像,或者只是images
中的图像?我有hem just-in-images:/这是一个错误吗?谢谢!这是正确的答案!它正在工作