实际上我正在使用java进行游戏,当用户点击我在课堂上创建的jButton(新游戏)时,我想开始新游戏' Window&#39 ;在包含我所有游戏组件的同一个包下。现在我想访问方法' newGame()'在我的buttonActionPerformed()方法的类Framework中定义,但没有调用框架的构造函数。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Framework f = new Framework();
f.newGame();
}
我使用此代码,但由于我使用了
,因此显示了许多错误 this.setContentPane(new Framework());
public class Framework extends Canvas {
public Framework ()
{
super();
gameState = GameState.VISUALIZING;
//We start game in new thread.
Thread gameThread = new Thread() {
@Override
public void run(){
GameLoop();
}
};
gameThread.start();
}
private void GameLoop()
{
long visualizingTime = 0, lastVisualizingTime = System.nanoTime();
// This variables are used for calculating the time that defines for how long we should put threat to sleep to meet the GAME_FPS.
long beginTime, timeTaken, timeLeft;
while(true)
{
beginTime = System.nanoTime();
switch (gameState)
{
case PLAYING:
gameTime += System.nanoTime() - lastTime;
game.UpdateGame(gameTime, mousePosition());
lastTime = System.nanoTime();
break;
case GAMEOVER:
//...
break;
case MAIN_MENU:
//...
break;
case OPTIONS:
//...
break;
case GAME_CONTENT_LOADING:
//...
break;
case STARTING:
// Sets variables and objects.
Initialize();
// Load files - images, sounds, ...
LoadContent();
// When all things that are called above finished, we change game status to main menu.
gameState = GameState.MAIN_MENU;
break;
case VISUALIZING:
if(this.getWidth() > 1 && visualizingTime > secInNanosec)
{
frameWidth = this.getWidth();
frameHeight = this.getHeight();
// When we get size of frame we change status.
gameState = GameState.STARTING;
}
else
{
visualizingTime += System.nanoTime() - lastVisualizingTime;
lastVisualizingTime = System.nanoTime();
}
break;
}
// Repaint the screen.
repaint(); // goes to paint component in canvas
timeTaken = System.nanoTime() - beginTime;
timeLeft = (GAME_UPDATE_PERIOD - timeTaken) / milisecInNanosec;
if (timeLeft < 10)
timeLeft = 10; //set a minimum
try {
Thread.sleep(timeLeft);
} catch (InterruptedException ex) { }
}
}
public void newGame()
{
// We set gameTime to zero and lastTime to current time for later calculations.
if(true)
{
BufferedImage blankCursorImg =
new BufferedImage(16, 16,BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor =
Toolkit.getDefaultToolkit().createCustomCursor(blankCursorImg, new Point(0, 0), null);
this.setCursor(blankCursor);
}
gameTime = 0;
lastTime = System.nanoTime();
game = new Game();
}
}
这里是WindowForm.java,它包含main()和jButtons 包直升机;
import javax.swing.JFrame;
public class WindowForm extends javax.swing.JFrame {
public WindowForm() {
this.setTitle("Helicopter battle");
if(true) // Full screen mode
{
this.setUndecorated(true);
this.setExtendedState(this.MAXIMIZED_BOTH);
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Creates the instance of the Framework.java that extends the Canvas.java and puts it on the frame.
this.setContentPane(new Framework());
initComponents();
jButton1.setBounds(630,490,100,30);
jButton2.setBounds(630,540,100,30);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Framework f = new Framework();
f.newGame();
jButton1.setVisible(false);
jButton2.setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WindowForm().setVisible(true);
}
});
}
public javax.swing.JButton jButton1;
public javax.swing.JButton jButton2;
// End of variables declaration
}
请帮助和谢谢。
答案 0 :(得分:0)
如果我理解正确,您想在不newGame()
类
Framework
方法
我认为你可以通过在构造函数之前在WindowForm
类中声明一个final变量来实现这一点
private final Framework myFramework = new Framework();
然后将其传递给setContentPane
方法
setContentPane(myFramework);
并在jButton1ActionPerformed
方法
myFramework.newGame();
-
另一种解决方案是使用Singleton设计模式从Framework
类创建静态实例,并在需要时调用Framework.getInstance()