在java中暂停游戏时显示消息

时间:2014-07-27 19:39:18

标签: java swing paintcomponent

我用Java开发了this game

我拥有暂停,恢复等所有功能。但我现在的问题是在暂停游戏时在JFrame的中间显示一条“GAME PAUSED”的消息。我试图实现JLabel ImageIcon,但没有成功。请有人提供某种帮助。提前致谢。

public class NewClass extends JFrame implements KeyListener{

boolean for_pause;


 private DrawCanvas canvas;
 //variable declaration-end 
  public NewClass()  {
    //initializing the frame section
   canvas = new DrawCanvas();
  canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
  this.setContentPane(canvas);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.pack();
  setLocationRelativeTo(null);
  this.setTitle("Bouncing Ball");
  this.setVisible(true);
    //this.setResizable(false);

 addKeyListener(this);
 draw_rect();

}

 ActionListener action = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent ae) {
       update();
       repaint();
    }
};
Timer t = new Timer(8,action);

public void update()
{
     x += xSpeed;
  y += ySpeed;
  if (x > CANVAS_WIDTH - size || x < 0)
  {
     xSpeed = -xSpeed;
  }
  if (y > CANVAS_HEIGHT - 80 || y < 0)
  {
     ySpeed = -ySpeed;
 }
}

@Override
public void keyTyped(KeyEvent ke) {
    //throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void keyPressed(KeyEvent ke) {
    int keycode = ke.getKeyCode();
       if(keycode == KeyEvent.VK_LEFT)
       {
            if(xx == 5)
            {
                xx = 5;
            }
            else{ 
            xx = xx-15;
            }

       }
       if(keycode == KeyEvent.VK_RIGHT)
       {

      if(xx == 515)
            {
                xx = 515;
            }
            else{ 
            xx = xx+15;
            }

       }

       if(keycode == KeyEvent.VK_SPACE)
       {
             if(for_pause == false)
             {
                 t.start();
                 for_pause = true;
             }


         }
       if(keycode == KeyEvent.VK_ESCAPE)
       {
             if(for_pause == false)
             {
                t.stop();
                for_pause = true;
             }  
             else
             {

                 t.start();
                 for_pause = false;
             }
         }
}

@Override
public void keyReleased(KeyEvent ke) {
    //throw new UnsupportedOperationException("Not supported yet.");
}


 class DrawCanvas extends JPanel {
    private static final long serialVersionUID = 1L;
     public DrawCanvas()
  {
      setBackground(Color.white);

     setLayout(null);
  }
    @Override
    public void paintComponent(Graphics g) {
     super.paintComponent(g);  // paint parent's background
     Graphics2D d = (Graphics2D)g;

     for(RoundRectangle2D r : s1){
        d.setPaint(gp1);
        d.setStroke(color);
        d.fill(r);
      }
     for(RoundRectangle2D r : s2){
        d.setPaint(gp2);
        d.setStroke(color);
        d.fill(r);
      }
     for(RoundRectangle2D r : s3){
        d.setPaint(gp3);
        d.setStroke(color);
        d.fill(r);
      }
     Board = new RoundRectangle2D.Double(xx,yy,120,10,20,20);
    //getting the center x and center y of the board--start 
    int Board_xm = (int)  Board.getCenterX();
    int Board_ym = (int) Board.getCenterY();
    Board_xm =Board_xm-15;
    Board_ym =Board_ym-25;

    Ball = new Ellipse2D.Double(x,y,size,size);

    d.setColor(Color.yellow);
    d.fill(Ball);
    d.draw(Board);


    collide_Bricks(); //function not included here
    collide_Board();//function not included here

 }

     }

1 个答案:

答案 0 :(得分:1)

您可以使用Glass Pane显示组件的消息。