我最近在學習Java,我的第一個專案是創建一個 "圍棋棋盤",9*9行和列,并在交叉點上放置黑色和白色的石頭。
我創建了一個9*9行和列的棋盤,現在我必須使用JButton組件來創建黑色和白色的石頭。
除了顏色、大小和按鈕在第一行的位置(setLayout)之外,我無法將按鈕變成一個圓,并將石頭放在交點上。
通過對相關指南的多次搜索,我注意到在創建和設計按鈕時,有一些我不熟悉的獨特結構。
現在我的問題來了--為了制作一個圓形的、大小為65*65、黑色或白色的按鈕,我需要創建什么樣的代碼結構?我需要為此創建一個新的類嗎?我應該如何以及在哪里整合JPanel?
public class Main {
public static void main(String[] args){
Board board = new Board(900, 900, "Go board")。)
}
}
import java.awt.*;
import javax.swing.*;
public class Board extends JPanel{
private int width;
private int height;
private String title;
public int getWidth {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height。
}
public void setHeight(int height) {
this.height = height。
}
public String getTitle() {
return title。
}
public void setTitle(String title) {
this.title = title。
}
public Board(int width, int height, String title) {
super()。
this.width = width;
this.height = height;
this.title=標題。
this.initBoard()。
}
public Board() {
super()。
}
public void initBoard() {
JFrame f = new JFrame(this. getTitle())。)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
//f.getContentPane().setBackground(Color.getHSBColor(25, 75, 47));
f.setSize(this.getWidth(), this.getHeight())。
//f.setLocation(550, 25);/span>
f.add(this, borderLayout.center);
f.setVisible(true)。
JButton stone = new JButton(")。
f.add(stone);
f.setLayout(new FlowLayout())。
stone.setBackground(Color.BLACK.darker())。
stone.setBorder(BorderFactory.createDashedBorder(getForeground()))。
stone.setPreferredSize(new Dimension(65, 65) )。
}
public void paintComponent(Graphics g) {
super.paintComponent(g)。
for (int i = 0; i < 10; i ) {
g.drawLine(0, 10 (i * ((this.getWidth() - 20) / 9)),this.getWidth()。
10 (i * ((this.getWidth() -20) / 9) ))。
g.drawLine(10 (i * ((this.getHeight() -20) / 9)), 0, 10 (i * ((this.getHeight() -20) / 9) )。
this.getHeight())。
}
}
在上傳帖子之前,我閱讀了以下帖子:
在上傳帖子之前,我閱讀了以下帖子。
另外,你可以通過自定義繪制JP面板來制作電路板。這將使各個 "石頭 "不能被點擊,并且更難修改:import java.awt.*; import javax.swing.*; public class GridByPainting extends JPanel { private static final int ROWS = 9, COLS = 9, SIZE = 65, BORDER = 2; private static final Color BOARD_COLOR = Color. BLACK, STONE = Color.YELLOW, WHITE_STONE = Color.WHITE, BLACK_STONE = Color.BLACK; private final 尺寸大小。 public GridByPainting() { int x = BORDER COLS*(SIZE BORDER)。 int y = BORDER ROWS*(SIZE BORDER)。 size = new Dimension(x,y)。 this.initBoard()。 } @Override[/span]. public Dimension getPreferredSize() { return size。 } public void initBoard() { JFrame f = new JFrame("Grid By Painting"/span>)。 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。 f.setLayout(new GridBagLayout()。 f.add(this)。 f.pack(); f.setVisible(true)。 } @Override; } public void paintComponent(Graphics g) { super.paintComponent(g)。 int width = getWidth()。int height = getHeight()。 int stoneWidth = (width - BORDER)/ COLS - BORDER。 int stoneHeight = (高度-BORDER)/ ROWS-BORDER 。 /draw board g.setColor(BOARD_COLOR); g.fillRect(0, 0, width, height); boolean isBlack = true; /draw square stones; /draw square stones. for (int col = 0; col < COLS; col ) { for (int row = 0; row < ROWS; row ) { int x = BORDER col*(stoneWidth BORDER)。 int y = BORDER row*(stoneHeight BORDER)。 g.setColor(STONE); g.fillRect(x, y, stoneWidth, stoneHeight)。 //draw circle[/span]。 g.setColor(isBlack ? BLACK_STONE : WHITE_STONE)。 isBlack = !isBlack; g.fillOval(x, y, stoneWidth, stoneHeight)。 } } } public static void main(String[] args) { SwingUtilities.invokeLater(()->new GridByPainting())。) }uj5u.com熱心網友回復:
我的代碼有一個邏輯模型。 我的代碼有一個繪圖
JP面板。我做的第一件事是創建了一個普通的Java getter / setter類來保存一個圍棋棋盤的邏輯表示。 我將其命名為
Board類。我所做的下一件事是通過呼叫
SwingUtilitiesinvokeLater方法來啟動我的Swing GUI。 該方法確保Swing組件在事件調度執行緒上創建和執行。我使用我的
Runnable類的run方法來創建JFrame。JFrame方法必須按照一個特定的順序被呼叫。 這是我在所有的Swing應用程式中使用的順序。我將
JFrame的創建與任何后續的JPanels的創建分開。 我這樣做是為了使我的代碼有條不紊,易于閱讀,并且易于理解。我擴展了一個
JPanel來創建繪圖JPanel。 我這樣做是為了能夠覆寫JPanel類的paintComponent方法。 繪圖的JPanel會繪制(paint)董事會的狀態。 這就是全部。 沒有別的了。 另一個類將負責向邏輯棋盤添加棋子,并重新繪制Jpanel。MoveListener類實作了一個MouseListener(擴展了一個MouseAdapter)來回應滑鼠在棋盤上的點擊。MoveListener類保持跟蹤誰的回合。 在一個更復雜的圍棋棋盤版本中,你將有另一個普通的 Java getter / setter 類來跟蹤游戲狀態。下面是完整的可運行代碼。 我把所有的類都變成了內部類,這樣我就可以把這段代碼作為一個塊來發布。
import java.awt.BasicStro; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.Jpanel; import javax.swing.SwingUtilities; public class GoBoard implements Runnable { public static void main(String[] args) { SwingUtilities.invokeLater(new GoBoard())。 } private Board板。 private DrawingPanel drawingPanel; public GoBoard(){ this.board = new Board() 。 } @Override public void run() { JFrame frame = new JFrame("Go Board") 。 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。 this.drawingPanel = new DrawingPanel(board)。 frame.add(drawingPanel, BorderLayout.CENTER); frame.pack(); frame.setLocationByPlatform(true)。 frame.setVisible(true)。 } public class DrawingPanel extends JPanel { private static final long serialVersionUID = 1L; private final int margin, pieceRadius, lineSpacing; private Board板。 public DrawingPanel(Board board) { this.board = board; this.margin = 60; this.pieceRadius = 40; this.lineSpacing = 100; this.setBackground(new Color(0x993300) )。) int width = 8 * lineSpacing margin margin。 this.setPreferredSize(new Dimension(width, width))。 this.addMouseListener(new MoveListener(board)) 。 } @Override; } protected void paintComponent(Graphics g) { super.paintComponent(g)。 Graphics2D g2d = (Graphics2D) g。 paintHorizontalLines(g2d); paintVerticalLines(g2d); paintPieces(g2d); } private void paintHorizontalLines(Graphics2D g2d){ int x = margin。 int y1 = margin; int y2 = getHeight() - margin。 g2d.setColor(Color.YELLOW)。 g2d.setStroke(new BasicStroke(3f) 。) for (int index = 0; index < 9; index ) { g2d.drawLine(x, y1, x, y2); x = lineSpacing。 } } private void paintVerticalLines(Graphics2D g2d){ int x1 = margin。 int x2 = getWidth() - margin。 int y = margin。 g2d.setColor(Color.YELLOW)。 g2d.setStroke(new BasicStroke(3f) 。) for (int index = 0; index < 9; index ) { g2d.drawLine(x1, y, x2, y); y = lineSpacing。 } } private void paintPieces(Graphics2D g2d) { int[][] b = board.getBoard()。 for (int row = 0; row < b.length; row ) { for (int column = 0; column < b[row].length; column ) { int x = column * lineSpacing margin。 int y = Row * lineSpacing margin; if (b[row][ column] == 1) { g2d.setColor(Color.BLACK)。 g2d.fillOval(x - pieceRadius, y - pieceRadius, pieceRadius pieceRadius, pieceRadius pieceRadius)。) } else if (b[row][umn] == 2) { g2d.setColor(Color.WHITE)。 g2d.fillOval(x - pieceRadius, y - pieceRadius, pieceRadius pieceRadius, pieceRadius pieceRadius)。) } } } } } public class MoveListener extends MouseAdapter { private boolean isBlackTurn = true; private Board板。 public MoveListener(Board board) { this.board = board; } @Override public void mouseReleased(MouseEvent event) { Point point = event.getPoint()。 int margin = 60; int pieceRadius = 40; int lineSpacing = 100; int column = (point.x - margin pieceRadius)/ lineSpacing。 int row = (point.y - margin pieceRadius)/ lineSpacing; int piece = (isBlackTurn) ? 1 : 2; board.setPiece(piece, row, column); drawingPanel.repaint()。 isBlackTurn = !isBlackTurn; } } public class Board { private int[][] board; public Board() { this.board = new int[9][9] 。 } /**。 * <p> * 這個方法在棋盤上插入一個棋子。 * </p> * * @param 棋子 - 1代表黑色,2代表白色 * @param row - 棋子的行。 @param column - 棋子的列。 */ public void setPiece(int piece, int row, int column) { this.board[row][ column] = piece; } public int[][] getBoard() { return board; } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310284.html標籤:
上一篇:對字串的指定字符進行著色


