Java Applet重绘的麻烦

时间:2015-05-03 19:51:01

标签: java swing applet repaint

以下程序应该将AlertDialog.Builder字符串放在java applet上的tic tac toe board中,以便它们在连续放置后保留。但是,当用户点击放置X时,X将消失并在其他位置绘制。我怎样才能克服这个问题?

X

1 个答案:

答案 0 :(得分:2)

hif(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这样的单独组件,我可以轻松地将其添加到JFrameJPanel

我在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}}