如何使Awt在单击的矩形上绘制网格?
我是制作TIC TAC脚趾游戏的学生,目前拥有大部分UI和网格本身。我知道AWT没有数据,并且挥杆更有用和有用,但是我正在被教导,并且在AWT上拥有最多的资源。我目前有5个类:
GUI类:
import java.awt.*;
import javax.swing.JFrame;
public class GUI{
JFrame frame1;
Board board1;
Buttons buttons;
MyMenu menu1;
public GUI() {
frame1 = new JFrame("Tic Tac Toe");
board1 = new Board(3,3 ,200);
buttons = new Buttons(this);
menu1 = new MyMenu(this);
}
public void start () {
frame1.setLayout(new FlowLayout());
board1.setBackground(Color.BLACK);
frame1.setMenuBar(menu1);
frame1.add(board1);
frame1.add(buttons);
frame1.pack();
frame1.setVisible(true);
}
public static void main (String[] args) {
GUI gui = new GUI();
gui.start();
}
public void setBackground (Color c) {
board1.setBackground(c);
}
public void closeGame() {
frame1.dispose();
}
}
董事会类:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.*;
public class Board extends Canvas {
Cell[][] array;
int rows, cols;
public Board(int rows, int cols, int size) {
this.rows = rows;
this.cols = cols;
array = new Cell[rows][cols];
for (int r = 0; r < rows; r++) {
int y = r * size;
for (int c = 0; c < cols; c++) {
int x = c * size;
array[r][c] = new Cell(x, y, size);
}
}
setSize(cols*size,rows*size);
}
public int getNumRows() {
return rows;
}
public int getNumCols () {
return cols;
}
public void draw(Graphics g) {
for (Cell[] row : array) {
for (Cell cell : row) {
cell.paint(g);
}
}
}
public void paint(Graphics g) {
draw(g);
}
}
单元类:
import java.awt.Color;
import java.awt.*;
public class Cell
{
private final int x;
private final int y;
private final int size;
int rows;
int cols;
Cell[][] array = new Cell[rows][cols];
public Cell(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}
public void fillArray() {
for (int r = 0; r < rows; r++) {
int y = r * size;
for (int c = 0; c < cols; c++) {
int x = c * size;
array[r][c] = new Cell(x, y, size);
}
}
}
public void paint(Graphics g) {
g.setColor(Color.YELLOW);
g.drawRect(x, y, size, size);
}
}
按钮类:
import java.awt.*;
import java.awt.event.*;
public class Buttons extends Panel implements ActionListener {
Button test;
Button giveIn;
GUI gui;
public Buttons(GUI gui) {
this.gui = gui;
giveIn = new Button("GIVE UP!");
this.add(giveIn);
giveIn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== giveIn) {
gui.closeGame();
}
}
}
我还有另一个类,实现了菜单
,但没有觉得有必要包括。我将如何实施和如何实施它,因此当我在网格上单击矩形时,将某物涂在特定的矩形上?我知道我需要一个鼠标侦听器,以及在我的油漆方法上进行某种语句,以及一些返回单元格或矩形单击的方法。这样的事情如何实现?
I am a student in the process of making a Tic Tac Toe game, and currently have most of the UI and grid itself. I know AWT is out of data and Swing is much more informative and useful, but I am being taught and have the most resources on AWT. I currently have 5 classes:
GUI Class:
import java.awt.*;
import javax.swing.JFrame;
public class GUI{
JFrame frame1;
Board board1;
Buttons buttons;
MyMenu menu1;
public GUI() {
frame1 = new JFrame("Tic Tac Toe");
board1 = new Board(3,3 ,200);
buttons = new Buttons(this);
menu1 = new MyMenu(this);
}
public void start () {
frame1.setLayout(new FlowLayout());
board1.setBackground(Color.BLACK);
frame1.setMenuBar(menu1);
frame1.add(board1);
frame1.add(buttons);
frame1.pack();
frame1.setVisible(true);
}
public static void main (String[] args) {
GUI gui = new GUI();
gui.start();
}
public void setBackground (Color c) {
board1.setBackground(c);
}
public void closeGame() {
frame1.dispose();
}
}
Board Class:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.*;
public class Board extends Canvas {
Cell[][] array;
int rows, cols;
public Board(int rows, int cols, int size) {
this.rows = rows;
this.cols = cols;
array = new Cell[rows][cols];
for (int r = 0; r < rows; r++) {
int y = r * size;
for (int c = 0; c < cols; c++) {
int x = c * size;
array[r][c] = new Cell(x, y, size);
}
}
setSize(cols*size,rows*size);
}
public int getNumRows() {
return rows;
}
public int getNumCols () {
return cols;
}
public void draw(Graphics g) {
for (Cell[] row : array) {
for (Cell cell : row) {
cell.paint(g);
}
}
}
public void paint(Graphics g) {
draw(g);
}
}
Cell Class:
import java.awt.Color;
import java.awt.*;
public class Cell
{
private final int x;
private final int y;
private final int size;
int rows;
int cols;
Cell[][] array = new Cell[rows][cols];
public Cell(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
}
public void fillArray() {
for (int r = 0; r < rows; r++) {
int y = r * size;
for (int c = 0; c < cols; c++) {
int x = c * size;
array[r][c] = new Cell(x, y, size);
}
}
}
public void paint(Graphics g) {
g.setColor(Color.YELLOW);
g.drawRect(x, y, size, size);
}
}
Button Class:
import java.awt.*;
import java.awt.event.*;
public class Buttons extends Panel implements ActionListener {
Button test;
Button giveIn;
GUI gui;
public Buttons(GUI gui) {
this.gui = gui;
giveIn = new Button("GIVE UP!");
this.add(giveIn);
giveIn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== giveIn) {
gui.closeGame();
}
}
}
I also have another class, implementing a Menu
, but didn't feel it was necessary to include. How hard would and how would I implement it so when I click a rectangle on my grid, something is painted onto that specific rectangle? I understand I would need a mouse listener, and some sort of if statement on my paint method, as well as some methods that return the cell or rectangle clicked. How is something like this implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论