私はPong-style
楽しみのためにこの基本的なゲームに取り組んでいますが、そのGUI
側面に問題があります。基本的に、JFrame
すべてを収容する呼び出されたウィンドウと、JPanel
呼び出されたコンテンツがあります。content はContentPanel
I have を参照することになっています。これまでのソースコードは次のとおりです(長くて申し訳ありません。多くはこの質問ではなくゲームにのみ関係しますが、質問を理解するためにソースコードのどの部分が必要かわかりません):
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class Pong extends JApplet {
public static void main(String[] args) {
JFrame window = new JFrame("Pong: The Game of Champions");
JPanel content = new JPanel();
window.setContentPane(content);
window.setSize(1000,600);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
window.setResizable(false);
JMenuBar settings = new JMenuBar();
ButtonHandler listener = new ButtonHandler();
window.setJMenuBar(settings);
JMenu toolsMenu = new JMenu("Settings");
settings.add(toolsMenu);
JMenuItem preferences = new JMenuItem("Preferences");
JMenuItem about = new JMenuItem("About");
toolsMenu.add(preferences);
toolsMenu.add(about);
about.addActionListener(listener);
preferences.addActionListener(listener);
}
public void init() {
setContentPane( new ContentPanel() );
}
public static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
if (command.equals("About")) {
JOptionPane.showMessageDialog(null, "Made by Kyle Evans for Mr. Richardson's AP Computer Science class. Aww yeah.");
}
else if (command.equals("Preferences")) {
}
}
}
public static class ContentPanel extends JPanel implements KeyListener, FocusListener, MouseListener, ActionListener {
private Timer timer = new Timer(10, this);
private static final int PADDLE_WIDTH = 10; // Length of side of square.
private static final int PADDLE_HEIGHT = 100;
private static final int BALL_WIDTH = 10;
private static final int UP = 1, DOWN = 2;
private Color squareColor; // The color of the square.
private int paddleOneDirection = -1;
private int paddleTwoDirection = -1;
private int paddleOneTop, paddleOneLeft, paddleTwoTop, paddleTwoLeft, paddleSpeed,
ballTop, ballLeft, ballSpeedHorizontal, ballSpeedVertical;
public ContentPanel() {
paddleOneTop = 230; // Initial position of top-left corner of square.
paddleOneLeft = 900;
paddleTwoTop = 230;
paddleTwoLeft = 90;
paddleSpeed = 4;
ballTop = 270;
ballLeft = 490;
ballSpeedHorizontal = (int)(Math.random()*2);
if (ballSpeedHorizontal == 0) {
ballSpeedHorizontal = -1;
}
ballSpeedVertical = (int)(Math.random()*6) - 3;
squareColor = Color.WHITE; // Initial color of square.
setBackground(Color.BLACK);
addKeyListener(this); // Set up event listening.
addFocusListener(this);
addMouseListener(this);
} // end init();
public void paintComponent(Graphics g) {
super.paintComponent(g); // Fills the panel with its
// background color, which is white.
g.setColor(squareColor);
g.fillRect(paddleOneLeft, paddleOneTop, PADDLE_WIDTH, PADDLE_HEIGHT);
g.fillRect(paddleTwoLeft, paddleTwoTop, PADDLE_WIDTH, PADDLE_HEIGHT);
g.fillRect(ballLeft, ballTop, BALL_WIDTH, BALL_WIDTH);
String messageH = "false";
String messageV1 = "false";
String messageV2 = "false";
if (isHittingPaddleHorizontal()) {
messageH = "true";
}
if (!isHittingPaddleHorizontal()) {
messageH = "false";
}
if (isHittingPaddleOneVertical()) {
messageV1 = "true";
}
if (!isHittingPaddleOneVertical()) {
messageV1 = "false";
}
if (isHittingPaddleTwoVertical()) {
messageV2 = "true";
}
if (!isHittingPaddleTwoVertical()) {
messageV2 = "false";
}
g.drawString("Is hitting horizontal: " + messageH,7,20);
g.drawString("Is hitting vertical right: " + messageV1,7,40);
g.drawString("Is hitting vertical left: " + messageV2,7,60);
} // end paintComponent()
// ------------------- Game controlling methods -------------------------
public void moveBall() {
ballLeft -= ballSpeedHorizontal;
ballTop -= ballSpeedVertical;
if (isHittingPaddleHorizontal()) {
if (isHittingPaddleOneVertical()) {
ballSpeedHorizontal = -ballSpeedHorizontal;
// ballSpeedHorizontal++;
}
else if (isHittingPaddleTwoVertical()) {
// ballSpeedHorizontal++;
ballSpeedHorizontal = -ballSpeedHorizontal;
}
}
if ((ballTop <= 0) || (ballTop >= 565)) // Sets the vertical boundaries so the ball bounces off the top and bottom.
ballSpeedVertical = -ballSpeedVertical;
if ((ballLeft > 1000) || ballLeft < 0) {
gameOver();
}
}
public boolean isHittingPaddleOneVertical() {
for (int i = 0; i < PADDLE_HEIGHT + (2 * BALL_WIDTH); i++) {
if (ballTop == (paddleOneTop - BALL_WIDTH + i)) {
return true;
}
}
return false;
}
public boolean isHittingPaddleTwoVertical() {
for (int i = 0; i < PADDLE_HEIGHT + (2 * BALL_WIDTH); i++) {
if (ballTop == (paddleTwoTop - BALL_WIDTH + i)) {
return true;
}
}
return false;
}
public boolean isHittingPaddleHorizontal() {
for (int i = 0; i < 50; i++) {
if ((ballLeft == paddleOneLeft + i) || (ballLeft == paddleTwoLeft + PADDLE_WIDTH - i)) {
return true;
}
}
return false;
}
public void robotControl() {
paddleTwoTop = ballTop - 45;
}
public void gameOver() {
paddleOneTop = 230;
paddleTwoTop = 230;
ballTop = 270;
ballLeft = 490;
ballSpeedHorizontal = (int)(Math.random()*2);
if (ballSpeedHorizontal == 0) {
ballSpeedHorizontal = -1;
}
ballSpeedVertical = (int)(Math.random()*6) - 3;
}
// ------------------- Event handling methods ----------------------
public void focusGained(FocusEvent evt) {
repaint(); // redraw with cyan border
}
public void focusLost(FocusEvent evt) {
repaint(); // redraw without cyan border
}
public void keyTyped(KeyEvent evt) {
char ch = evt.getKeyChar(); // The character typed.
} // end keyTyped()
public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode(); // keyboard code for the pressed key
timer.start();
if (key == KeyEvent.VK_UP) { // up arrow key
paddleOneDirection = UP;
}
else if (key == KeyEvent.VK_DOWN) { // down arrow key
paddleOneDirection = DOWN;
}
else {
paddleOneDirection = -1; // This prevents other keys from interfering with the paddle movement.
}
if (key == KeyEvent.VK_W) {
paddleTwoDirection = UP;
}
else if (key == KeyEvent.VK_S) {
paddleTwoDirection = DOWN;
}
else {
paddleTwoDirection = -1;
}
if ((paddleOneTop > 1000 - PADDLE_HEIGHT))
paddleOneTop = 1000 - PADDLE_HEIGHT;
if (paddleTwoTop > 1000 - PADDLE_HEIGHT)
paddleTwoTop = 1000 - PADDLE_HEIGHT;
} // end keyPressed()
public void keyReleased(KeyEvent evt) {
paddleOneDirection = -1;
paddleTwoDirection = -1;
}
public void mousePressed(MouseEvent evt) {
requestFocus();
}
public void mouseEntered(MouseEvent evt) { } // Required by the
public void mouseExited(MouseEvent evt) { } // MouseListener
public void mouseReleased(MouseEvent evt) { } // interface.
public void mouseClicked(MouseEvent evt) { }
public void actionPerformed(ActionEvent evt) {
switch (paddleOneDirection) {
case UP:
paddleOneTop -= paddleSpeed;
break;
case DOWN:
paddleOneTop += paddleSpeed;
break;
}
switch (paddleTwoDirection) {
case UP:
paddleTwoTop -= paddleSpeed;
break;
case DOWN:
paddleTwoTop += paddleSpeed;
break;
}
robotControl();
moveBall();
repaint();
}
} // end nested class ContentPanel
} // end class
(コードは少し乱雑です。申し訳ありませんが、私は Java とプログラミング全般にかなり慣れていません。)
ここに私の質問があります:
デフォルトでコンテンツ
JFrame
を表示するように初期化するにはどうすればよいですか?JPanel
JPanel
切り替え可能なセパレートの作成を開始するにはどうすればよいですか? 私の最終的な目標は、ユーザーが「設定」をクリックしたときに個別に表示され、設定とコンテンツのJPanel
間を行き来できるようにすることです。JPanel
JPanel
助けていただければ幸いです。