gpt4 book ai didi

java - 在我移动东西之前不画画

转载 作者:行者123 更新时间:2023-11-29 04:42:41 24 4
gpt4 key购买 nike

在我尝试制作的游戏中,在我移动角色之前图像不会绘制。

这是我的代码:

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。

如果我遗漏了任何有用的信息,请告诉我。我想也许它是在图像初始化之前尝试绘制,所以我在初始化它们之后重新绘制,但没有帮助。

最佳答案

问题是因为 boolean 值都不是 updownleft righttruegameLoop() 中的逻辑不会调用 repaint()!这可以通过从 main(String[]) 方法显式调用 repaint() 来解决。

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); ..(调用 super 方法也很重要)。
  • frame.setSize(1200,800); .. setSize(1200,800); 这是错误的,由于框架装饰,面板将小于 1200 x 800,布局管理器通常会忽略超过首选尺寸的尺寸。要解决这个问题:
    1. @Override public Dimension getPreferredSize() { return new Dimension(1200,800); } 在绘画表面(JPanel)。
    2. 将面板添加到框架中。
    3. 在添加组件并调用 frame.setResizable(false); 之后、frame.setVisible(true) 之前调用 frame.pack(); ..
  • player = ImageIO.read(new File("char.png")); 应用程序资源在部署时将成为嵌入式资源,因此明智的做法是像访问它们一样开始访问它们是,现在。一个embedded-resource必须通过 URL 而不是文件访问。查看info. page for embedded resource了解如何形成 URL。
  • if (e.getKeyCode() == 87) { 不要使用“魔数(Magic Number)”,而是使用 KeyEvent.VK_..。它不仅提供编译时检查,而且还为其他程序员提供了更好的文档。

关于java - 在我移动东西之前不画画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38538383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com