コード例 #1
0
  // Constructor//
  public Game_Screen() {
    JFrame game_frame = new JFrame("Legend of Pong"); // creates the jframe
    JLabel Background =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\gameScreen.jpg")); // gets the BG image
    game_frame.setContentPane(Background); // places background image

    // setting the header section//

    // Score//
    long score = getScore(); // int to hold score value
    JLabel scoreL = new JLabel(); // label to display score
    scoreL.setText("Score: " + String.valueOf(score)); // connects label and int
    scoreL.setBounds(25, 15, 200, 35); // sets location of score
    scoreL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    scoreL.setForeground(Color.GREEN); // sets color of Score
    game_frame.add(scoreL); // adds score

    // Timer//
    int time = getTime();
    JLabel timeL = new JLabel(); // label to hold time
    timeL.setText("Time(sec): " + String.valueOf(time)); // connects label and int
    timeL.setBounds(350, 15, 200, 35); // set location of time
    timeL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    timeL.setForeground(Color.GREEN); // sets color
    game_frame.add(timeL); // adds time

    // Life//
    int life = getLife(); // int to hold value of lives left
    JLabel lifeL = new JLabel(); // label to hold life
    lifeL.setText("Lives: " + String.valueOf(life)); // connects label and int
    lifeL.setBounds(700, 15, 200, 35); // sets location of life
    lifeL.setFont(new Font("Courier New", Font.BOLD, 20)); // sets font and size of font
    lifeL.setForeground(Color.GREEN); // sets color of life
    game_frame.add(lifeL); // adds life

    // Paddle//
    JPanel paddle = new JPanel(); // new panel for paddle object and image
    Paddle object = new Paddle(); // creates new paddle object
    JLabel link =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\Link.gif")); // new
                                                                                             // paddle image(also link)
    paddle.add(object); // adds the actual object
    paddle.add(link); // adds image to the paddle Panel
    paddle.setBounds(
        object.getX(),
        object.getY(),
        object.getWidth(),
        object.getHeight()); // sets location and size of the paddle
    game_frame.add(paddle); // game_frame gets link image

    // a single ball = test //
    JPanel ball = new JPanel(); // creates the JPanel to hold the ball object and image
    Ball obj = new Ball(); // creates the ball object
    JLabel stone =
        new JLabel(
            new ImageIcon(
                "C:\\Users\\Daniel\\workspace\\Pong_Game\\src\\resources\\Pics\\Ball.png")); // creates the ball image
    ball.add(obj); // adds the object
    ball.add(stone); // adds the image
    ball.setBounds(750, 250, obj.getWidth(), obj.getHeight()); // sets ball location and size
    game_frame.add(ball); // adds it to the screen

    // final frame things//
    game_frame.pack();
    game_frame.setResizable(false);
    game_frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    game_frame.setVisible(true);
  } // end of screen constructor