java军旗源码 --第一讲
java吧
全部回复
仅看楼主
level 6
lbj2004032 楼主
建文本 在SRC下
2-2-2
11111
10201
12021
10201
11111
3.3.3
3.3.3
11111
10201
12021
10201
11111
2-2-2
import java.awt.Graphics;
import java.awt.Image;
/**
* 棋子
*/
public class ChessBlock {
     private String chessName;// 司令,工兵
     private Image img;
     private String color;
     public String getChessName() {
         return chessName;
     }
     public void setChessName(String chessName) {
         this.chessName = chessName;
     }
     public String getColor() {
         return color;
     }
     public void setColor(String color) {
         this.color = color;
     }
     public Image getImg() {
         return img;
     }
     public void setImg(Image img) {
         this.img = img;
     }
     public ChessBlock(String chessName, Image img, String color) {
         super();
         this.chessName = chessName;
         this.img = img;
         this.color = color;
     }
    
     //画图
     public void draw(Graphics g){
         g.drawImage(img, 0, 0, null);
         g.drawString(chessName, 3, 10);
     }
}
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JPanel;
/**
* 棋盘
*/
public class ChessBoard extends JPanel {
     private final static int WIDTH   = 393;
     private final static int HEIGHT = 532;
    
     private JPanel panel = new JPanel();
     private static ChessBox boxs[][] = new ChessBox[14][5];
     public ChessBoard() {
         this.setLayout(null);
         this.setSize(WIDTH, HEIGHT);
         this.setLocation(100, 0);

2010年02月26日 02点02分 1
level 6
lbj2004032 楼主
         panel.setLayout(new GridLayout(14, 5, 5, 15));
         panel.setLocation(100, 0);
         panel.setSize(195, HEIGHT);
         loadMap();
         this.add(panel);
     }
     public void paint(Graphics g) {
         super.paint(g);
         g.drawImage(LoadImage.bgimg, 0, 0, null);
         this.repaint();
        
         ChessBlock chess = new ChessBlock("工兵",LoadImage.redimg,ChessColor.getRED());
         chess.draw(g);
     }
     // 加载地图
     public void loadMap() {
         try {
             BufferedReader br = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream("map.txt")));
             for (int i = 0; i < 14; i++) {
                 String line = br.readLine();
                 for(int j=0;j<5;j++){
                     char c = line.charAt(j);
                     ChessBox box = null;
                     if(c=='2'){
                         box = new ChessBox(false,false);
                         if(i<6){
                             box.setColor(ChessColor.getRED());
                         }else if(i>7){
                             box.setColor(ChessColor.getBLUE());
                         }

2010年02月26日 02点02分 2
level 6
lbj2004032 楼主
                     }else if(c=='1'){
                         box = new ChessBox(true,false);
                         if(i<6){
                             box.setColor(ChessColor.getRED());
                         }else if(i>7){
                             box.setColor(ChessColor.getBLUE());
                         }
                     }else if(c=='0'){
                         box = new ChessBox(false,true);
                     }else if(c=='3'){
                         box = new ChessBox(false,false);
                     } else {
                         box = new ChessBox(false,false);
                     }
                     boxs[i][j] = box;
                     panel.add(box);
                 }
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
}
import javax.swing.JLabel;
public class ChessBox extends JLabel{

2010年02月26日 02点02分 3
level 6
lbj2004032 楼主

     private final static int WIDTH = 36;
    
     private final static int HEIGHT = 27 ;
    
     private boolean fly;//标示能不能飞行
    
     private boolean protect;//标示能是不是营
    
     private String color;
    
     private boolean canMove = true;
    
     public ChessBox(boolean fly,boolean protect){
         this.setText("a");
         this.fly = fly;
         this.protect = protect;
     }
     public static int getHEIGHT() {
         return HEIGHT;
     }
     public static int getWIDTH() {
         return WIDTH;
     }
     public boolean isCanMove() {
         return canMove;
     }
     public void setCanMove(boolean canMove) {
         this.canMove = canMove;
     }
     public String getColor() {
         return color;
     }
     public void setColor(String color) {
         this.color = color;
     }
     public boolean isFly() {
         return fly;
     }
     public void setFly(boolean fly) {
         this.fly = fly;
     }
     public boolean isProtect() {
         return protect;
     }
     public void setProtect(boolean protect) {
         this.protect = protect;
     }
}
public class ChessColor {
     private final static String RED = "red";
     private final static String BLUE = "blue";
     public static String getBLUE() {
         return BLUE;
     }
     public static String getRED() {
         return RED;
     }
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

2010年02月26日 02点02分 4
level 6
lbj2004032 楼主

import javax.swing.JFrame;
/**
* 主窗口
* */
public class GameWin extends JFrame{
     private ChessBoard cb = new ChessBoard();
    
     public GameWin(){
         this.setLayout(null);
         this.setSize(600,600);
         this.add(cb);
         this.setLocationRelativeTo(null);
         this.setVisible(true);
         this.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e) {
                 System.exit(0);
             }
         });
     }
    
    
     public static void main(String[] args) {
         new GameWin();
     }
}
import java.awt.Image;
import java.awt.Toolkit;
public class LoadImage {
     public static Image bgimg = Toolkit.getDefaultToolkit().createImage(
             "./image/chessboard.gif");
     public static Image blueimg = Toolkit.getDefaultToolkit().createImage(
             "./image/blue.gif");
     public static Image redimg = Toolkit.getDefaultToolkit().createImage(
             "./image/red.gif");
}

2010年02月26日 02点02分 5
1