これはゲーム用のゲームパネルの一部です。スタートボタンを手に入れてリセットボタンを作成しましたが、押すたびにゲームをリセットする方法はわかりません。 「リセット」を押した後にゲームを元の位置にリセットする方法を誰かが知っていますか?今すぐ "あなたは死んでしまった"という言葉が表示されますが、リセットを押すと何もしません。リセットボタンの使い方Javaアルカノイドゲーム
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class GamePanel extends JPanel
{
private Brick[][] bary;
private static final Color BACKGROUND = Color.black;
private BufferedImage myImage;
private Graphics myBuffer;
private Ball ball = new Ball();
public Bumper bumper;
private Timer t;
private int hits = 0;
private boolean isPlayingGame = false;
public GamePanel()
{
myImage = new BufferedImage(600, 800, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, 600,800);
JButton sbutton = new JButton("Start");
sbutton.setFocusable(false);
sbutton.addActionListener(new Listener());
add(sbutton);
JButton rbutton = new JButton("Reset");
rbutton.setFocusable(false);
rbutton.addActionListener(new Listener());
add(rbutton);
bary = new Brick[5][14];
bumper = new Bumper(270, 775, 60, 10, Color.BLUE);
ball = new Ball(300, 400, 10, Color.RED);
t = new Timer(5, new Listener());
setFocusable(true);
//addKeyListener(new Key());
}
// tick method is called every 10ms by Arkanoid.java
// only does stuff if game is actually being played
public void tick()
{
if(isPlayingGame)
{
ball.move(600,800, 600, 800);
death(ball);
}
}
// these get called by the BumperLitsener, which is added to the whole frame
public void moveBumperLeft()
{
bumper.setX(bumper.getX()-20);
//System.out.println("moving bumper left");
}
public void moveBumperRight()
{
bumper.setX(bumper.getX()+20);
//System.out.println("moving bumper right");
}
/*
public class Key extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_A)
bumper.setX(bumper.getX()+10);
if(e.getKeyCode() == KeyEvent.VK_S)
bumper.setX(bumper.getX()-10);
}
}
*/
public void startGame()
{
isPlayingGame = true;
}
public void paintComponent(Graphics g)
{
// draw myBuffer, then draw that onto g
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0,0,600,800);
setLayout(new GridLayout(5, 14));
myBuffer.setColor(Color.WHITE);
int b=1;
int d=40;
for(int r = 0; r < bary.length; r++)
{
for(int c = 0; c < bary[0].length; c++)
{
bary[r][c] = new Brick(b, d,40,20, Color.BLUE);
b=b+43;
bary[r][c].draw(myBuffer);
}
d=d+23;
b=1;
}
ball.draw(myBuffer);
bumper.draw(myBuffer);
repaint();
if(ball.getColor()==Color.BLACK)
{
myBuffer.setFont(new Font("MS Comic Sans", Font.ITALIC, 45));
myBuffer.drawString("GAME OVER!", 168, 300);
ball.move(0, 0, 0, 0);
}
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
GamePanel.this.getTopLevelAncestor().requestFocus();
startGame(); // starts game when button is pressed
System.out.println("Game is starting!");
}
}
private void death(Ball ball)
{
double d = ball.getY() + ball.getRadius();
if(d>800.0)
{
ball.setX(300);
ball.setY(400);
ball.setdx(ball.getdx()*-1);
ball.setdy(ball.getdy()*-1);
ball.setColor(Color.BLACK);
}
}
/* private void collide(Ball ball, Bumper b)
{
double d = distance(ball.getX(), ball.getY(), b.getX(), b.getY());
if(d <= 37.5)
{
ball.move();
hits++;
}
}*/
// private double distance(double x1, double y1, double x2, double y2)
// {
// return(Math.sqrt(Math.pow(x2 - x1, 2.0) + Math.pow(y2 - y1, 2.0))); // enter the calculation here.
//}
}
ありがとうございます!
スタートボタン](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html)および[アクション・リスナーの作成方法](https://docs.oracle.com/javase/tutorial/uiswing /events/actionlistener.html) – MadProgrammer
スイングはデフォルトでダブルバッファーであるため、独自のバッファリングを実装する必要はありません – MadProgrammer