在我尝试制作的游戏中,直到我移动角色,图像才开始绘制。

这是我的代码:

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。

如果我遗漏了什么,请告诉我。我以为可能是在初始化图像之前尝试绘制,所以我在初始化图像后进行了重新绘制,但没有帮助。

最佳答案

问题在于,由于booleanupdownleftright都不是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); ..(调用super方法也很重要)。
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"));应用程序资源将在部署时成为嵌入式资源,因此明智的做法是立即开始访问它们。 embedded-resource必须通过URL而不是文件进行访问。有关如何形成URL,请参见info. page for embedded resource
if (e.getKeyCode() == 87) {请勿使用“幻数”,而应使用KeyEvent.VK_..。它不仅提供编译时间检查,而且还为其他程序员提供了更好的文档。

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

10-10 21:01