[英]How do I change the color of a grid cell in java swing?
我正在為我的一個課程開發最短路徑查找應用程序,作為該項目的一部分,我們需要制作一個非常簡單的 GUI。 它只需要一個 7 橫向和 5 向下的網格、一個指令框、一個下拉菜單和幾個按鈕來調整網格大小和啟動每個算法。 我們決定使用 java swing 構建 GUI。但是當用戶單擊網格上的一個正方形時,我們需要在該網格正方形中着色。 而且我們還沒有找到一種方法來做到這一點。
為了構建網格,我們創建了一個內部 class,它使用嵌套的 for 循環簡單地創建和繪制一個網格。 這部分工作完美。
public static class Grid extends JPanel {
public Grid() {
setSize(400, 400);
setVisible(true);
}
public void paint(Graphics g) {
for (int x = 30; x <= 30*cols; x += 30)
for (int y = 30; y <= 30*rows; y += 30)
g.drawRect(x,y, 30, 30);
}
}
我們試圖為我們想要着色的單元格做一些類似的事情,但它不會出現在我們的 GUI Window 中。我從其他人的問題中看到,你需要將圖形 object 轉換為 2D 圖形 object,當我運行一些在接受鼠標輸入之外的測試代碼有效。 但是當我嘗試使用我創建的方法在網格單元格中着色時,它沒有出現。 這是我創建的用於繪制和填充單元格的內部 class 單元格。
public static class Cell extends JPanel {
int x;
int y;
Color color;
public Cell(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
setVisible(true);
}
public void paintComponent(Graphics g) {
System.out.println("here");
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(color);
g2d.drawRect(x,y, 30, 30);
g2d.setColor(color);
g2d.fillRect(x,y,30,30);
}
}
我是否需要以某種方式訪問我們已經在網格中繪制的單元格而不是嘗試在網格頂部繪制? 我怎么能那樣做? 我什至可以在網格上畫畫嗎? 我們的鼠標事件是否阻止了它的出現? 我們是否應該每次都重新繪制一個帶有顏色的單元格的網格?
我還創建了這個方法,一旦用戶單擊我們的網格我就會調用它。 這是我們還沒有真正弄清楚的部分。
//input: the cell's id number and the color we want the cell to be
public Cell colorCell(int cell, Color color){
int gridTopX = 33; //this is how our grid is oriented in the window
int gridTopY = 33; //this is how our grid is oriented in the window
int cellX;
int cellY;
//this finds the row and colum of the cell (to find top left corner to draw it)
int counter = 0; //row
int holder = cell; //column
while(holder > 7){
holder -= 7; //subtract 7
counter++; //increment counter
}
//once that is done we have the row and column -1 each
holder++;
counter++;
cellX = 33 + (30 *holder);
cellY = 33 + (30*counter);
//create the cell
Cell cellToColor = new Cell(cellX,cellY, color);
return cellToColor;
}
我想如果我用這個方法創建一個單元格,它就可以在網格頂部的單元格中繪制和着色。 但是單元格的創建永遠不會進入單元格 class 的 paintComponent(Graphics g) 方法。我不確定如何調用此方法,因為我不相信有一種方法可以創建圖形 object。其他當我從單元格 class 創建一個單元格時,它已被自動調用。 不能從另一個方法中調用它嗎? 任何幫助或建議將不勝感激。
您可以放棄自定義繪畫工作流程,只使用JPanel
中已有的功能
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new GridPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GridPane extends JPanel {
public GridPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
for (int row = 0; row < 5; row++) {
gbc.gridy = row;
for (int col = 0; col < 7; col++) {
gbc.gridx = col;
add(new CellPane(), gbc);
}
}
}
}
public class CellPane extends JPanel {
private boolean selected;
public CellPane() {
setBorder(new LineBorder(Color.DARK_GRAY));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
toggleSelection();
}
});
}
public void setSelected(boolean selected) {
this.selected = selected;
if (selected) {
setBackground(Color.BLUE);
setBorder(new LineBorder(Color.WHITE));
} else {
setBackground(null);
setBorder(new LineBorder(Color.DARK_GRAY));
}
repaint();
}
public void toggleSelection() {
setSelected(!isSelected());
}
public boolean isSelected() {
return selected;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(30, 30);
}
}
}
當然,如果你想遵循自定義繪畫流程,你可以看看:
作為一些想法
請記住,model 為王。 cell的state應該是基於model的state
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.