尝试Java pong

时间:2012-12-07 17:26:22

标签: java swing awt keypress paintcomponent

我正在尝试做一个Java Pong游戏。我对Java的经验很少,需要一些帮助。我在线阅读了很多内容,下面是我提出的代码。 我创建了以下类,但我得到的只是灰色屏幕。我只是想为此实现最简单的解决方案。任何人都可以指出这段代码有什么问题吗?谢谢。

    package pong_test_1;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    /**
     *
     * @author jtrinidad
     */

    public class main_class extends JFrame implements KeyListener{
        static final int FRAME_WIDTH = 800;
        static final int FRAME_HEIGHT = 500;
        JFrame f;

        public main_class()
        {
            super();
            addKeyListener (this);
            setFocusable (true);

            f = new JFrame("Pong");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            f.setResizable(false);

            f.setSize(FRAME_WIDTH,FRAME_HEIGHT);
            f.setVisible(true);

        }

        @Override
         public void keyTyped(KeyEvent e) {
        }
        @Override
        public void keyPressed(KeyEvent e) {
            Pong_Test_1 p = new Pong_Test_1();
            int keyCode = e.getKeyCode();
            switch( keyCode ) { 
                case KeyEvent.VK_UP:
                    Pong_Test_1.p2_pos_y--; 
                    break;
                case KeyEvent.VK_DOWN:
                    Pong_Test_1.p2_pos_y++; 
                    break;
                case KeyEvent.VK_Q:
                    Pong_Test_1.p1_pos_y++; 
                    break;
                case KeyEvent.VK_A :
                    Pong_Test_1.p1_pos_y++; 
                    break;
            }
            add(p);
            repaint();
        }

        /** Handle the key released event from the text field. */
         @Override
        public void keyReleased(KeyEvent e) {

        }

         public static void main(String[] args) {
            // TODO code application logic here
             main_class c = new main_class();
        }


    }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package pong_test_1;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;



    /**
     *
     * @author jtrinidad
     */
    public class Pong_Test_1 extends JPanel implements ActionListener{

        /**
         * @param args the command line arguments
         */
        //Sizes of each object
        static final int WIDTH = 10;
        static final int HEIGHT = 120;
        static final int RADIUS = 10;
        //Position of Player 1's paddle
        static int p1_pos_x = 10;
        static int p1_pos_y = main_class.FRAME_HEIGHT/2;
        //Position of Player 2's paddle
        static int p2_pos_x = main_class.FRAME_WIDTH - 20;
        static int p2_pos_y = main_class.FRAME_HEIGHT/2;;
        //Position of the ball
        static int ball_pos_x;
        static int ball_pos_y;


        Timer animator;

        public Pong_Test_1()
        {
            animator = new Timer (10, this);
            animator.start();
        }


        public void paintComponent(Graphics g){
            super.paintComponent(g);
            this.setBackground(Color.BLACK); //Sets background color

            g.setColor(Color.WHITE);
            g.fillRect(p1_pos_x, p1_pos_y, WIDTH, HEIGHT);
            g.fillRect(p2_pos_x, p2_pos_y, WIDTH, HEIGHT);
            g.fillOval(100, 100, RADIUS, RADIUS);
        }

        public void actionPerformed( ActionEvent e ) {
            repaint();
            revalidate();  // new line
        }

        private void addKeyListener(Pong_Test_1 aThis) {
            throw new UnsupportedOperationException("Not yet implemented");
        }


    }

2 个答案:

答案 0 :(得分:4)

查看此SSCCE是否更符合您的期望,主要问题是扩展并拥有JFrame的实例。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class main_class implements KeyListener {

    static final int FRAME_WIDTH = 800;
    static final int FRAME_HEIGHT = 500;
    JFrame f;

    public main_class() {
        f = new JFrame("Pong");

        JPanel gui = new JPanel();
        gui.setFocusable(true);
        gui.addKeyListener(this);
        f.setContentPane(gui);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setResizable(false);

        f.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        f.setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // this will steal the focus for KeyEvents, or rather..
        // Pong_Test_1 p = new Pong_Test_1();
        int keyCode = e.getKeyCode();
        System.out.println(e);
        switch (keyCode) {
            case KeyEvent.VK_UP:
                Pong_Test_1.p2_pos_y--;
                break;
            case KeyEvent.VK_DOWN:
                Pong_Test_1.p2_pos_y++;
                break;
            case KeyEvent.VK_Q:
                Pong_Test_1.p1_pos_y++;
                break;
            case KeyEvent.VK_A:
                Pong_Test_1.p1_pos_y++;
                break;
        }
        // ..adding it will steal the content area & focus
        //add(p);
        f.repaint();
    }

    /**
     * Handle the key released event from the text field.
     */
    @Override
    public void keyReleased(KeyEvent e) {
    }

    public static void main(String[] args) {
        // TODO code application logic here
        main_class c = new main_class();
    }
}

作为一般建议:

  • 不要延伸框架,只需保留对框架的引用。
  • 对于Swing,请使用键绑定而不是KeyListener

答案 1 :(得分:1)

  1. 请勿为您的班级main_class命名,请改为使用MainClassPongWindow - > Java JLS
  2. 你的main_class extends JFrame,所以这应该是要实例化的JFrame,不要在你的构造函数中创建一个新的。
  3. Pong_Test_1
  4. 中的字段创建getter和setter
  5. 阅读更多关于OOP和Java的教程。

  6. private final Pong_Test_1 myPong;    
    
    public PongWindow() {
      super();
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addKeyListener(this);
      this.setFocusable(true);
      this.setResizable(false);
      this.setSize(FRAME_WIDTH,FRAME_HEIGHT);
    
      myPong = new Pong_Test_1();
      this.getContentPane().add(myPong);
    
      this.setVisible(true);
    }
    

    keyPressed()方法中,使用您的实例进行操作。

    myPong.p2_pos_y--;