以下程序应该将AlertDialog.Builder
字符串放在java applet上的tic tac toe board中,以便它们在连续放置后保留。但是,当用户点击放置X
时,X
将消失并在其他位置绘制。我怎样才能克服这个问题?
X
答案 0 :(得分:2)
h
和if(board[i][r] == true) g.drawString("X",j ,h );
包含最后一次鼠标点击的位置。你在这里所做的是错误的
X
因为您试图在同一位置绘制所有X
。
因此,要解决您的问题,我们需要存储已绘制的Position
的位置。我们需要创建一个class Position{
private int x;
private int y;
public Position(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
添加此课程
TicTacToe
然后在包含所有位置的private Position[][] positions = new Position[3][3];
类中添加新属性
mouseClicked
最后,这是public void mouseClicked(MouseEvent e){
int x = e.getX(); int y = e.getY();
j=x/50;
h=y/50;
board[j][h] = true;
if(positions[j][h]==null){
positions[j][h]=new Position(x,y);
}
positions[j][h].setX(x);
positions[j][h].setY(y);
repaint();
}
方法
paint
public void paint(Graphics g)
{
// background color
g.setColor(Color.white);
g.fillRect(0, 0, 150, 150);
//draw board
g.setColor(Color.BLACK);
//draw vertical line
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(i,0,i,150);
}
//draw horizontal lines
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(0,i,150,i);
}
//paint x values when applicable
g.setColor(Color.RED);
for(int i = 0; i < 3; i ++){
for(int r = 0; r < 3; r ++){
if(board[i][r] == true) g.drawString
("X",positions[i][r].getX() ,positions[i][r].getY() );
}
}
}
方法
JApplet
现在您的代码应该根据您的需要工作。但是,大多数时候,我真的不想将所有工作委托给一个单独的班级JPanel
。相反,我更喜欢将逻辑分成像JApplet
这样的单独组件,我可以轻松地将其添加到JFrame
或JPanel
。
我在JFrame
中使用package example;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
class Position{
private int x;
private int y;
public Position(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
public class NewTicTacToe {
public static void main(String[] args) {
new NewTicTacToe();
}
public NewTicTacToe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("NewTicTacToe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PaintTicTacToe());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintTicTacToe extends JPanel {
// instance variables - replace the example below with your own
private boolean[][] board = new boolean[3][3];
private Position[][] positions = new Position[3][3];
int j; int h;
public PaintTicTacToe() {
super();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX(); int y = e.getY();
j=x/50;
h=y/50;
board[j][h] = true;
if(positions[j][h]==null){
positions[j][h]=new Position(x,y);
}
positions[j][h].setX(x);
positions[j][h].setY(y);
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
// background color
g.setColor(Color.white);
g.fillRect(0, 0, 150, 150);
//draw board
g.setColor(Color.BLACK);
//draw vertical line
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(i,0,i,150);
}
//draw horizontal lines
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(0,i,150,i);
}
//paint x values when applicable
g.setColor(Color.RED);
for(int i = 0; i < 3; i ++){
for(int r = 0; r < 3; r ++){
if(board[i][r] == true) g.drawString
("X",positions[i][r].getX() ,positions[i][r].getY() );
}
}
}
}
}
来获取您需要的最干净的代码。
{{1}}