关于GridLayout和paintComponent方法的用户界面Java Swing问题
在这个程序中,我们需要创建一个8x8网格的“LifeCell”小部件。讲师没有提到小部件必须是Shape
的对象,所以我继续使用GridLayout
类。{
我的问题在很大程度上取决于让细胞上漆。我的代码可能有问题,但我不是100%确定
节目2。java
public class Program2 extends JPanel implements ActionListener {
private LifeCell[][] board; // Board of life cells.
private JButton next; // Press for next generation.
private JFrame frame; // The program frame.
public Program2() {
// The usual boilerplate constructor that pastes the main
// panel into a frame and displays the frame. It should
// invoke the "init" method before packing the frame
frame = new JFrame("LIFECELL!");
frame.setContentPane(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.init();
frame.pack();
frame.setVisible(true);
}
public void init() {
// Create the user interface on the main panel. Construct
// the LifeCell widgets, add them to the panel, and store
// them in the two-dimensional array "board". Create the
// "next" button that will show the next generation.
LifeCell[][] board = new LifeCell[8][8];
this.setPreferredSize(new Dimension(600, 600));
this.setBackground(Color.white);
this.setLayout(new GridLayout(8, 8));
// here is where I initialize the LifeCell widgets
for (int u = 0; u < 8; u++) {
for (int r = 0; r < 8; r++) {
board[u][r] = new LifeCell(board, u, r);
this.add(board[u][r]);
this.setVisible(true);
}
}
生命细胞。java
public class LifeCell extends JPanel implements MouseListener {
private LifeCell[][] board; // A reference to the board array.
private boolean alive; // Stores the state of the cell.
private int row, col; // Position of the cell on the board.
private int count; // Stores number of living neighbors.
public LifeCell(LifeCell[][] b, int r, int c) {
// Initialize the life cell as dead. Store the reference
// to the board array and the board position passed as
// arguments. Initialize the neighbor count to zero.
// Register the cell as listener to its own mouse events.
this.board = b;
this.row = r;
this.col = c;
this.alive = false;
this.count = 0;
addMouseListener(this);
}
这里是paintComponent
方法:
public void paintComponent(Graphics gr) {
// Paint the cell. The cell must be painted differently
// when alive than when dead, so the user can clearly see
// the state of the cell.
Graphics2D g = (Graphics2D) gr;
super.paintComponent(gr);
g.setPaint(Color.BLUE);
}
我不需要精确的解决方案来修复它,但我在试图让它发挥作用时束手无策
谢谢
编辑:
我增加了更多的程序片段2。java课,我明天可以回来我要睡觉了,我感谢所有的帮助
编辑#2:
当我用一个8x8 GridLayout
填充帧时,我真正的困惑在于,由于缺少更好的单词,每个单独的“单元”都是LifeCell
类型。我怎样才能把每个LifeCell
画成不同的颜色?如果这对你们有任何意义的话,我可以尽量修改它。还有camickr,我会看看那个网站,谢谢
作业可以在here中找到,以避免关于我的问题和/或代码片段的任何和所有混淆
# 1 楼答案
为什么你的LifeCell有一个paintComponent()方法?没有必要做定制绘画。可以使用以下命令更改任何组件的背景色:
除此之外,你的问题对我来说毫无意义。首先,你声明你需要使用一个Shape对象,但是我在你的代码中没有看到Shape对象,那么为什么你提到它会混淆这个问题呢
我真的不明白你的问题,我们没有足够的代码来提供任何真正的建议
如果您需要更多帮助,请发布SSCCE以显示问题
# 2 楼答案
你说得对
如果你想使用一个现有的组件(比如JPanel、JLabel、JButton等),最好尊重组件已经做过的事情,并将所需的参数化
因此,在您使用JPanel的情况下,这个(和其他JComponents)有一个
background
属性,您可以更改它。因此,与其尝试自己绘制组件(这是目前失败的),不如设置该值,让绘制自己绘制您可以添加“getLifeColor”,根据单元格状态返回不同的颜色:
然后让细胞用这种颜色绘制背景:
之后,您只需将单元格的状态设置为“生存”或“死亡”,组件将以相应的颜色显示:
Here's你发布的代码的简短自包含正确示例(SSCCE)以及活/死颜色用法。我想你可以从那里继续