在我想要制作的游戏中,图像不会在我移动角色之前进行绘画。
这是我的代码:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ForgottenMain extends JPanel implements KeyListener,MouseListener{
/**
*
*/
private static final int TIMER_DELAY = 35;
private static final long serialVersionUID = -4926251405849574401L;
public static BufferedImage attic,flashlight,player,killer;
public static boolean up,down,left,right,inAttic;
public static int px,py,kx,ky;
public static int spawnLocation;
public static JFrame frame = new JFrame("Forgotten");
public static void main(String[] args){
inAttic = true;
px = 600;
py = 400;
frame.setSize(1200,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
frame.add(new ForgottenMain());
}
public ForgottenMain(){
init();
}
public void init(){
setSize(1200,800);
setVisible(true);
frame.addKeyListener(this);
frame.addMouseListener(this);
try{
player = ImageIO.read(new File("char.png"));
flashlight = ImageIO.read(new File("flashlightimage.png"));
attic = ImageIO.read(new File("attic.png"));
killer = ImageIO.read(new File("killer.png"));
} catch (Exception e){
e.printStackTrace();
}
// Gameloop
new javax.swing.Timer(TIMER_DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
gameLoop();
}
}).start();
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
int fx = px - 1033;
int fy = py - 635;
kx = 500;
ky = 500;
// Removes the flickering of the images
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Resets the screen to make sure that it only shows the character once
g2.clearRect(0, 0, 1200, 800);
// Draws the background attic
g2.drawImage(attic,0,0,this);
// Draws the player
g2.drawImage(player, px, py, this);
// Draws the Serial Killer
g2.drawImage(killer, kx, ky, this);
// Draws the flashlight
g2.drawImage(flashlight, fx, fy, this);
System.out.println(px + " " + py);
}
public void gameLoop(){
if(up == true && py > 88){
py-=4;
repaint();
}
if(down == true && py < 604){
py+=4;
repaint();
}
if(left == true && px > 80){
px-=4;
repaint();
}
if(right == true && px < 1028){
px+=4;
repaint();
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("MouseLocation: " + arg0.getX() + ", " + arg0.getY());
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == 87){
up = true;
}
if(e.getKeyCode() == 83){
down = true;
}
if(e.getKeyCode() == 65){
left = true;
}
if(e.getKeyCode() == 68){
right = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == 87){
up = false;
}
if(e.getKeyCode() == 83){
down = false;
}
if(e.getKeyCode() == 65){
left = false;
}
if(e.getKeyCode() == 68){
right = false;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
}
例如,在我的游戏中,您使用W,A,S和D键移动角色。当程序运行时,它显示一个空白屏幕,并且不显示应该绘制的所有内容,直到我按W,A,S或D.
如果我遗漏任何有用的信息,请告诉我。我想也许它是在图像初始化之前尝试绘制的,所以我在初始化它之后重新绘制它,它没有帮助。
答案 0 :(得分:3)
问题是因为boolean
值up
,down
,left
或right
都不是true
,所以逻辑在gameLoop()
无法呼叫repaint()
!这可以通过从repaint()
方法明确调用main(String[])
来解决。
public static void main(String[] args) {
inAttic = true;
px = 600;
py = 400;
//frame.setSize(1200, 800);
ForgottenMain fm = new ForgottenMain();
frame.add(fm);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
fm.repaint();
}
public void paint(Graphics g){ ..
应为public void paintComponent(Graphics g){ super.paintComponent(g); ..
(调用超级方法也很重要)。 frame.setSize(1200,800); .. setSize(1200,800);
这是错误的,由于框架装饰,面板将小于1200 x 800,并且布局管理器通常会忽略大小超过首选大小。要解决这个问题:
@Override public Dimension getPreferredSize() { return new Dimension(1200,800); }
(JPanel
)。 frame.pack();
之后,在frame.setResizable(false);
之前致电frame.setVisible(true);
.. player = ImageIO.read(new File("char.png"));
应用程序资源在部署之前将成为嵌入式资源,因此立即开始访问它们是明智之举。必须通过URL而不是文件访问embedded-resource。有关如何形成网址,请参阅info. page for embedded resource。 if (e.getKeyCode() == 87) {
不要使用“幻数”&#39;而是使用KeyEvent.VK_..
。它不仅提供编译时检查,而且也是其他程序员的更好的文档。