gpt4 book ai didi

java - 图形绘制图像双陆棋游戏

转载 作者:行者123 更新时间:2023-12-01 18:46:07 26 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,但我只是一个初学者,所以怜悯我的灵魂:)。所以我的问题是我有一个名为游戏页面的类,它是显示所有游戏的 IFrame。在它里面我有一个名为 Board 的类,其中包含一张板子的图片,它可以很好地使用:

public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(image, 0, 0, null);
}

图像是我得到的板的图像,如下所示:

image = ImageIO.read(new File("pics/board.png"));

好的,效果很好。所以在板子里面我也有一个“Slot”类型的二维数组,Slot是我编写的另一个类,它有一个WhitePiece堆栈或BlackPiece堆栈这些是我编写的代表棋盘上黑色或白色棋子的类。

因此,在 Board 类中,有一个名为“reset board”的方法,它将其组织到双陆棋的起始位置,以便初始化所有插槽。

现在黑白片在图像类型中得到了一张图片,我得到的是这样的: pic = ImageIO.read(new File("pics/whitePiece.png"));

现在的问题是当我在重置板内调用 Slot 类中的 drawSlot 方法时我认为它无法获取图片。我这样调用它:首先我调用drawBoard: 绘图板(板);

然后这是绘图板:

  public void drawBoard(Slot[][] board) throws IOException{
for(int i=0;i<2;i++){
for(int j=0;j<12;j++){
board[i][j].drawSlot(getGraphics());
}
}
}

这是有问题的方法:drawSlot:

  public void drawSlot(Graphics g) throws IOException{    
if(type == SlotType.empty){
System.out.println("no type selected slot is empty Slot Number"+slotNumber);
}else
if(type == SlotType.white){
if(!wPieces.isEmpty()){
Image pic = wPieces.pop().getPic();
wPieces.push(new WhitePiece());
if(slotNumber <= 11){
for(int i=0;i<piecesAmount;i++){
g.drawImage(pic, 5, i*30, null);
}
}
else{
for(int i=0;i<piecesAmount;i++){
g.drawImage(pic, 5,300-(i*30), null);
}
}
}else{
System.out.println("Slot Stack is Empty Slot #"+slotNumber);
}
}else
{
if(!bPieces.isEmpty()){
Image pic = bPieces.pop().getPic();
bPieces.push(new BlackPiece());
if(slotNumber<=11){
for(int i=0;i<piecesAmount;i++){
g.drawImage(pic, 5, i*30, 30, 30, null);
}
}else{
for(int i=0;i<piecesAmount;i++){
g.drawImage(pic, 5, 300-(i*30), 30, 30, null);
}
}
}
else{
System.out.println("Slot Stack is empty Slot #"+slotNumber);
}
}

}

这是我得到的错误:(我认为这表明我获取图片的方式不好。)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Try1.Slot.drawSlot(Slot.java:122)
at Try1.Board.drawBoard(Board.java:128)
at Try1.Board.resetBoard(Board.java:122)
at Try1.GamePage.<init>(GamePage.java:98)
at Try1.StartPage.startGame(StartPage.java:67)
at Try1.StartPage$eventHandler.actionPerformed(StartPage.java:49)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我很想知道我做错了什么。谢谢大家,我知道这是一篇很长的文章,你真的必须投入其中但我是初学者,需要帮助:)。

最佳答案

不要在 swing 组件上使用 getGraphics()。在 paintComponent() 中绘制插槽。

public void paintComponent(Graphics page) {
super.paintComponent(page);
page.drawImage(image, 0, 0, null);
drawBoard(page, board);
}

并修改drawBoard()以使用传递的图形对象:

public void drawBoard(Graphics g, Slot[][] board) {
for(int i=0;i<2;i++) {
for(int j=0;j<12;j++){
board[i][j].drawSlot(g);
}
}
}

然后当棋盘发生变化时调用repaint()

关于java - 图形绘制图像双陆棋游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17771199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com