Java游戏 - 直到Thread.sleep之后才绘画

时间:2016-08-01 16:43:48

标签: java swing paint

我目前正在编写一个游戏,如果你点击首页上的第一个按钮,我会尝试将游戏编入其中,它会显示5秒的过渡画面,然后显示游戏。这是我的代码:

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,frontpage,transition;
    public static boolean onFrontPage,up,down,left,right,inAttic,onTransition;
    public static int px,py,kx,ky;
    public static Thread th1,th2;
    public static JFrame frame = new JFrame("Forgotten");
    public static void main(String[] args){
        onFrontPage = true;
        px = 600;
        py = 400;
        ForgottenMain fm = new ForgottenMain();
        frame.add(fm);
        frame.setSize(1200,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.add(new ForgottenMain());
        fm.repaint();
    }
    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"));
            frontpage = ImageIO.read(new File("frontpageoutline.png"));
            transition = ImageIO.read(new File("transitionoutline.png"));
        } catch (Exception e){
            e.printStackTrace();
        }


        // Gameloop
          new javax.swing.Timer(TIMER_DELAY, new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                    gameLoop();
                 }
              }).start();   
    }
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(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
        if(inAttic == true){
            System.out.println("Drawing attic");
            g2.drawImage(attic,0,0,this);
        }
        if(onFrontPage == true){
            g2.drawImage(frontpage, 0, 0, this);
        } 
        // Draws the player
        if(onFrontPage == false && onTransition == false){
            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);
        }
        if(onTransition == true){
            g2.drawImage(transition, 0, 0, this);
            System.out.println("Drawing Transition");
            try {
                System.out.println("Sleeping for 5 Seconds");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Done sleeping.");
            onTransition = false;
            inAttic = true;
        }
        System.out.println(px + " " + py);
    }
    public void gameLoop(){

    }
    @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 e) {
        if(e.getX() > 499 && e.getY() > 343 && e.getX() < 748 && e.getY() < 391){
            onFrontPage = false;
            onTransition = true;
            repaint();
        }
    }
    @Override
    public void mouseReleased(MouseEvent arg0) {

    }

我的问题是,在paint方法中,在if语句测试中,如果onTransition等于true,它应该绘制图像转换,等待5秒,然后绘制游戏。

然而,现在它等了5秒,然后快速绘制过渡屏幕,然后是游戏。出于某种原因,他们出了故障。

我该如何解决这个问题?我已经尝试了等待5秒的替代方法,例如使用currentTimeMillis();,但结果相同。

1 个答案:

答案 0 :(得分:2)

你有一些严重的问题:

  • 您添加了两次面板
  • 你有一些迂回使用框架等

除此之外,以下内容将起作用:您必须意识到您无法控制绘画过程。因此,您应该启动一个新的线程来计算睡眠,让油漆不受限制地进行工作。

(我也删除了静电 - 如果你真的想要它们,你可以尝试将它们放回去 - 如果它不起作用你就把它们扔出去)

class F extends JPanel implements MouseListener{
    /**
     * 
     */
    private static final int TIMER_DELAY = 35;
    private static final long serialVersionUID = -4926251405849574401L;
    public BufferedImage attic,flashlight,player,killer,frontpage,transition;
    public boolean onFrontPage,up,down,left,right,inAttic,onTransition;
    public int px,py,kx,ky;
    public static Thread th1,th2;
    public JFrame frame = new JFrame("Forgotten");
    public F(){
        init();
    }
    public void init(){
        setSize(1200,800);
        setVisible(true);
        onFrontPage = true;
        px = 600;
        py = 400;
        frame.add(this);
        frame.setSize(1200,800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

        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"));
            attic = ImageIO.read(new File(...));
            frontpage = ImageIO.read(new File(...));
            transition = ImageIO.read(new File(...));
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(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
        if(inAttic == true){
            System.out.println("Drawing attic"+System.currentTimeMillis());
            g2.drawImage(attic,0,0,this);
        }
        if(onFrontPage == true){
            g2.drawImage(frontpage, 0, 0, this);
        } 
        // Draws the player
        if(onFrontPage == false && onTransition == false){
            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);
        }
        if(onTransition == true){
            Graphics gt=transition.getGraphics();
            gt.setColor(Color.red);
            gt.drawString("xxx"+System.currentTimeMillis(), 10, 100);
            g2.drawImage(transition, 0, 0, this);
            onTransition = false;
            inAttic = true;
        }
        System.out.println(px + " " + py);
    }
    public void gameLoop(){

    }
    @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 e) {
            onFrontPage = false;
            onTransition = true;

    Thread th=new Thread() {
      public void run() {
            repaint();
             System.out.println("Drawing Transition"+System.currentTimeMillis());
            try {
                System.out.println("Sleeping for 5 Seconds"+System.currentTimeMillis());
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Done sleeping."+System.currentTimeMillis());
            repaint();
      }
    };
    th.start();


    }
    @Override
    public void mouseReleased(MouseEvent arg0) {

    }

}