[英]Draw things after board is drawn with paint (JPanel) (java)
我試圖在Java中制作一個非常簡單的Tic Tac Toe游戲。 但是,在我畫完電路板之后,我無法弄清楚如何繪制東西。 這是我的游戲結構。
public class ThreeInARowMain extends JPanel {
public static final int WIDTH = 600, HEIGHT = 640;
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.black);
g.drawString("1.", 0,20);
g.drawString("2.", 210,20);
g.drawString("3.", 410,20);
g.drawString("4.", 0,220);
g.drawString("5.", 210,220);
g.drawString("6.", 410,220);
g.drawString("7.", 0,420);
g.drawString("8.", 210,420);
g.drawString("9.", 410,420);
//Horizontal Lines
g.drawLine(0, 200, WIDTH, 200);
g.drawLine(0, 400, WIDTH, 400);
//Vertical Lines
g.drawLine(200, 0, 200, HEIGHT);
g.drawLine(400, 0, 400, HEIGHT);
}
public static void main (String [] args) {
boolean firstorsecond = true;
JFrame frame = new JFrame("Tic Tac Toe");
frame.setSize(WIDTH, HEIGHT);
frame.getContentPane().add(new ThreeInARowMain());
frame.setLocationRelativeTo(null);
frame.setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
boolean someonewins = false;
int firstplayerpos, secondplayerpos;
int [] [] board = new int [3] [3];
Scanner in = new Scanner(System.in);
while (!someonewins){
System.out.println("P1, enter the number of the square you'd like to mark.");
firstplayerpos = in.nextInt();
DrawXorO(firstplayerpos,firstorsecond);
System.out.println("P2, enter the number of the square you'd like to mark.");
secondplayerpos = in.nextInt();
DrawXorO(secondplayerpos,!firstorsecond);
}
}
public static void DrawXorO (int position, boolean firstorsecond) {
}
}
我知道這是一款設計糟糕的游戲; 我稍后會糾正。 但是,我想要的DrawXorO是在玩家輸入的位置繪制X或O.如何在使用方法繪畫后添加圖形? 謝謝!
Scanner
)與UI混合使用。 UI是事件驅動的,也就是說,事情發生了,你回應它。 paint
,而不是覆蓋paintComponent
並確保調用super.paintComponent
我會重新設計你的GUI以使用9個JButtons
,每個方塊1個。 然后,您可以調用JButton方法,例如JButton.setText("X")
來制作方形顯示X,這將更加簡單。 如果你真的想要覆蓋paintComponent,你可以創建變量來存儲每個方塊當前顯示的內容,更新這些變量並在需要更新時調用repaint()。
你的意思是這樣的嗎?
這是在GUI左側繪制tic tac toe圖的一種方法。 我有一個模型類,它將電路板位置保持在一個二維int數組中,還有一個鼠標控制器類讓我知道這個人點擊了tic tac toe圖的位置。
package com.ggl.tictactoe.view;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import com.ggl.tictactoe.controller.MouseMoveController;
import com.ggl.tictactoe.model.GameStatus;
import com.ggl.tictactoe.model.TicTacToeModel;
public class TicTacToePanel extends JPanel {
private static final long serialVersionUID = -9150412950439480699L;
private MouseMoveController controller;
private TicTacToeModel model;
public TicTacToePanel(TicTacToeFrame frame, TicTacToeModel model) {
this.model = model;
this.controller = new MouseMoveController(frame, model);
this.addMouseListener(controller);
this.setPreferredSize(new Dimension(360, 360));
}
public void setComputerMove() {
if (!model.isPlayerGoesFirst()) {
controller.setComputerMove();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());
int[][] board = model.getBoard();
int width = getWidth() / board.length;
int height = getHeight() / board[0].length;
int x = 0;
int y = 0;
Stroke stroke = new BasicStroke(7F);
g2d.setStroke(stroke);
g2d.setColor(Color.BLACK);
x += width;
for (int i = 0; i < (board.length - 1); i++) {
g2d.drawLine(x, y, x, getHeight());
x += width;
}
x = 0;
y += width;
for (int i = 0; i < (board.length - 1); i++) {
g2d.drawLine(x, y, getWidth(), y);
y += width;
}
float fontSize = (float) height * 72F / 96F;
Font largeFont = getFont().deriveFont(fontSize);
for (int i = 0; i < board.length; i++) {
x = width * i;
for (int j = 0; j < board[i].length; j++) {
y = height * j;
Rectangle r = new Rectangle(x, y, width, height);
if (board[i][j] == 1) {
g2d.setColor(Color.BLUE);
centerString(g2d, r, "X", largeFont);
} else if (board[i][j] == -1) {
g2d.setColor(Color.CYAN);
centerString(g2d, r, "O", largeFont);
}
}
}
if (model.getGameStatus() == GameStatus.TIE_GAME) {
BufferedImage image = GameOverImage.createImage(getWidth(),
getHeight(), "Tie Game");
g2d.drawImage(image, 0, 0, this);
} else if (model.getGameStatus() == GameStatus.COMPUTER_WINS) {
BufferedImage image = GameOverImage.createImage(getWidth(),
getHeight(), "Computer Wins");
g2d.drawImage(image, 0, 0, this);
} else if (model.getGameStatus() == GameStatus.PLAYER_WINS) {
BufferedImage image = GameOverImage.createImage(getWidth(),
getHeight(), "Player Wins");
g2d.drawImage(image, 0, 0, this);
}
}
/**
* This method centers a <code>String</code> in a bounding
* <code>Rectangle</code>.
*
* @param g2d
* - The <code>Graphics2D</code> instance.
* @param r
* - The bounding <code>Rectangle</code>.
* @param s
* - The <code>String</code> to center in the bounding rectangle.
* @param font
* - The display font of the <code>String</code>
*
* @see java.awt.Graphics
* @see java.awt.Rectangle
* @see java.lang.String
*/
private void centerString(Graphics2D g2d, Rectangle r, String s, Font font) {
FontRenderContext frc = new FontRenderContext(null, true, true);
Rectangle2D r2D = font.getStringBounds(s, frc);
int rWidth = (int) Math.round(r2D.getWidth());
int rHeight = (int) Math.round(r2D.getHeight());
int rX = (int) Math.round(r2D.getX());
int rY = (int) Math.round(r2D.getY());
int a = (r.width / 2) - (rWidth / 2) - rX;
int b = (r.height / 2) - (rHeight / 2) - rY;
g2d.setFont(font);
g2d.drawString(s, r.x + a, r.y + b);
}
}
這是鼠標控制器代碼。
package com.ggl.tictactoe.controller;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import com.ggl.tictactoe.model.GameStatus;
import com.ggl.tictactoe.model.TicTacToeModel;
import com.ggl.tictactoe.view.TicTacToeFrame;
public class MouseMoveController extends MouseAdapter {
private ComputerMoveRunnable controller;
private TicTacToeFrame frame;
private TicTacToeModel model;
public MouseMoveController(TicTacToeFrame frame, TicTacToeModel model) {
this.frame = frame;
this.model = model;
this.controller = new ComputerMoveRunnable(frame, model);
}
@Override
public void mousePressed(MouseEvent event) {
if ((model.getGameStatus() == GameStatus.ACTIVE_GAME)
&& (event.getButton() == MouseEvent.BUTTON1)) {
int width = frame.getTicTacToePanel().getWidth();
int height = frame.getTicTacToePanel().getHeight();
int positionWidth = TicTacToeModel.getPositionWidth();
Point p = event.getPoint();
int x = getPosition(p.x, width, positionWidth);
int y = getPosition(p.y, height, positionWidth);
int[][] board = model.getBoard();
if (board[x][y] == 0) {
model.setPlayerMove(x, y);
setComputerMove();
}
}
}
public void setComputerMove() {
controller.run();
frame.repaintTicTacToePanel();
}
private int getPosition(int location, int dimension, int squareWidth) {
for (int i = 0; i < squareWidth; i++) {
if (location < (dimension * (i + 1) / squareWidth)) {
return i;
}
}
return squareWidth - 1;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.