自己编了一个五子棋游戏,用JPanel绘制了棋子,但是在最小化或者有其他对话框覆盖棋子后,棋子就消失了!
请各位帮忙!谢谢!代码如下import javax.swing.*;
import java.awt.*;
import java.awt.event.*; public class Go extends JPanel implements MouseListener{
static int array[][] = new int[15][15];
static int counter=0;
public static int judge;
JFrame f = new JFrame("Gobang Game");
JTextField taHelp = new JTextField();
public Go(){ for(int i=0; i<15; i++){
for(int j=0; j<15; j++){
array[i][j]=0;
}
}
taHelp.setText("Welcome.");
taHelp.setEditable(false);
this.addMouseListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(330,380);
f.setResizable(false);
f.getContentPane().add(this);
f.getContentPane().add(taHelp, BorderLayout.SOUTH);
f.setVisible(true);
}
public static void main(String[]args) {
new Go();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(int i=0;i <15; i++)
{
g.drawLine(20,(i+1)*20,300,(i+1)*20);
g.drawLine((i+1)*20,20,(i+1)*20,300);
}
}
public void drawBlack(int x,int y){
Graphics g1= this.getGraphics();
g1.setColor(Color.BLACK);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
//g1.dispose();
} public void drawWhite(int x,int y){
Graphics g1= this.getGraphics();
g1.setColor(Color.YELLOW);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
//g1.dispose();
}
public int checkHorizontal(){
int result=1;
for(int i=0; i<15; i++){
for(int j=2; j<13; j++){
if(array[i][j]==1 && array[i][j-1]==1 && array[i][j-2]==1 && array[i][j+1]==1 && array[i][j+2]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i][j-1]==2 && array[i][j-2]==2 && array[i][j+1]==2 && array[i][j+2]==2){
result = 1;
break;
}
else{
result = 0;
}
}
if(result ==1 )
break;
}
return result;
}
public int checkVertical(){
int result=1;
for(int i=2; i<13; i++){
for(int j=0; j<15; j++){
if(array[i][j]==1 && array[i-1][j]==1 && array[i-2][j]==1 && array[i+1][j]==1 && array[i+2][j]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i-1][j]==2 && array[i-2][j]==2 && array[i+1][j]==2 && array[i+2][j]==2){
result = 1;
break;
}
else{
result = 0;
}
}
if(result ==1 )
break;
}
return result;
}
public int checkOblique(){
int result=1;
for(int i=2; i<13; i++){
for(int j=2; j<13; j++){
if(array[i][j]==1 && array[i-1][j-1]==1 && array[i-2][j-2]==1 && array[i+1][j+1]==1 && array[i+2][j+2]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i-1][j-1]==2 && array[i-2][j-2]==2 && array[i+1][j+1]==2 && array[i+2][j+2]==2){
result = 1;
break;
}
else if(array[i][j]==1 && array[i-1][j+1]==1 && array[i-2][j+2]==1 && array[i+1][j-1]==1 && array[i+2][j-2]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i-1][j+1]==2 && array[i-2][j+2]==2 && array[i+1][j-1]==2 && array[i+2][j-2]==2){
result = 1;
break;
}
else{
result = 0;
}
}
if(result == 1 )
break;
}
return result;
}
public void mouseClicked(MouseEvent e){
int x=(e.getX()+10)/20;
int y=(e.getY()+10)/20;
if(x>0 && x<16 && y>0 && y<16){
if(array[y-1][x-1]==0){
counter++;
String abc = Integer.toString(counter);
taHelp.setText("This is the No."+abc+" step.");
/*System.out.println(counter);
System.out.println(x);
System.out.println(y);*/
if(counter %2 ==1){
drawBlack(x*20, y*20);
array[y-1][x-1]=1; //数组的前面一项表示行数,后面是列数。
}
else{
drawWhite(x*20, y*20);
array[y-1][x-1]=2;
}
int horizontal = checkHorizontal();
int vertical = checkVertical();
int oblique = checkOblique();
if (horizontal==1 || vertical==1 || oblique==1){
if(counter %2 ==1)
JOptionPane.showMessageDialog(null, "Black wins. ", "Congratulations!",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "White wins. ", "Congratulations!",JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "A chess already existed.", "Warning",JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "Out of bounds.", "Warning",JOptionPane.ERROR_MESSAGE);
} }
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
请各位帮忙!谢谢!代码如下import javax.swing.*;
import java.awt.*;
import java.awt.event.*; public class Go extends JPanel implements MouseListener{
static int array[][] = new int[15][15];
static int counter=0;
public static int judge;
JFrame f = new JFrame("Gobang Game");
JTextField taHelp = new JTextField();
public Go(){ for(int i=0; i<15; i++){
for(int j=0; j<15; j++){
array[i][j]=0;
}
}
taHelp.setText("Welcome.");
taHelp.setEditable(false);
this.addMouseListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(330,380);
f.setResizable(false);
f.getContentPane().add(this);
f.getContentPane().add(taHelp, BorderLayout.SOUTH);
f.setVisible(true);
}
public static void main(String[]args) {
new Go();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(int i=0;i <15; i++)
{
g.drawLine(20,(i+1)*20,300,(i+1)*20);
g.drawLine((i+1)*20,20,(i+1)*20,300);
}
}
public void drawBlack(int x,int y){
Graphics g1= this.getGraphics();
g1.setColor(Color.BLACK);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
//g1.dispose();
} public void drawWhite(int x,int y){
Graphics g1= this.getGraphics();
g1.setColor(Color.YELLOW);
g1.drawOval(x-5,y-5,10,10);
g1.fillOval(x-5,y-5,10,10);
//g1.dispose();
}
public int checkHorizontal(){
int result=1;
for(int i=0; i<15; i++){
for(int j=2; j<13; j++){
if(array[i][j]==1 && array[i][j-1]==1 && array[i][j-2]==1 && array[i][j+1]==1 && array[i][j+2]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i][j-1]==2 && array[i][j-2]==2 && array[i][j+1]==2 && array[i][j+2]==2){
result = 1;
break;
}
else{
result = 0;
}
}
if(result ==1 )
break;
}
return result;
}
public int checkVertical(){
int result=1;
for(int i=2; i<13; i++){
for(int j=0; j<15; j++){
if(array[i][j]==1 && array[i-1][j]==1 && array[i-2][j]==1 && array[i+1][j]==1 && array[i+2][j]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i-1][j]==2 && array[i-2][j]==2 && array[i+1][j]==2 && array[i+2][j]==2){
result = 1;
break;
}
else{
result = 0;
}
}
if(result ==1 )
break;
}
return result;
}
public int checkOblique(){
int result=1;
for(int i=2; i<13; i++){
for(int j=2; j<13; j++){
if(array[i][j]==1 && array[i-1][j-1]==1 && array[i-2][j-2]==1 && array[i+1][j+1]==1 && array[i+2][j+2]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i-1][j-1]==2 && array[i-2][j-2]==2 && array[i+1][j+1]==2 && array[i+2][j+2]==2){
result = 1;
break;
}
else if(array[i][j]==1 && array[i-1][j+1]==1 && array[i-2][j+2]==1 && array[i+1][j-1]==1 && array[i+2][j-2]==1){
result = 1;
break;
}
else if(array[i][j]==2 && array[i-1][j+1]==2 && array[i-2][j+2]==2 && array[i+1][j-1]==2 && array[i+2][j-2]==2){
result = 1;
break;
}
else{
result = 0;
}
}
if(result == 1 )
break;
}
return result;
}
public void mouseClicked(MouseEvent e){
int x=(e.getX()+10)/20;
int y=(e.getY()+10)/20;
if(x>0 && x<16 && y>0 && y<16){
if(array[y-1][x-1]==0){
counter++;
String abc = Integer.toString(counter);
taHelp.setText("This is the No."+abc+" step.");
/*System.out.println(counter);
System.out.println(x);
System.out.println(y);*/
if(counter %2 ==1){
drawBlack(x*20, y*20);
array[y-1][x-1]=1; //数组的前面一项表示行数,后面是列数。
}
else{
drawWhite(x*20, y*20);
array[y-1][x-1]=2;
}
int horizontal = checkHorizontal();
int vertical = checkVertical();
int oblique = checkOblique();
if (horizontal==1 || vertical==1 || oblique==1){
if(counter %2 ==1)
JOptionPane.showMessageDialog(null, "Black wins. ", "Congratulations!",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "White wins. ", "Congratulations!",JOptionPane.INFORMATION_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "A chess already existed.", "Warning",JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null, "Out of bounds.", "Warning",JOptionPane.ERROR_MESSAGE);
} }
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
paintComponent()是程序运行只调用一次吗?
这样你的按钮就能一直显示了