45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:汇智学堂-JAVA桌面游戏开发(五子棋之三:程序主界面)

汇智学堂-JAVA桌面游戏开发(五子棋之三:程序主界面)

2019-03-28 08:10:44 来源:www.45fan.com 【

之三:程序主界面
等级选择“初级”,进入“程序主界面”,效果见下图:
汇智学堂-JAVA桌面游戏开发(五子棋之三:程序主界面)

准备工作:
汇智学堂-JAVA桌面游戏开发(五子棋之三:程序主界面)
Images文件夹下,放置level.png图片一张。

代码实现:

PCBoard .java

package com.fivechess.view;

public class PCBoard extends Board {
private PCChessBoard2 pcchessboard2;//棋盘
public PCBoard() {
init();
}

public void init() {
pcchessboard2=new PCChessBoard2();
pcchessboard2.setBounds(200, 50, 600, 700);
pcchessboard2.setVisible(true);
add(pcchessboard2);
}

}

//Board类的代码:

package com.fivechess.view;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Board extends JFrame {

public Board() {
 
 setLayout(null); //取消原来布局
   setBounds(300,50,800,700);
   //init1();
   setVisible(true);
    
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setResizable(false);
   
   setTitle("雷雷的五子棋");
}

/**
 * 绘图
 * @param g
 */
public void paint(Graphics g)
{
  super.paint(g);
  repaint();
}

}

===========================================================================
//棋盘PCChessBoard2类

package com.fivechess.view;

import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;

public class PCChessBoard2 extends ChessBoard2{

public int whichColor=0;
private int ex;
private int ey;

public static int isClose=0;

//落下棋子
@Override
public void mouseMoved(MouseEvent e) {
repaint();  
} 

@Override
public void mouseClicked(MouseEvent e) {

if(isClose==1) {
return;
}

 ex=e.getX();
 ey=e.getY();
 
 float x = (ex - 25) / 25;
   float y = (ey - 25) / 25;
   int x1 = (int) x;
   int y1 = (int) y;
}

}

ChessBoard2类

package com.fivechess.view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

/*
 * 汇制棋盘的代码
 * */
public class ChessBoard2 extends JPanel implements MouseMotionListener,MouseListener{

public Image chessBoardImage;
public Image blackChess;
public Image whiteChess;

public Image buttonBoard;

public static final int COLS=19;
public static final int RAWS=19;//棋盘的行数和列数

public int position[][]=new int[COLS][RAWS];

public ChessBoard2() {

chessBoardImage = Toolkit.getDefaultToolkit().getImage("images/chessBoard.jpg");
blackChess= Toolkit.getDefaultToolkit().getImage("images/black.png");
whiteChess= Toolkit.getDefaultToolkit().getImage("images/white.png");

buttonBoard= Toolkit.getDefaultToolkit().getImage("images/buttonBoard.png");

addMouseListener(this);
}

 /**
    * 初始化数组为BLANK
   */
  public void initStart()
  {
    for(int i=0;i<19;i++)
    {
      for(int j=0;j<19;j++)
      {
      position[i][j]= 0;//“0”代表当前无棋子
      }
    }
  }

//汇制棋盘
@Override
  public void paintComponent(Graphics g) {
  super.paintComponent(g);
    g.drawImage(chessBoardImage, 0, 0,null);     
    g.drawImage(buttonBoard, 550, 50, null);
     
    for (int i = 0; i < 19; i++) {
      for (int j = 0; j < 19; j++) {
        if (position[i][j] ==2) {
          g.drawImage(blackChess, 13+i * 25 , 13+j * 25 , null);
        } else if (position[i][j] ==1) {
          g.drawImage(whiteChess, 13+i * 25 , 13+j * 25 , null);
        }
      }
    }
  }

 @Override
  public void mouseClicked(MouseEvent e) {
 
  }
  /**
   * 按下鼠标时,记录鼠标的位置,并改写数组的数值,重新绘制图形
   * @param e
   */
  @Override
  public void mousePressed(MouseEvent e) {
   }

  @Override
  public void mouseReleased(MouseEvent e) {
  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}

 
 

本文地址:http://www.45fan.com/dnjc/99792.html
Tags: 学堂 -java 汇智
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部