我正在为我的足球比赛创建一个菜单。我有三个面板在一个面板下面。这四个都在一帧中显示。我用CardLayout
来做这件事。您可以从第一个面板切换到第二个面板,也可以从第二个面板切换到第三个面板。第三个面板是游戏应该运行的地方。问题是:第一个面板不刷新以显示玩家动作,它只显示第一帧。当我在代码中包含一个片段来刷新此窗口时,我的面板显示一个白色屏幕。如果我发表评论,我可以做上述操作。我需要帮助来解决这个问题。
以下是代码:
public class GameRunner {
JFrame frame = new JFrame(g_szApplicationName);
//game panels
JPanel mainPanel = new JPanel();
JPanel mainMenu = new JPanel();
JPanel teamSelect = new JPanel();
JPanel gamePlay = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
gdi.StartDrawing(hdcBackBuffer);
//fill our backbuffer with white
gdi.fillRect(Color.WHITE, 0, 0, WindowWidth, WindowHeight);
SoccerPitchLock.lock();
g_SoccerPitch.Render();
SoccerPitchLock.unlock();
gdi.StopDrawing(hdcBackBuffer);
g.drawImage(buffer, 0, 0, null);
}
};
//Buttons
JButton exit = new JButton("EXIT");
JButton cont = new JButton("CONTINUE");
JButton startGame = new JButton("START GAME");
JButton back = new JButton("BACK");
//instance of the cardlayout
CardLayout gr = new CardLayout();
//Game Globals
static String g_szApplicationName = "NHABVU";
static SoccerPitch g_SoccerPitch;
// bacause of game restart (g_SoccerPitch could be null for a while)
static Lock SoccerPitchLock = new ReentrantLock();
//create a timer
static PrecisionTimer timer = new PrecisionTimer(Prm.FrameRate);
//graphics
static BufferedImage buffer;
static Graphics2D hdcBackBuffer;
//these hold the dimensions of the client window area
static int cxClient;
static int cyClient;
public boolean gameOn = false;
//constructor
public GameRunner() throws InterruptedException {
frame.setIconImage(LoadIcon("/SimpleSoccer/icon1.png"));
buffer = new BufferedImage(WindowWidth, WindowHeight, BufferedImage.TYPE_INT_RGB);
hdcBackBuffer = buffer.createGraphics();
//these hold the dimensions of the client window area
cxClient = buffer.getWidth();
cyClient = buffer.getHeight();
//seed random number generator
common.misc.utils.setSeed(0);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
int y = center.y - frame.getHeight() / 2;
frame.setLocation(center.x - frame.getWidth() / 2, y >= 0 ? y : 0);
g_SoccerPitch = new SoccerPitch(cxClient, cyClient);
//set other sizes of JPanels
mainMenu.setSize(WindowWidth, WindowHeight);
teamSelect.setSize(WindowWidth, WindowHeight);
gamePlay.setSize(WindowWidth, WindowHeight);
//setframe size
frame.setPreferredSize(new Dimension(WindowWidth, WindowHeight));
gamePlay.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyChar()) {
case KeyEvent.VK_ESCAPE: {
System.exit(0);
}
break;
case 'r':
case 'R': {
SoccerPitchLock.lock();
g_SoccerPitch = null;
g_SoccerPitch = new SoccerPitch(cxClient, cyClient);
SoccerPitchLock.unlock();
}
break;
case 'p':
case 'P': {
g_SoccerPitch.TogglePause();
}
break;
case 'w':
case 'W':
gr.show(mainPanel, "2");
break;
}//end switch
}//end switch
});
frame.addComponentListener(new ComponentAdapter() {
//has the user resized the client area?
public void componentResized(ComponentEvent e) {
//if so we need to update our variables so that any drawing
//we do using cxClient and cyClient is scaled accordingly
cxClient = e.getComponent().getBounds().width;
cyClient = e.getComponent().getBounds().height;
//now to resize the backbuffer accordingly.
buffer = new BufferedImage(cxClient, cyClient, BufferedImage.TYPE_INT_RGB);
hdcBackBuffer = buffer.createGraphics();
}
});
mainPanel.setLayout(gr);
//add these to main menu
mainMenu.add(cont, BorderLayout.SOUTH);
mainMenu.add(exit, BorderLayout.NORTH);
mainMenu.setBackground(Color.blue);
//add these to second mmenu
teamSelect.add(startGame, BorderLayout.SOUTH);
teamSelect.add(back, BorderLayout.NORTH);
teamSelect.setBackground(Color.GREEN);
mainPanel.add(mainMenu, "1");
mainPanel.add(teamSelect, "2");
mainPanel.add(gamePlay, "3");
//inbuilt method to show panels
gr.show(mainPanel, "1");
//action listeners for buttons
cont.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent v) {
gr.show(mainPanel, "2");
gameOn = true;
}
});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent v) {
System.exit(0);
}
});
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent v) {
gr.show(mainPanel, "1");
gameOn = true;
}
});
startGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent v) {
gr.show(mainPanel, "3");
gameOn = true;
}
});
frame.add(mainPanel);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
timer.Start();
while (true) {
//update
if (timer.ReadyForNextFrame()) {
System.out.println("ANDREW");
SoccerPitchLock.lock();
g_SoccerPitch.Update();
SoccerPitchLock.unlock();
//render
gamePlay.repaint();
try {
//System.out.println(timer.TimeElapsed());
Thread.sleep(2);
} catch (InterruptedException ex) {
}
}
}//end while
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new GameRunner();
} catch (InterruptedException ex) {
Logger.getLogger(GameRunner.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
答案 0 :(得分:0)
好吧,伙计们我已经决定使用不同的概念。我使用框架隐藏来创建我的菜单,选择某个actionlistener将使某个框架可见而其他框架不可见。这里的问题是我应该在哪里调用更新部分在我进入游戏菜单之前,游戏不会开始更新自己的代码是代码
public class Main {
//--------------------------------- Globals ------------------------------
//
//------------------------------------------------------------------------
static String g_szApplicationName = "NHABVU";
//static String g_szWindowClassName = "MyWindowClass";
static SoccerPitch g_SoccerPitch;
// bacause of game restart (g_SoccerPitch could be null for a while)
static Lock SoccerPitchLock = new ReentrantLock();
//create a timer
static PrecisionTimer timer = new PrecisionTimer(Prm.FrameRate);
//static UserControlledTeam UserTeam = new UserControlledTeam(g_SoccerPitch.m_pRedTeam);
static BufferedImage buffer;
static Graphics2D hdcBackBuffer;
//these hold the dimensions of the client window area
static int cxClient;
static int cyClient;
static boolean gameOn;
public static void main(String[] args) throws InterruptedException {
//code segment for the first panel/the first panelm that is displayed
final Window MainMenu = new Window(g_szApplicationName);
MainMenu.setIconImage(LoadIcon("/SimpleSoccer/icon1.png"));
MainMenu.setResizable(false);
JPanel MainMenuPanel = new JPanel();
//create buttons for main menu
JButton Exit = new JButton("EXIT");
JButton Continue = new JButton("CONTINUE");
//add these buttons to the mainmenupanel
MainMenuPanel.add(Continue);
MainMenuPanel.add(Exit);
//code segment for the team select panel
//code segment for the first panel/the first panelm that is displayed
final Window TeamMenu = new Window(g_szApplicationName);
TeamMenu.setIconImage(LoadIcon("/SimpleSoccer/icon1.png"));
TeamMenu.setResizable(false);
JPanel TeamMenuPanel = new JPanel();
//create buttons for main menu
JButton StartGame = new JButton("PLAY");
JButton Back = new JButton("BACK");
JComboBox TeamSelect = new JComboBox();
String[] teams = {"Choose team...", "HIGHLANDERS", "DYNAMOS"};
TeamSelect.addItem(teams[0]);
TeamSelect.addItem(teams[1]);
TeamSelect.addItem(teams[2]);
//add these buttons to the mainmenupanel
//StartGame.setEnabled(false);
TeamMenuPanel.add(TeamSelect);
TeamMenuPanel.add(StartGame);
TeamMenuPanel.add(Back);
/*
* game window
* this is where the game will be played
*/
final Window window = new Window(g_szApplicationName);
window.setIconImage(LoadIcon("/SimpleSoccer/icon1.png"));
buffer = new BufferedImage(WindowWidth, WindowHeight, BufferedImage.TYPE_INT_RGB);
hdcBackBuffer = buffer.createGraphics();
//these hold the dimensions of the client window area
cxClient = buffer.getWidth();
cyClient = buffer.getHeight();
//seed random number generator
common.misc.utils.setSeed(0);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
//Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
window.setResizable(false);
//set window locations
int y = center.y - window.getHeight() / 2;
//setlocation of main window
window.setLocation(center.x - window.getWidth() / 2, y >= 0 ? y : 0);
//set location of main menu window
MainMenu.setLocation(center.x - window.getWidth() / 2, y >= 0 ? y : 0);
//set location for teaam select menu
TeamMenu.setLocation(center.x - window.getWidth() / 2, y >= 0 ? y : 0);
g_SoccerPitch = new SoccerPitch(cxClient, cyClient);
final JPanel panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
gdi.StartDrawing(hdcBackBuffer);
//fill our backbuffer with white
gdi.fillRect(Color.WHITE, 0, 0, WindowWidth, WindowHeight);
SoccerPitchLock.lock();
g_SoccerPitch.Render();
SoccerPitchLock.unlock();
gdi.StopDrawing(hdcBackBuffer);
g.drawImage(buffer, 0, 0, null);
}
};
//panel configurations
panel.setSize(WindowWidth, WindowHeight);
panel.setPreferredSize(new Dimension(WindowWidth, WindowHeight));
window.add(panel);
window.pack();
//MainMenuPanelConfigurations
MainMenuPanel.setSize(WindowWidth, WindowHeight);
MainMenuPanel.setPreferredSize(new Dimension(WindowWidth, WindowHeight));
MainMenu.add(MainMenuPanel);
MainMenu.pack();
//TeamMenuPanelConfigurations
TeamMenuPanel.setSize(WindowWidth, WindowHeight);
TeamMenuPanel.setPreferredSize(new Dimension(WindowWidth, WindowHeight));
TeamMenu.add(TeamMenuPanel);
TeamMenu.pack();
//add keylistener to window frame
window.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyChar()) {
case KeyEvent.VK_ESCAPE: {
System.exit(0);
}
break;
case 'r':
case 'R': {
SoccerPitchLock.lock();
g_SoccerPitch = null;
g_SoccerPitch = new SoccerPitch(cxClient, cyClient);
SoccerPitchLock.unlock();
}
break;
case 'p':
case 'P': {
g_SoccerPitch.TogglePause();
}
break;
}//end switch
}//end switch }
@Override
public void keyPressed(KeyEvent e) {
if(g_SoccerPitch.GameOn() && g_SoccerPitch != null){
switch (e.getKeyChar()) {
case 'w':
case 'W':{
System.out.println("W IS PRESSED");
//if player is within ballkickingrange kick the ball up
//if yu're at target switch off ball chasing
if( g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinReceivingRange()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveUp();
g_SoccerPitch.Ball().Kick(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().ballPos, 1.5 );
}
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOn();
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SetTarget(g_SoccerPitch.Ball().m_vPosition);
if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().AtTarget()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOff();
}
}
break;
case 's':
case 'S':{
System.out.println("S IS PRESSED");
//if player is within ballkickingrange kick the ball down
//if yu're at target switch off ball chasing
if( g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinReceivingRange()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveDown();
g_SoccerPitch.Ball().Kick(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().ballPos, 1.5 );
}
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOn();
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SetTarget(g_SoccerPitch.Ball().m_vPosition);
if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().AtTarget()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOff();
}
}
break;
case 'a':
case 'A':{
System.out.println("A IS PRESSED");
//if player is within ballkickingrange kick the ball up
//if yu're at target switch off ball chasing
if( g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinReceivingRange()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveLeft();
g_SoccerPitch.Ball().Kick(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().ballPos, 1.5 );
}
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOn();
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SetTarget(g_SoccerPitch.Ball().m_vPosition);
if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().AtTarget()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOff();
}
}
break;
case 'd':
case 'D':{
System.out.println("D IS PRESSED");
//if player is within ballkickingrange kick the ball up
//if yu're at target switch off ball chasing
if( g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinKickingRange()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveRight();
g_SoccerPitch.Ball().Kick(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().ballPos, 1.5 );
}
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOn();
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SetTarget(g_SoccerPitch.Ball().m_vPosition);
if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().AtTarget()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOff();
}
}
break;
case 'm':
case 'M':{
if(g_SoccerPitch.UserControlledTeam.InControl() && g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinReceivingRange()){
System.out.println("M IS PRESSED");
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().UserPlayerShootBall();
}
}
break;
case 'l':
case 'L':{
if(g_SoccerPitch.UserControlledTeam.InControl()){
System.out.println("L IS PRESSED");
if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinReceivingRange()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().UserPlayerPassBall();
}
} else{
System.out.println("Marking The Ball");
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOn();
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SetTarget(g_SoccerPitch.Ball().m_vPosition);
if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().AtTarget()){
g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOff();
}
}
}
break;
}//end switch
}//end switch
}//closes if
});
window.addComponentListener(new ComponentAdapter() {
@Override //has the user resized the client area?
public void componentResized(ComponentEvent e) {
//if so we need to update our variables so that any drawing
//we do using cxClient and cyClient is scaled accordingly
cxClient = e.getComponent().getBounds().width;
cyClient = e.getComponent().getBounds().height;
//now to resize the backbuffer accordingly.
buffer = new BufferedImage(cxClient, cyClient, BufferedImage.TYPE_INT_RGB);
hdcBackBuffer = buffer.createGraphics();
}
});
//make the mainmenu visible when you start the game
MainMenu.setVisible(true);
//add actionlisteners
Continue.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent v){
window.setVisible(false);
MainMenu.setVisible(false);
TeamMenu.setVisible(true);
g_SoccerPitch.SetGameOff();
}
});
Exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent v){
System.exit(0);
}
});
StartGame.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent v){
window.setVisible(true);
MainMenu.setVisible(false);
TeamMenu.setVisible(false);
//gameOn=true;
g_SoccerPitch.SetGameOff();
}
});
Back.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent v){
window.setVisible(false);
MainMenu.setVisible(true);
TeamMenu.setVisible(false);
gameOn=false;
}
});
TeamSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selected = (String)TeamSelect.getSelectedItem();
if (selected.equals("HIGHLANDERS")) {
g_SoccerPitch.setUserControlledTeam(g_SoccerPitch.m_pRedTeam);
g_SoccerPitch.UserControlledTeam.setToBeUserControlledTeam(true);
} else if (selected.equals("DYNAMOS")) {
g_SoccerPitch.setUserControlledTeam(g_SoccerPitch.m_pBlueTeam);
g_SoccerPitch.UserControlledTeam.setToBeUserControlledTeam(true);
}
}
});
timer.Start();
while (true) {
//update
if (timer.ReadyForNextFrame()) {
SoccerPitchLock.lock();
g_SoccerPitch.Update();
SoccerPitchLock.unlock();
//render
//panel.revalidate();
panel.repaint();
try {
//System.out.println(timer.TimeElapsed());
Thread.sleep(2);
} catch (InterruptedException ex) {
}
}
}//end while
}
}