无法将椭圆绘制到JPanel

时间:2013-12-28 23:38:43

标签: java swing user-interface jpanel paintcomponent

我正在制作一个跳棋游戏,以帮助我学习使用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的圆圈或任何内容。

2 个答案:

答案 0 :(得分:2)

有许多基本问题......

  1. 您的RedPieceDrawer没有任何大小调整提示,允许布局管理员决定如何最好地布局此组件
  2. 默认情况下,JLayeredPane根本不使用布局管理器,这意味着当您向其添加组件时,它们没有初始大小,这意味着无法呈现
  3. 使用您当前的方法,您也会遇到将棋盘片放在棋盘上的问题。

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

    例如

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

答案 1 :(得分:0)

为何延伸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);
    }

}
相关问题