[英]Java Swing questions regarding GridLayout and paintComponent methods
在這個程序中,我們需要創建一個8x8網格的“LifeCell”小部件。 教師沒有提到小部件必須是Shape
的對象,所以我繼續使用GridLayout
類。 GridLayout
類工作正常(因為我知道,因為沒有可視化幫助來確認。)該程序的目標是玩生命游戲,用戶可以點擊其中一個LifeCell小部件並在狀態之間切換'活着'或'死了。
我的問題很大程度上依賴於讓細胞被塗上。 這可能是我的代碼的問題,但我不是100%肯定。
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);
}
}
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);
}
我不需要確切的解決方案來修復它,但我最終試圖讓它工作。
謝謝。
編輯:
我添加了更多的Program2.java類,我明天可以回來看看我要去睡覺了,我感謝所有的幫助。
編輯#2:
當我用8x8 GridLayout
填充我的框架時,我真正的困惑是每個單獨的“單元格”缺少更好的單詞是LifeCell
類型。 我怎樣才能為每個LifeCell
塗上不同的顏色? 如果這對你們有任何意義,我可以嘗試盡可能地修改它。 而且,我會看那個網站,謝謝你。
JPanel沒有默認的首選大小或可見內容。 你需要添加一些可見的組件(例如JLabel)或給它一個首選的大小。
除此之外,如果你按如下方式設置它,你的布局應該有效:
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
cp.add(new JLabel(i + "-" + j));
frame.pack();
frame.setVisible(true);
為什么你的LifeCell甚至有一個paintComponent()方法? 沒有必要做自定義繪畫。 您可以使用以下命令更改任何組件的背景顏色:
setBackground( Color.BLUE )
除此之外,你的問題對我沒有意義。 首先你聲明你需要使用一個Shape對象,但我沒有在你的代碼中的任何地方看到一個Shape對象,那你為什么要提這個問題呢?
我真的不明白你的問題,我們沒有足夠的代碼來提供任何真正的建議。
如果您需要更多幫助,請在SSCCE上發布問題。
你走在正確的軌道上。
如果你想使用現有的組件(例如JPanel,JLabel,JButton等),你可以更好地尊重組件已經完成的工作,並且只需要參數化所需的內容。
因此,在您使用JPanel的情況下,此(以及其他JComponents)具有可以更改的background
屬性。 因此,不要試圖將組件繪制成你自己(這就是現在失敗的組件),而只需設置該值並讓油漆自己繪制。
你可以添加一個“getLifeColor”,它根據單元格狀態返回不同的顏色:
private Color getLifeColor() {
return this.alive?liveColor:deadColor;
}
然后讓細胞用這種顏色繪制背景:
public void paintComponent(Graphics gr) {
setBackground( getLifeColor() );
super.paintComponent( gr );
}
之后,您只需將單元格的狀態設置為活動或死亡,組件將以相應的顏色顯示:
這是您發布的代碼的簡短自包含正確示例(SSCCE)+實時/死色使用情況。 我想你可以從那里繼續。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.