最近,我决定开始学习如何使用JAVA(eclipse)制作2D游戏,因此我在网上找到了一个教程,该教程显示了如何使用Java制作superMari游戏,我编写了与他编写的代码相同的代码,并逐步遵循了他的所作所为,没什么大不了的,不幸的是,他的代码显示了执行后的一个窗口,其中包含两个图像,而我的代码仅显示了一个没有图像的窗口,我确保您导入了两个图像并将它们放在一个包中避免各种问题,但仍然不显示任何内容。
我的代码有两个类,“ main”和“ Scene”,就在这里,希望有人能为我找到解决方案,谢谢!
Main.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);
}
}
Scene.java:
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
不是paintCompenent
:
public class Example extends JPanel {
@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句