简体   繁体   English

为什么我的董事会不出现? 用Java创建游戏

[英]Why won't my board appear? Creating a game in java

First off I would like to say that I understand I am asking a lot so any help would be appreciated and I thank you for your time. 首先,我想说的是,我理解我的要求很多,因此我们将不胜感激,感谢您的宝贵时间。 I am extremely grateful. 我万分感谢。

Anyway I am creating a game for my A2 coursework, most commonly known as Checkers. 无论如何,我正在为我的A2课程创建游戏,通常被称为“跳棋”。 I have completed my code and everything works as I had planned except that the CheckerBoard itself as well as the checkerpieces do not appear to be showing. 我已经完成了代码,所有工作均按计划进行,只是CheckerBoard本身以及棋盘格未显示。

The section of were my board should be present is just a black space. 我的董事会应该出现的部分只是一个空白。 Although my board does not appear to be displaying, all of the actions I perform on it such as clicking certain section produces the planned response, and although I've checked through my code I cannot work out what I've done wrong. 尽管我的面板似乎没有显示,但是我在面板上执行的所有操作(例如单击某些部分)都会产生计划的响应,尽管我检查了我的代码,但无法找出做错了什么。

Anyway if anyone could possibly spot my mistake or perhaps give me some advice I would be extremely grateful. 无论如何,如果有人能发现我的错误或给我一些建议,我将非常感激。 Thank you 谢谢

As I of course can't upload my entire code I will do snippets of where I think the problem might lie or just important sections. 因为我当然不能上传我的全部代码,所以我将对我认为问题可能出在哪里或只是重要部分进行摘要。 Thank you 谢谢

CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line
        application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard
        application.pack();  // Use preferred size of content to set size of application.
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        application.setLocation( (screensize.width - application.getWidth())/2,
                (screensize.height - application.getHeight())/2 );
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Sets so that the application can be exited when the application is closed
        application.setResizable(false);  // This makes it so that the user can't change the application's size.
        application.setVisible(true); // Sets it so that the application can actually be seen

The code above is placed within a "public static void main(String[] args)" 上面的代码位于“公共静态void main(String [] args)”中

Then below that I have: 然后在下面我有:

public CheckerBoard()  {
// This is going to be a constructor, this constructor will set the layout manager for the panel to be null, it will then add components to the panel
// and it will set their bounds.

    setLayout(null);  // So that it will match my requirement specification, I will do the layout myself
    setBackground(new Color(0,120,0));  // Dark Green Background colour.
    setPreferredSize(new Dimension(350,250)); // The size of the Panel

    BoardComplete checkerboardData = new BoardComplete();
    add(checkerboardData); // This will create the components and add them to the content pane
    add(NewGameButton);
    add(ResignButton);
    add(MessageDisplay);

 // I will now have to produce a method to set the position and size of each component by calling its setBounds() method
    checkerboardData.setBounds(20,20,164,164); // Sets the board dimensions
    NewGameButton.setBounds(210, 60, 120, 30); 
    ResignButton.setBounds(210, 120, 120, 30); 
    MessageDisplay.setBounds(20, 200, 350, 30); 
    }   

And then finally I have a another public class called BoardComplete which contains all the relevant code involving the actionlistners ect. 最后,我还有一个名为BoardComplete的公共类,其中包含涉及actionlistners的所有相关代码。 used: 用过的:

 BoardComplete() {
        setBackground(Color.BLACK);
        addMouseListener(this);
        ResignButton = new JButton("Resign");
        ResignButton.addActionListener(this);
        NewGameButton = new JButton("New Game");
        NewGameButton.addActionListener(this);
        MessageDisplay = new JLabel("",JLabel.CENTER);
        MessageDisplay.setFont(new  Font("Serif", Font.BOLD, 14));
        MessageDisplay.setForeground(Color.green);
        checkerboardData = new DataForCheckers();
        MakeaNewGame();
    }       

I understand that this is a lot to ask but any help would be greatly appreciate and I would be extremely grateful. 我知道有很多问题要问,但是任何帮助将不胜感激,我将非常感谢。 Thank you. 谢谢。

Edit due to comment: This is the code for my Paint Class: 根据评论进行编辑:这是我的绘画班的代码:

    public void PaintCheckerBoard(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // This will draw a two-pixel black border around the edges of the canvas. 

        g.setColor(Color.black);
        g.drawRect(0,0,getSize().width-1,getSize().height-1);
        g.drawRect(1,1,getSize().width-3,getSize().height-3);

        // Draw the squares of the checkerboard and the checkers.

        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                if ( row % 2 == col % 2 )
                    g.setColor(Color.LIGHT_GRAY);
                else
                    g.setColor(Color.GRAY);
                g.fillRect(2 + col*20, 2 + row*20, 20, 20);
                switch (checkerboardData.PieceLocation(row,col)) {
                case DataForCheckers.RED:
                    g.setColor(Color.RED);
                    g.fillOval(4 + col*20, 4 + row*20, 15, 15);
                    break;
                case DataForCheckers.BLACK:
                    g.setColor(Color.BLACK);
                    g.fillOval(4 + col*20, 4 + row*20, 15, 15);
                    break;
                case DataForCheckers.RED_KING:
                    g.setColor(Color.RED);
                    g.fillOval(4 + col*20, 4 + row*20, 15, 15);
                    g.setColor(Color.WHITE);
                    g.drawString("K", 7 + col*20, 16 + row*20);
                    break;
                case DataForCheckers.BLACK_KING:
                    g.setColor(Color.BLACK);
                    g.fillOval(4 + col*20, 4 + row*20, 15, 15);
                    g.setColor(Color.WHITE);
                    g.drawString("K", 7 + col*20, 16 + row*20);
                    break;
                }
            }
        }

        // If there is a game in progress, highlight the legal moves that the player can make.
        // It can be seen that in this process, LegalMoves is never null while a game is in progress     

        if (CheckerMatchInProgress) {
            /* First, draw a 2-pixel cyan border around the pieces that can be moved. */
            g.setColor(Color.cyan);
            for (int i = 0; i < LegalMoves.length; i++) {
                g.drawRect(2 + LegalMoves[i].fromCol*20, 2 + LegalMoves[i].fromRow*20, 19, 19);
                g.drawRect(3 + LegalMoves[i].fromCol*20, 3 + LegalMoves[i].fromRow*20, 17, 17);
            }
            // If a piece is selected to be moved, for example if ChosenRow>=0, then
            // draw a 2-pixel white border around that piece, and draw a green border
            // around each square that the piece can be legally moved to.

            if (ChosenRow >= 0) {
                g.setColor(Color.white);
                g.drawRect(2 + ChosenColumn*20, 2 + ChosenRow*20, 19, 19);
                g.drawRect(3 + ChosenColumn*20, 3 + ChosenRow*20, 17, 17);
                g.setColor(Color.green);
                for (int i = 0; i < LegalMoves.length; i++) {
                    if (LegalMoves[i].fromCol == ChosenColumn && LegalMoves[i].fromRow == ChosenRow) {
                        g.drawRect(2 + LegalMoves[i].toCol*20, 2 + LegalMoves[i].toRow*20, 19, 19);
                        g.drawRect(3 + LegalMoves[i].toCol*20, 3 + LegalMoves[i].toRow*20, 17, 17);
                    }
                }
            }
        }

    }  // end PaintCheckerBoard()

So looking through your code on the other site, your custom paint method never seems to be getting called from anywhere. 因此,在其他站点上浏览代码时,似乎从未从任何地方调用过您的自定义绘制方法。 You're supposed to override the paintComponent method from the Component class (JPanel is a subclass of Component). 您应该重写Component类中的paintComponent方法(JPanel是Component的子类)。 This paintComponent method will be called whenever swing thinks, or is told to redraw something. 每当swing进行思考或被告知重绘某些内容时,都会调用此paintComponent方法。

So, add this code to your BoardComplete class: 因此,将此代码添加到您的BoardComplete类中:

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    PaintCheckerBoard(g);
} 

super.paintComponent will do anything swing would do to paint this component (which at this point is just drawing the background). super.paintComponent将执行任何摆动操作以绘制此组件的操作(此时仅绘制背景)。 And then you pass the Graphics object to your own custom paint method, that method will draw your entire game on the Graphics object, and swing will display whatever is drawn on there for you. 然后,将Graphics对象传递给自己的自定义绘制方法,该方法将在Graphics对象上绘制整个游戏,并且swing将为您显示绘制在该对象上的所有内容。

So now you have to remember also to call repaint() every time something in your game changes. 因此,现在您必须记住,每次游戏中的某些变化时,都要调用repaint()。 The repaint() method will just tell swing he should repaint something (note: this tells the awt thread it should repaint the component, so it happens asynchronously from where youre calling repaint()). repaint()方法只会告诉swing他应该重新绘制某些内容(注意:这告诉awt线程应该重新绘制组件,因此它从调用repaint()的位置异步发生)。

If youre still confused, you should probably look up some information or tutorials on how to draw with awt/swing using this method. 如果您仍然感到困惑,则可能应该查找一些有关如何使用此方法使用awt / swing进行绘制的信息或教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM