[英]Having Trouble drawing an oval to a JPanel
I am making a checkers game to help me learn GUI's with java. 我正在制作一个跳棋游戏来帮助我学习使用java的GUI。 I am using a JLayeredPane boardAndPieces to hold 2 JPanels: board and boardPiecesPanel. 我使用JLayeredPane boardAndPieces来容纳2个JPanels:board和boardPiecesPanel。 board holds 2 more JPanels: boardPanel and boardButtonPanel. 董事会还有2个JPanels:boardPanel和boardButtonPanel。 boardPanel is the checkers game board and, as far as I can tell, has no issues so far. boardPanel是跳棋游戏板,据我所知,到目前为止没有任何问题。 boardButtonPanel holds several buttons used for play, but shouldn't be causing a problem either. boardButtonPanel拥有几个用于播放的按钮,但也不应该导致问题。 Here is the relevant code: 这是相关代码:
` `
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);
}
}
` `
Using this code, The buttons and checkerboard appear, but no circle or anything from boardPiecesPanel appears. 使用此代码,会出现按钮和棋盘格,但不会出现来自boardPiecesPanel的圆圈或任何内容。
There are a number of basic issues... 有一些基本问题......
RedPieceDrawer
does not have any sizing hints that would allow a layout manager to make decisions about how best to layout this component 您的RedPieceDrawer
没有任何大小调整提示,允许布局管理员决定如何最好地布局此组件 JLayeredPane
does not use a layout manager at all, meaning that when you add components to it, they have no initial size, meaning that won't render 默认情况下, JLayeredPane
根本不使用布局管理器,这意味着当您向其添加组件时,它们没有初始大小,这意味着它不会呈现 With your current approach, you're also going to have issues with placing the checker pieces onto the board to begin with. 使用您当前的方法,您也会遇到将检查器件放在板上的问题。
A better approach might be to simply create a series of panels that act as cells and then simply add
the game piece directly to... 更好的方法可能是简单地创建一系列充当单元格的面板,然后直接add
游戏块add
到...
For example 例如
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();
}
}
}
Now, if you're not up to doing cell translations, you could use a two dimensional array instead. 现在,如果您不打算进行单元格转换,则可以使用二维数组。
I would also be tempted to use a single MouseListener
attached to each panel and/or game piece so you could manage the user interactions... 我也很想使用附加到每个面板和/或游戏块的单个MouseListener
,以便您可以管理用户交互......
Why extends JPanel? 为何延伸JPanel? Just create a object that can draw itself: 只需创建一个可以绘制自己的对象:
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);
}
}
Then draw it on some JPanel: 然后在一些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.