1

Swingを使ったシンプルなピンポンゲームをデザインしています。ゲームは5つの.javaファイルによって実装されます– PingPongApp.javaはJVMのエントリポイント、MainPanel.javaは2つのサブパネル(ボタンパネルと画面パネル(ScreenPanel.java)、次にBall.javaとRacket)を編成するパネルです。ボールとラケットのロジックを実装する.java。画面パネルにボールとラケットの両方を描画しようとしていますが、問題は、アプリを実行したときに描画されるオブジェクトがボールだけであるということです。これが私のコードです:

Racket.java

import java.awt.*;
public class Racket{
    public Racket(int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    public void draw(Graphics g){
        g.setColor(Color.RED);
        g.fillRect(x, y, width, height);
    }
    private int x, y, width, height;
}

Ball.java

import java.awt.*;
public class Ball{
    public Ball(int x, int y, int velocityX, int velocityY){
        this.x = x;
        this.y = y;
        this.velocityX = velocityX;
        this.velocityY = velocityY;
    }
    public void setBounds(int width, int height){
        rightBoundary = width - DIAMETER;
        bottomBoundary = height - DIAMETER;
    }
    public void move(){
        // Move the ball
    }
    public void draw(Graphics g){
        g.fillOval(x, y, DIAMETER, DIAMETER)
    }
    private final static int DIAMETER = 21;
    private int x, y, velocityX, velocityY, rightBoundary, bottomBoundary;
}

ScreenPanel.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ScreenPanel extends JPanel{
    public ScreenPanel(){
        initComponents();
    }
    private void initComponents(){
        interval = 35;
        ball     = new Ball(130, 0, 2, 3);
        racket   = new Racket(120, 250, 70, 10);            
        setPreferredSize(new Dimension(200, 200));
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        timer = new Timer(interval, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt){
                ball.setBounds(getWidth(), getHeight());
                ball.move();
                repaint();  
            }
        });
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        ball.draw(g);
        racket.draw(g);  
    }
    public void startOrPauseGame(boolean turnOnOff){
        if(turnOnOff){
            timer.start();
        } else {
            timer.stop();
        }
    }
    private Racket racket;
    private Timer timer;
    private Ball ball;
    private int interval;
}

MainPanel.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainPanel extends JPanel{
    public MainPanel(){
        initComponents();
    }
    private void initComponents(){
        buttonPanel = new JPanel(new FlowLayout());
        screenPanel = new ScreenPanel();
        cmdStart    = new JButton("Start/Resume");
        cmdPause    = new JButton("Pause");
        addComponentsToPane();
    }
    public void addComponentsToPane(){
        buttonPanel.add(cmdStart);
        buttonPanel.add(cmdPause);
        setLayout(new BorderLayout());
        add(buttonPanel , BorderLayout.NORTH);
        add(screenPanel , BorderLayout.SOUTH);
        cmdStart.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt){
                // Start the game  
            }                   
        });
        cmdPause.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt){
                // Pause the game 
            }    
        });   
    }
    private ScreenPanel screenPanel;
    private JPanel buttonPanel;
    private JButton cmdStart, cmdPause;
}
4

2 に答える 2

3

問題は、ラケットのyScreenPanel座標が、パネルの推奨サイズの高さよりも大きいことです:-250> 200:

    racket = new Racket(120, 250, 70, 10);
                              ^
    setPreferredSize(new Dimension(200, 200));

ラケットを画面外に描画します。

于 2012-08-11T11:12:33.520 に答える
3

ScreenPanel クラスのプログラムで見つけたコードは次のとおりです

racket   = new Racket(120, 250, 70, 10);            
setPreferredSize(new Dimension(200, 200));

取得する正しい値に変更します。Y は 250 で、サイズは 200 です。

于 2012-08-11T11:13:31.340 に答える