java - 在 JPanel 上绘制椭圆时遇到问题

标签 java swing user-interface jpanel paintcomponent

我正在制作一个跳棋游戏来帮助我使用 java 学习 GUI。我正在使用 JLayeredPane boardAndPieces 来容纳 2 个 JPanel:board 和 boardPiecesPanel。 board 还包含 2 个 JPanel:boardPanel 和 boardButtonPanel。 boardPanel 是跳棋游戏板,据我所知,到目前为止没有任何问题。 boardButtonPanel 包含几个用于播放的按钮,但也不应该引起问题。相关代码如下:

`

boardAndPieces = new JLayeredPane();//a layered pane to hold the pieces and board panels
board = new JPanel(new BorderLayout());//A panel to hold the board and buttons
boardPanel = new JPanel(new GridLayout(8, 8));//the checker board. Within the JPanel board
boardPiecesPanel = new JPanel(new GridLayout(8,8));//The panel that will hold the pieces
board.add(boardPanel, BorderLayout.NORTH);
boardButtonPanel = new JPanel(new GridLayout(1, 3));//holds the buttons used for play. Within the JPanel board
board.add(boardButtonPanel, BorderLayout.SOUTH);
//...add to the board in this space. This works fine

boardAndPieces.add(board, Integer.valueOf(1));
boardAndPieces.add(boardPiecesPanel, Integer.valueOf(2));

//Draw initial pieces
boardPiecesPanel.add(new RedPieceDrawer);


//Class RedPieceDrawer looks like this:
class RedPieceDrawer extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.drawOval(30, 30, 40, 40);

    }
}

`

使用此代码,会出现按钮和棋盘,但不会出现圆圈或 boardPiecesPanel 中的任何内容。

最佳答案

有一些基本问题......

  1. 您的 RedPieceDrawer 没有任何大小调整提示,无法让布局管理器决定如何最好地布局此组件
  2. 默认情况下,JLayeredPane 根本不使用布局管理器,这意味着当您向其中添加组件时,它们没有初始大小,这意味着不会呈现

按照您当前的方法,您在开始将棋子放置到棋盘上时也会遇到问题。

更好的方法可能是简单地创建一系列充当单元的面板,然后简单地将游戏 block 直接添加到...

例如

Checkers

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Checkers {

    public static void main(String[] args) {
        new Checkers();
    }

    private JPanel[] cells;

    public Checkers() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(8, 8));

                cells = new JPanel[8 * 8];
                int col = 0;
                int counter = 0;
                for (int index = 0; index < cells.length; index++) {

                    JPanel panel = new JPanel(new BorderLayout());
                    cells[index] = panel;
                    panel.setPreferredSize(new Dimension(40, 40));
                    panel.setMinimumSize(panel.getPreferredSize());
                    if (counter % 2 == 0) {
                        panel.setBackground(Color.BLACK);
                    } else {
                        panel.setBackground(Color.WHITE);
                    }

                    frame.add(panel);

                    counter++;
                    col++;
                    if (col > 7) {
                        counter++;
                        col = 0;
                    }

                }

                int row = 3;
                col = 4;
                int cell = (row * 8) + col;

                cells[cell].add(new GamePiece(Color.RED));

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class GamePiece extends JPanel {

        public GamePiece(Color color) {
            setOpaque(false);
            setBackground(color);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int dim = Math.max(getWidth() - 4, getHeight() - 4);
            int x = (getWidth() - dim) / 2;
            int y = (getHeight() - dim) / 2;
            g2d.setColor(getBackground());
            g2d.fillOval(x, y, dim, dim);
            g2d.dispose();
        }

    }

}

现在,如果您不愿意进行单元格转换,您可以使用二维数组。

我还想使用附加到每个面板和/或游戏片段的单个MouseListener,以便您可以管理用户交互...

关于java - 在 JPanel 上绘制椭圆时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20820554/

相关文章:

java - 从 Spring Boot 中排除 Maven 依赖项

java - java 程序中双数的系统输出

java - 调整多个 JPanel 的大小以适应窗口

database - 以某种规范格式或 "as entered"存储电话号码更好吗?

java - 调用一个java方法,询问用户公司的员 worker 数(无参数,返回值> = 0)

java - 使用java替换字符串中的字符

java - 当我使用选项卡导航到某个字段并输入一些文本时,文本消失了?

java - 对执行 Swing 应用程序的主类中的 SwingUtilities.invokeLater() 方法的调用到底是什么?

python - cx_freeze windows 32 程序无法运行

java - 如何为多个不同的无名 JButton 设置 ActionListener。当他们都有相同的名字或没有名字时,如何能够独立对待他们?