首页 > 解决方案 > It's lagging when I press the button to go to the game screen

问题描述

I made a game that moves players on the keyboard and increases scores by eating random coins. When you touch TNT, HP is lowered, and when you touch coffee, HP is raised. Prints on screen using Graphics g. Now I'm going to make the game start screen. I have already created a start button for another class, and when you press the button, a new game window appears. But I can't see the game screen and it's lagging. Even the close button doesn't press well, so we're forcing it through the To Do Manager. Can I know what the problem is? It's okay if the game doesn't run as a new window and comes out of the same window. Please help me. I'll show you the Start button class and the Game class in turn. My English is not good, so thank you for reading the long question.

★★★ StartButtonClass ★★★</p>

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class gamestart {

public static void main(String[] args) {
JFrame frame = new JFrame("Start up frame");
JButton newGameButton = new JButton("New Game");
frame.setLayout(new FlowLayout());
frame.add(newGameButton);
frame.setVisible(true);

newGameButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JFrame newGameWindow = new JFrame("A new game!");
        newGameWindow.setVisible(true);
        newGameWindow.add(new CoinEat1());
        newGameWindow.pack();
    }
});
frame.pack();
}
}

★★★ GameClass ★★★</p>

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class CoinEat1 extends JFrame {
   private Image bufferImage;
   private Graphics screenGraphic; 

   private Clip clip;

   private Image backgroundImage = new ImageIcon("src/images/배경.jpg").getImage();
   private Image player = new ImageIcon("src/images/1P.png").getImage();
   private Image coin = new ImageIcon("src/images/코인.png").getImage();
   private Image TNT = new ImageIcon("src/images/폭탄.png").getImage();
   private Image Coffee = new ImageIcon("src/images/커피.png").getImage();

   private int playerX,playerY;
   private int playerWidth = player.getWidth(null); 
   private int playerHeight = player.getHeight(null); 

   private int coinX,coinY;
   private int coinWidth = coin.getWidth(null);
   private int coinHeight = coin.getHeight(null);

   private int TNTX,TNTY;
   private int TNTWidth = TNT.getWidth(null);
   private int TNTHeight = TNT.getHeight(null);

   private int CoffeeX,CoffeeY;
   private int CoffeeWidth = Coffee.getWidth(null);
   private int CoffeeHeight = Coffee.getHeight(null);

   private int score;

   private int HP;

   private boolean playerup, down, left, right; 

   public CoinEat1() {
      setTitle("동전JAVA");
      setVisible(true);
  setSize(Main.SCREEN_WIDTH,Main.SCREEN_HEIGHT);
  setLocationRelativeTo(null);
  setResizable(false);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  addKeyListener(new KeyAdapter(){
     public void keyPressed(KeyEvent e) { 
        switch(e.getKeyCode()) {
        case KeyEvent.VK_UP:
           playerup=true;
           break;
        case KeyEvent.VK_DOWN:
           down=true;
           break;
        case KeyEvent.VK_LEFT:
           left=true;
           break;
        case KeyEvent.VK_RIGHT:
           right=true;
           break;   
        }
     }
     
     public void keyReleased(KeyEvent e) { 
        switch(e.getKeyCode()) {
        case KeyEvent.VK_UP:
           playerup=false;
           break;
        case KeyEvent.VK_DOWN:
           down=false;
           break;
        case KeyEvent.VK_LEFT:
           left=false;
           break;
        case KeyEvent.VK_RIGHT:
           right=false;
           break;   
        }
     }      
  });

  
  Init();
  
  while(true){
     try {
        Thread.sleep(20);
     } catch(InterruptedException e) {
        e.printStackTrace();
     }
     keyProcess();
     CoinCrashCheck();
     TNTCrashCheck();
     CoffeeCrashCheck();
  }
   }


   public void Init() {
      score = 0;
  
  HP = 100;
  
  playerX = (Main.SCREEN_WIDTH-playerWidth)/2; 
  playerY = (Main.SCREEN_HEIGHT-playerHeight)/2;
  
  coinX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
  coinY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
  
  TNTX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth)); 
  TNTY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
    
  CoffeeX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth)); 
  CoffeeY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerWidth))+30;
  
      playsound("src/music/BGM.wav",true);
   }

   public void keyProcess() {
      if (playerup && playerY - 3 > 30) playerY-=3;
      if (down && playerY + playerHeight + 3 < Main.SCREEN_HEIGHT) playerY+=3;
      if (left && playerX -3 > 0) playerX-=3;
      if (right && playerX + playerWidth + 3 < Main.SCREEN_WIDTH) playerX+=3;
   }

   public void CoinCrashCheck(){
      if (playerX+playerWidth > coinX && coinX+coinWidth > playerX && playerY+playerHeight > coinY && coinY+coinHeight > playerY){ 
         score += 100;
         playsound("src/music/coin.wav",false);
         coinX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
         coinY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30; 
      }
   }

   public void TNTCrashCheck() { 
        if (playerX+playerWidth > TNTX && TNTX+TNTWidth > playerX && playerY+playerHeight > TNTY && TNTY+TNTHeight > playerY){ 
        HP -= 25;
        TNTX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
        TNTY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30; 
        }
    }

   public void CoffeeCrashCheck() { 
        if (playerX+playerWidth > CoffeeX && CoffeeX+CoffeeWidth > playerX && playerY+playerHeight > CoffeeY && CoffeeY+CoffeeHeight > playerY){ 
            HP += 25; 
            CoffeeX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
            CoffeeY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30; 
            }
    }

   public void playsound(String pathName, boolean isLoop) {
        try {
            clip = AudioSystem.getClip();
            File audioFile = new File(pathName);
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
            clip.open(audioStream);
            clip.start();
            if(isLoop) 
                clip.loop(Clip.LOOP_CONTINUOUSLY);
        }catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

   public void paint(Graphics g) {
      bufferImage = createImage(Main.SCREEN_WIDTH,Main.SCREEN_HEIGHT); 
      screenGraphic = bufferImage.getGraphics();  
      screenDraw(screenGraphic);
      g.drawImage(bufferImage,0,0,null); 
   }

   public void screenDraw(Graphics g){//이미지를 출력해줄 paint 메소드
      g.drawImage(backgroundImage,0,0,null);
      g.drawImage(player,playerX,playerY,null);
      g.drawImage(coin,coinX,coinY,null);
      g.drawImage(TNT,TNTX,TNTY,null);
      g.drawImage(Coffee,CoffeeX,CoffeeY,null);
  
      g.setColor(Color.white);
      g.setFont(new Font("Arial",Font.BOLD,35));
      g.drawString("SCORE : "+score, 30, 80);
      g.drawString("HP : " + HP, 30, 120);
  
      this.repaint(); 
   }

   public static void main(String[] args) {
      new CoinEat1();
  
   }

}

标签: javaswingevent-dispatch-thread

解决方案


推荐阅读