繁体   English   中英

无法将椭圆绘制到JPanel

[英]Having Trouble drawing an oval to a JPanel

我正在制作一个跳棋游戏来帮助我学习使用java的GUI。 我使用JLayeredPane boardAndPieces来容纳2个JPanels:board和boardPiecesPanel。 董事会还有2个JPanels: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根本不使用布局管理器,这意味着当您向其添加组件时,它们没有初始大小,这意味着它不会呈现

使用您当前的方法,您也会遇到将检查器件放在板上的问题。

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

例如

跳棋

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 ,以便您可以管理用户交互......

为何延伸JPanel? 只需创建一个可以绘制自己的对象:

public class RedPiece{

    //..ivars
    protected int x;
    protected int y;
    protected int w;
    protected int h;
    protected Color color = Color.RED;

    public void draw(Graphics g){

        g.setColor(color);
        g.drawOval(x, y, w, h);

    }
}

然后在一些JPanel上绘制它:

public class GamePanel extends JPanel{

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        for (RedPiece p : pieces)
            p.draw(g);
    }

}

暂无
暂无

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

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