简单的乒乓球游戏;无法让挡板在屏幕上移动

时间:2013-01-15 15:18:59

标签: java swing jbutton actionlistener pong

我正在尝试制作简单的Pong游戏。 我正在做的第一件事就是当用户点击框架中提供的“向左”或“向右”按钮时,让桨向左或向右移动。 当用户单击按钮时,MainPanel类中的动作侦听器会更改名为“paddlePosition”的Paddle类中的常量,然后调用“repaint()”。 但是,它没有工作,并且桨没有移动。我知道按钮正在工作,因为它输出“Hello World”和paddlePosition的值,但显然重绘有问题。 我正在使用Swing,JFrame和Graphics2D来完成所有工作。

enter image description here

MY CLASSES:

Pong Frame(跑步课程)

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;


public class PongFrame extends JFrame{

    private static final long serialVersionUID = 1L;

    public PongFrame(){

    ButtonPanel buttonPanel = new ButtonPanel();
    MainPanel mainPanel = new MainPanel();

    add(buttonPanel, BorderLayout.SOUTH);
    add(mainPanel);

    setSize(400, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
}

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

    }

}

主要小组:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainPanel extends JPanel implements ActionListener{

    public Paddle paddle;

    public MainPanel(){

        setSize(300, 300);
        paddle = new Paddle();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        paddle.draw(g2);
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("Hello World");
        Paddle.movePaddleLeft();
        System.out.println(Paddle.paddlePosition);
        this.repaint();
    }
}

按钮面板:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonPanel extends JPanel {


    private static final long serialVersionUID = 1L;

    public MainPanel mainPanelActionListener = new MainPanel(); 

    JButton left;
    JButton right;

    public ButtonPanel(){
        left = new JButton("Left");
        right = new JButton("Right");

        add(left);
        add(right);

        left.addActionListener(mainPanelActionListener);
        right.addActionListener(mainPanelActionListener);
    }
}

桨:

import java.awt.Graphics2D;

public class Paddle{

    public static int paddlePosition = 200;
    private java.awt.Rectangle paddle;
    private int centerCoordinateX;
    private int centerCoordinateY;

    public Paddle(){
        //paddle = new java.awt.Rectangle(paddlePosition, 285, 100, 10); // x, y, width, height
    }

    public void draw(Graphics2D g2){
        paddle = new java.awt.Rectangle(paddlePosition, 285, 100, 10);
        g2.draw(paddle);
    }

    public static void movePaddleLeft(){
        paddlePosition = (paddlePosition + 3);
    }
}

有谁看到有什么问题?为什么单击按钮时拨片不会移动?

1 个答案:

答案 0 :(得分:2)

ButtonPanel您正在创建新的MainPanel。这会导致您创建一个正在更新的新不可见面板。

您实际需要做的是将MainPanel发送到其ButtonPanel的构造函数中。


编辑: (免责声明:此处所有代码均在没有IDE的情况下生成)

PongFrame构造函数中执行以下操作:

MainPanel mainPanel = new MainPanel();
ButtonPanel buttonPanel = new ButtonPanel(mainPanel);

这会创建一个ButtonPanel,其中包含对MainPanel

的引用

现在为ButtonPanel

public MainPanel mainPanelReference;

JButton left;
JButton right;

public ButtonPanel(MainPanel mainPanelReference) {
    this.left = new JButton("Left");
    this.right = new JButton("Right");

    add(left);
    add(right);

    this.mainPanelReference = mainPanelReference;

    left.addActionListener(mainPanelReference);
    right.addActionListener(mainPanelReference);
}