我相信問題出在我的 GameEngine 的滴答聲之間。換句話說,我認為在一個游戲中的緩沖區中,我認為 GUI 元素出于某種原因會變為 null。這是我的代碼:GameEngine 類:
package pong;
import static pong.Main.window;
public class GameEngine{
static GameState gameState;
private long timeElapsed;
public GameEngine(){
gameState=new GameState(0);//0 is for the menu
}
public void run(){
long beginTime=System.nanoTime();
//33 million is 30 fps
if(timeElapsed>=33333333){
gameState.buffer();
timeElapsed-=33333333;
}
window.getPanel().repaint();
timeElapsed =System.nanoTime()-beginTime;
}
public long getTimeElapsed(){
return timeElapsed;
}
}
GUIButton 類:
package pong;
import java.awt.Color;
import java.awt.Graphics2D;
import static pong.Panel.mouseClicked;
import static pong.Panel.mouseX;
import static pong.Panel.mouseY;
public abstract class GUIButton extends GUI{
private boolean mouseOverButton,readyForButtonAction;
@Override
public void paint(Graphics2D g) {
g.setColor(Color.white);
if(mouseOverButton){
g.fillRect(x-5,y-5,110,40);
g.setColor(Color.black);
g.fillRect(x, y, 100, 30);
}
else{
g.fillRect(x,y,100,30);
g.setColor(Color.black);
g.fillRect(x 5, y 5, 90, 20);
}
}
@Override
public void buffer(){
mouseOverButton=mouseX-8>x&&mouseX-8<x 100&&
mouseY-32>y&&mouseY-32<y 30;
if(mouseOverButton&&mouseClicked)
readyForButtonAction=true;
}
public void buttonAction(){
readyForButtonAction=false;
mouseClicked=false;
}
public boolean buttonActionIsReady(){
return readyForButtonAction;
}
}
GUIButtonMultiplayer(將您轉移到標題 GUI 到多人游戲選項 GUI 的按鈕)
package pong;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import static pong.GameState.GUI;
public class GUIButtonMultiPlayer extends GUIButton{
public GUIButtonMultiPlayer(){
x=300;
y=300;
}
@Override
public void paint(Graphics2D g) {
super.paint(g);
g.setColor(Color.white);
g.setFont(new Font("Serif",Font.BOLD,12));
g.drawString("Multiplayer",x 10,y 20);
}
@Override
public void buttonAction() {
super.buttonAction();
GUI=new GUI[4];
GUI[0]=new GUIButtonLocal();
GUI[1]=new GUIButtonJoin();
GUI[2]=new GUIButtonHost();
GUI[3]=new GUIButtonBackMultiPlayer();
}
}
Panel 類(包含 mouseClicked 變數)
package pong;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import static pong.GameEngine.gameState;
public class Panel extends JPanel implements KeyListener,MouseListener,MouseMotionListener{
static int mouseX,mouseY;
static boolean mouseDown,mouseClicked;
private boolean keyDown[];
public Panel(){
keyDown=new boolean[4];
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
gameState.paint((Graphics2D)g);
}
public boolean getKeyDown(int index){
return keyDown[index];
}
@Override
public void keyTyped(KeyEvent ke) {}
@Override
public void keyPressed(KeyEvent ke) {
switch(ke.getKeyCode()){
case KeyEvent.VK_DOWN:
keyDown[0]=true;
break;
case KeyEvent.VK_UP:
keyDown[1]=true;
break;
case KeyEvent.VK_W:
keyDown[2]=true;
break;
case KeyEvent.VK_S:
keyDown[3]=true;
break;
}
}
@Override
public void keyReleased(KeyEvent ke) {
switch(ke.getKeyCode()){
case KeyEvent.VK_DOWN:
keyDown[0]=false;
break;
case KeyEvent.VK_UP:
keyDown[1]=false;
break;
case KeyEvent.VK_W:
keyDown[2]=false;
break;
case KeyEvent.VK_S:
keyDown[3]=false;
break;
}
}
@Override
public void mouseClicked(MouseEvent me) {
mouseClicked=true;
}
@Override
public void mousePressed(MouseEvent me) {
mouseDown=true;
}
@Override
public void mouseReleased(MouseEvent me) {
mouseDown=false;
}
@Override
public void mouseEntered(MouseEvent me) {}
@Override
public void mouseExited(MouseEvent me) {}
@Override
public void mouseDragged(MouseEvent me) {}
@Override
public void mouseMoved(MouseEvent me) {
mouseX=me.getX();
mouseY=me.getY();
}
}
GameState 類(實作)
package pong;
import java.awt.Color;
import java.awt.Graphics2D;
import static pong.Panel.mouseClicked;
public class GameState{
static GUI GUI[];
private int ID;
private Ball ball;
private Paddle paddles[];
public GameState(int id){
ID=id;
switch(ID){
case 0://title screen
GUI=new GUI[4];
GUI[0]=new GUITitle();
GUI[1]=new GUIButtonSinglePlayer();
GUI[2]=new GUIButtonMultiPlayer();
GUI[3]=new GUIButtonExit();
break;
case 1://single player
paddles=new Paddle[2];
//true indicates that it is a left paddle
paddles[0]=new PaddlePlayer(true);
paddles[1]=new PaddleCPU();
GUI=new GUI[1];
GUI[0]=new GUIScores();
ball=new Ball(paddles,(GUIScores)GUI[0]);
((PaddleCPU)paddles[1]).setBall(ball);
break;
case 2://local multiplayer
paddles=new Paddle[2];
//true indicates that it is a left paddle
paddles[0]=new PaddlePlayer(true);
paddles[1]=new PaddlePlayer(false);
GUI=new GUI[1];
GUI[0]=new GUIScores();
ball=new Ball(paddles,(GUIScores)GUI[0]);
break;
case 3://host multiplayer
paddles=new Paddle[2];
paddles[0]=new PaddleMultiplayer();
paddles[1]=new PaddleMultiplayer();
GUI=new GUI[1];
GUI[0]=new GUIScores();
ball=new Ball(paddles,(GUIScores)GUI[0]);
break;
}
}
public void paint(Graphics2D g){
g.setColor(Color.black);
g.fillRect(0,0,800,600);
switch(ID){
case 0://title screen
System.out.println();
for(GUI gui:GUI){
System.out.println(gui==null);
gui.paint(g);
}
break;
case 1:case 2:case 3://single player and multiplayer
g.setColor(Color.white);
for(int a=10;a<600;a =20)
g.fillRect(380, a, 10, 10);
ball.paint(g);
for(Paddle paddle:paddles)
paddle.paint(g);
GUI[0].paint(g);
break;
}
}
public void buffer(){
switch(ID){
case 0://title screen
for(GUI gui:GUI)
if(gui instanceof GUIButton && ((GUIButton)gui).buttonActionIsReady()){
((GUIButton)gui).buttonAction();
break;
}
else gui.buffer();
break;
case 1:case 2:case 3://single player and multiplayer
ball.buffer();
for(Paddle paddle:paddles)
paddle.buffer();
break;
}
mouseClicked=false;
}
public int getID(){
return ID;
}
}
此代碼在繪制 GUI 時生成 NullPointerException。如果我注釋掉該行
gui.paint(g);
然后它只是創建一個 NullPointerException ,而不是緩沖 GUI。這是輸出的一個片段:
false
true
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
false
false
false
false
at pong.GameState.paint(GameState.java:62)
at pong.Panel.paintComponent(Panel.java:25)
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
at javax.swing.JComponent.paint(JComponent.java:1056)
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
false
false
false
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
false
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
哪個 GUI 元素顯示為 null 似乎是隨機的。
uj5u.com熱心網友回復:
對我來說,您似乎在為 GUI 元素使用靜態 GameState.GUI 陣列(如果我錯了,請糾正我)。
在代碼的其他地方,您將按順序訪問這個靜態 GUI 陣列。似乎有時其他代碼會改變這個靜態 GUI 陣列的內容(像這樣的代碼可能是罪魁禍首GUI=new GUI[1];),而其他代碼即將開始回圈遍歷該陣列。您必須更改代碼,以免發生這種情況。您可以同步訪問此靜態 GUI 陣列的方法(但這會阻止其他方法并使它們等待,這對于您的用例可能適用,也可能不適用)。
如果您確定這確實是問題的原因(可能不是),并且您想避免使用同步方法,那么您還有其他一些選擇。您可以研究雙緩沖的作業原理。使用 2 個陣列,始終從主要陣列讀取并始終修改次要陣列,并在它們之間進行交換。
或者,如果您想保留當前代碼,您可以更改這些模式:
GUI=new GUI[4];
GUI[0]=new GUITitle();
GUI[1]=new GUIButtonSinglePlayer();
GUI[2]=new GUIButtonMultiPlayer();
GUI[3]=new GUIButtonExit();
在您使用的代碼中隨處可見這種模式GUI=new GUI[...];:
GUI[] newGui=new GUI[4];
newGui[0]=new GUITitle();
newGui[1]=new GUIButtonSinglePlayer();
newGui[2]=new GUIButtonMultiPlayer();
newGui[3]=new GUIButtonExit();
GUI=newGui
這可能足以避免這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/376585.html
上一篇:如何避免組件相互推上/下
