1)国际象棋棋盘
package zh;
import java.awt.*;
import javax.swing.*;
public class Chessboard extends JApplet{
int baseXPosition,baseYPosition;
int currentXPosition,currentYPosition;
public void init(){
baseXPosition=40;
baseYPosition=40;
setBackground(Color.BLACK);
}
public void paint(Graphics g){
currentXPosition=baseXPosition;
currentYPosition=baseYPosition;
for(int row=0;row<8;row++){
currentXPosition=baseXPosition+row*40;
for(int column=0;column<8;column++){
if((column+row)%2==0)
g.setColor(Color.WHITE);
else
g.setColor(Color.DARK_GRAY);
currentYPosition=baseYPosition+column*40;
            g.drawRect(currentXPosition, currentYPosition,  40, 40);
            g.fillRect(currentXPosition, currentYPosition,  40, 40);               
}
}
g.setColor(Color.BLACK);
g.drawRect(40, 40, 320, 320);
}
}
2)中国象棋棋盘
package  zh;
import java.applet.*;
import java.awt.*;
public class Chess1 extends Applet 
{
public void paint(Graphics h)
{
for(int i = 0;i <=9;i ++)
{
h.drawLine(60, 60 *(i + 1), 540, 60*(i + 1)); 
}
for(int j = 0;j <=8;j ++)
{
h.drawLine(60*(j + 1), 60,60*(j + 1) , 300);
h.drawLine(60*(j + 1), 360,60*(j + 1) , 600);
}
 
h.drawLine(60 , 300, 60 , 360);
h.drawLine(540, 300, 540, 360);    
h.drawLine(240, 60 , 360, 180);     
h.drawLine(240, 180, 360, 60 ) ;
h.drawLine(240, 600, 360, 480);
h.drawLine(240, 480, 360, 600);
h.drawLine(58, 58, 542, 58);
h.drawLine(58, 602, 542, 602);
h.drawLine(58, 58, 58, 602);
h.drawLine(542, 58, 542, 602);
h.setFont(new Font("宋体",Font.BOLD,24));
h.drawString("汉   界", 420, 340);
h.drawString("楚    河", 130, 340);
}
}
3)跳棋棋盘
package zh;
import java.awt.*;
import javax.swing.*;
public class Jump extends JApplet {
public void paint(Graphics P)
{ setBackground(Color.white);
  int  width=9,heigth=9;
  int currentx1,currenty1,currentx2,currenty2;
 P.setColor(Color.GRAY);
  int[] xPoints=new int[]{178+5,71-9,297+9};//所有点的x坐标
  int npoints=3;//点数
  int[] ypoints=new int[]{71,297+12,297+12};//所有点的y坐标
  P.drawPolygon(xPoints,ypoints,npoints);
  P.fillPolygon(xPoints, ypoints, 3);
  int[] x2Points=new int[]{71-9,297+9,178+5};
  int[] y2points=new int[]{143+7,143+7,369+15}; 
  P.drawPolygon(x2Points,y2points,npoints);
  P.fillPolygon(x2Points, y2points, 3); 
   for(int i=0;i<13;i++){
  for(int r=0;r<=i;r++){
  currentx1=178-i*9+r*18;
  currenty1=80+i*18;
 P.setColor(Color.blue);
  P.drawOval(currentx1,currenty1,width,heigth);
  P.fillOval(currentx1,currenty1,width,heigth); 
  }
  } 
   for(int n=0;n<13;n++){
  for(int r=13;r>n;r--){
  currentx2=80-9+n*9+(13-r)*18;
  currenty2=152+n*18;
  P.drawOval(currentx2,currenty2,width,heigth);
  P.fillOval(currentx2,currenty2,width,heigth); 
  }
  }  
}
4)主程序
package zh;
import javax.swing.*;
public class zhx {
public static void main(String[] args) {
String str=JOptionPane.showInputDialog("请输入你想选择的棋盘:\n"+"z、中国象棋棋盘\n"+"g、国际象棋棋盘\n"
+"j、跳棋棋盘\n");
        switch(str){
case"z": Chess1 a=new Chess1();break;
case"g": Chessboard b=new Chessboard();break;
case"j": Jump  c=new  Jump();break;  
}
}
疑问:
单独运行前面三个程序能输出相应的棋盘,但是运行第四个程序时,显示输入界面后,就没其他输出了。
PS:
这个程序的功能是实现从键盘输入选择,可以根据选择绘制国际象棋棋盘或中国象棋棋盘或跳棋棋盘

解决方案 »

  1.   

    是的
    switch(表达式){
      case 表达式的值1:
          表达式值1所执行的语句
      break;
      case 表达式的值2:
          表达式值2所执行的语句
      break;
      ......
       default:
          默认要执行的语句
    }有以下方面需要注意:   1.switch括号里的表达式的值运算结果必须为整形一般为int型常量,其后面跟一个冒号。2.break语句用来跳出分支语句,如果没有会跳出switch体,最好每个分支语句上加上break。3.default后面为默认要执行的语句,一般放在程序的最后面。
    也就是说表达式必须是int型常量,或能转换成int的类型,比如char就可以