import javax.swing.border.*
import java.awt.*
import java.beans.*
import java.util.*
/**
* 面板上的光标改变成为手型
* @author hardneedl
*/
final class ChessPaint extends JFrame{
private static final Dimension SIZE = new Dimension(600,400)
public Dimension getMinimumSize() {return SIZE}
public Dimension getMaximumSize() {return SIZE}
public Dimension getPreferredSize() {return SIZE}
public String getTitle() {return "ChessPaint"}
/**
* 棋盘
* 实现属性变化监听
*/
private class Chessboard extends Observable implements PropertyChangeListener{
private int columns, rows, cellWidth
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this)
private Chessboard(int columns, int rows, int cellWidth) {
this.columns = columns
this.rows = rows
this.cellWidth = cellWidth
}
private int getColumns() {
return columns
}
private void setColumns(int columns) {
propertyChangeSupport.firePropertyChange("columns", this.columns, columns)
this.columns = columns
}
private int getRows() {
return rows
}
private void setRows(int rows) {
propertyChangeSupport.firePropertyChange("columns", this.rows, rows)
this.rows = rows
}
private int getCellWidth() {
return cellWidth
}
private void setCellWidth(int cellWidth) {
this.cellWidth = cellWidth
}
public void propertyChange(PropertyChangeEvent evt) {
switch (evt.getPropertyName()) {
case "columns":
case "rows":
case "cellWidth":
if (!evt.getOldValue().equals(evt.getNewValue())) {
setChanged()
notifyObservers(this)
}
break
}
}
}
/**
* 画笔
*/
private interface Brush {
void paint(Graphics g)
Component getCompoment()
}
abstract private class ChessboardBrush implements Brush, Observer {
private Chessboard chessboard
private ChessboardBrush(Chessboard chessboard) {
this.chessboard = chessboard
}
public void paint(Graphics g) {
if (chessboard == null) return
Graphics2D g2 = (Graphics2D) g.create()
//背景白色
g2.setColor(Color.WHITE)
g2.fillRect(0,0, getCompoment().getWidth(), getCompoment().getHeight())
//整体偏移坐标系
g2.translate(100,100)
g2.setColor(Color.BLACK)
//绘制行线
for (int r = 0 r <= chessboard.getRows() r ++)
g2.drawLine(0, r * chessboard.getCellWidth(), chessboard.getColumns() * chessboard.getCellWidth(), r * chessboard.getCellWidth())
//绘制竖线
for (int c = 0 c <= chessboard.getColumns() c++)
g2.drawLine(c * chessboard.getCellWidth(), 0, chessboard.getCellWidth() * c , chessboard.getRows() * chessboard.getCellWidth())
g2.dispose()
}
public void update(Observable o, Object arg) {
if (arg instanceof Chessboard)
chessboard = (Chessboard)arg
}
}
/*画布*/
private class Canvas extends JComponent{
private Brush getBrush() {
return brush
}
private void setBrush(Brush brush) {
this.brush = brush
}
private Brush brush
private Canvas() {
super()
}
protected void paintComponent(Graphics g) {
super.paintComponent(g)
if(isVisible() && brush != null)
brush.paint(g)
}
public Border getBorder() {
return BorderFactory.createLineBorder(Color.BLUE,2)
}
}
private Canvas canvas
private ChessboardBrush brush
private Chessboard chessboard
private ChessPaint() {
super()
init()
addListeners()
doLay()
}
private void init(){
chessboard = new Chessboard(19,19,30)
canvas = new Canvas()
brush = new ChessboardBrush(chessboard) {
public Component getCompoment() {
return canvas
}
}
canvas.setBrush(brush)
chessboard.addObserver(brush)
}
private void addListeners(){
}
private void doLay(){
Container container = getContentPane()
container.add(canvas, BorderLayout.CENTER)
pack()
setVisible(true)
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
SwingUtilities.invokeLater(ChessPaint::new)
}
}
其实我也有用JAVA做五子棋呢~,棋盘都是用画的,我把代码发下,你自己试下,也不定合你一意.事件代码我都去啦,因为是简单的麻烦事.~!import java.awt.*
import javax.swing.*
@SuppressWarnings("serial")
public class ChessBoard extends JPanel{
/*
* 制作棋盘的宽高
*/
public static final int BOARD_WIDTH=515
/*
* 计算棋盘表格坐标(单元格宽高相等)
*/
public static int [] location=new int[22]
static{
for(int i=0,WIDTH=30i<location.lengthi++,WIDTH+=22){
location[i]=WIDTH
}
}
public ChessBoard(int x,int y){
super(null)
this.setBounds(x, y, BOARD_WIDTH, BOARD_WIDTH)
this.setBackground(new Color(255, 164, 85))
}
/**
* 重写方法,绘制棋盘表格图
*/
public void paintComponent(Graphics g){
super.paintComponent(g)
char ch='A'
g.setFont(new Font("宋体",Font.BOLD,12))
//画横线
for(int i=0,width=30+22*21i<location.lengthi++,ch++){
g.setColor(Color.black)
g.drawLine(30,location[i],width,location[i])
g.setColor(Color.blue)
g.drawString(""+ch,5,location[i]+3)
}
//画竖线
for(int i=0,width=30+22*21i<location.lengthi++){
g.setColor(Color.black)
g.drawLine(location[i],30,location[i],width)
g.setColor(Color.blue)
g.drawString(""+(i+1),location[i]-3,13)
}
}
}
楼主要是觉得看的不舒服可以拷到记事本里看~ import java.applet.* import java.awt.* import java.awt.event.* import java.applet.Applet import java.awt.Color//这一段import就不说了,下面要用到的就import进来 public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //继承Applet表明是个applet,后面的接口必须要实现每个接口的所有方法 { int color_Qizi=0//旗子的颜色标识 0:白子 1:黑子 int intGame_Start=0//游戏开始标志 0未开始 1游戏中 int intGame_Body[][]=new int[16][16]//设置棋盘棋子状态 0 无子 1 白子 2 黑子 Button b1=new Button("游戏开始") Button b2=new Button("重置游戏")//两个按钮 Label lblWin=new Label(" ")//这个label用来显示最后输赢信息的,先留空 Checkbox ckbHB[]=new Checkbox[2]//用来表明选择黑气或白棋先走的checkbox CheckboxGroup ckgHB=new CheckboxGroup()//两个checkbox必须放在同一个checkboxgroup里才能做到单选 public void init() //初始化,堆砌界面 { setLayout(null)//不设布局管理器 addMouseListener(this)//将本类作为鼠标事件的接口响应鼠标动作 add(b1)//将事先定义好的第一个按钮添加入界面 b1.setBounds(330,50,80,30)//设置第一个按钮左上角的位置和大小 b1.addActionListener(this)//将本类作为按钮事件的接口响应按钮动作 add(b2)//将事先定义好的第二个按钮添加进去 b2.setBounds(330,90,80,30)/设置第二个按钮左上角的位置和大小 b2.addActionListener(this)//将本类作为按钮事件的接口响应按钮动作 ckbHB[0]=new Checkbox("白子先",ckgHB,false)//new一个checkbox ckbHB[0].setBounds(320,20,60,30)//设置左上角位置和大小 ckbHB[1]=new Checkbox("黑子先",ckgHB,false)//new第二个checkbox ckbHB[1].setBounds(380,20,60,30)//设置左上角位置和大小 add(ckbHB[0])//将第一个checkbox加入界面 add(ckbHB[1])//将第二个checkbox加入界面 ckbHB[0].addItemListener(this)//将本类作为其事件接口来响应选中动作 ckbHB[1].addItemListener(this)//将本类作为其事件接口来响应选中动作 add(lblWin)//将标签加入界面 lblWin.setBounds(330,130,80,30)//设置标签的左上角位置和大小 Game_start_csh()//调用游戏初始化 } public void itemStateChanged(ItemEvent e) //ItemListener接口中的方法,必须要有 { if (ckbHB[0].getState()) //选择黑子先还是白子先 { color_Qizi=0//白棋先 } else { color_Qizi=1//黑棋先 } } public void actionPerformed(ActionEvent e) //ActionListener接口中的方法,也是必须的 { Graphics g=getGraphics()//这句话貌似可以去掉,g是用来画图或者画界面的 if (e.getSource()==b1) //如果动作的来源是第一个按钮 { Game_start()//游戏开始 } else //否则 { Game_re()//游戏重新开始 } } public void mousePressed(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseClicked(MouseEvent e) //鼠标单击时 { Graphics g=getGraphics()//获得画笔 int x1,y1 x1=e.getX()//单击处的x坐标 y1=e.getY()//单击处的y坐标 if (e.getX()<20 || e.getX()>300 || e.getY()<20 || e.getY()>300) //在棋盘范围之外 { return//则这是不能走棋的,直接返回 } //下面这两个if和两个赋值的作用是将x和y坐标根据舍入原则修改成棋盘上格子的坐标 if (x1%20>10) { x1+=20 } if(y1%20>10) { y1+=20 } x1=x1/20*20 y1=y1/20*20 set_Qizi(x1,y1)//在棋盘上画上一个棋子 } public void mouseEntered(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseExited(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseReleased(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseDragged(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseMoved(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void paint(Graphics g) //重绘和applet程序装载的时候会调用这个绘制的过程 { draw_qipan(g)//画棋盘 }欢迎分享,转载请注明来源:内存溢出
评论列表(0条)