📄 five.java
字号:
/**
* @(#)FIVE.java
*
*
* @author
* @version 1.00 2008/4/27
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class theFive extends Canvas {
public static final int BLACK=1,WHITE=-1;
private int[][]map;
private int size,cell;
int color=BLACK;
Graphics gbuff,gfive;
Image buff;
int id=1;
public theFive(int s,int c) {
this.size=s;
this.cell=c;
map=new int[size+2][];
for(int i=0;i<map.length;i++){
map[i]=new int[size+2];
}
setBackground(new Color(200,55,25));
setSize(size*(cell+2),size*(cell+2));
addMouseListener(new MouseHander());
}
public void paint(Graphics g){
if(gbuff==null){
buff=createImage(getWidth(),getHeight());
gbuff=buff.getGraphics();
}drawBoard(g);
}
private void drawLine(){
gbuff.setColor(Color.black);
for(int i=1;i<=size;i++){
gbuff.drawLine(cell,i*cell,cell*size,i*cell);
gbuff.drawLine(i*cell,cell,i*cell,cell*size);
}
}
private void drawBlack(int x,int y){
Graphics2D gbuff=(Graphics2D)this.gbuff;
gbuff.setColor(Color.black);
gbuff.fillOval(x*cell-cell/2,y*cell-cell/2,cell,cell);
gbuff.setColor(Color.white);
gbuff.drawOval(x*cell-cell/2,y*cell-cell/2,cell,cell);
}
private void drawWhite(int x,int y){
Graphics2D gbuff=(Graphics2D)this.gbuff;
gbuff.setColor(Color.white);
gbuff.fillOval(x*cell-cell/2,y*cell-cell/2,cell,cell);
gbuff.setColor(Color.black);
gbuff.drawOval(x*cell-cell/2,y*cell-cell/2,cell,cell);
}
private void drawStones(){
for(int x=1;x<=size;x++)
for(int y=1;y<=size;y++)
{ if(map[x][y]==BLACK)
drawBlack(x,y);
else if
(map[x][y]==WHITE)
drawWhite(x,y);
}
}
synchronized private void drawBoard(Graphics g){
// gbuff.clearRect(0,0,getWidth(),getHeight());
drawLine();
drawStones();
g.drawImage(buff,0,0,this);
}
public void update(Graphics g){paint(g);
}
class MouseHander implements MouseListener{
public void mousePressed(MouseEvent me){
int x=(int)Math.round(me.getX()/(double)cell);
int y=(int)Math.round(me.getY()/(double)cell);
if(x==0||y==0||x==size+1||y==size+1) return;
if(map[x][y]==BLACK||map[x][y]==WHITE) return;
if(id==1)
{color=BLACK;
id=-1;
}else if(id==-1){
color=WHITE;
id=1;
}
map[x][y]=color;
if(check(new Point(x,y),color)){
System.out.print("win...、\n\n\n\n\n\n");
}
repaint();
}
public void mouseExited(MouseEvent me){
}
public void mouseReleased(MouseEvent me){
}
public void mouseEntered(MouseEvent me){
}
public void mouseClicked(MouseEvent me){
}
private boolean check(Point p,int col){
if(count(p,1,0,col)+count(p,-1,0,col)==4)
return true;
if(count(p,0,1,col)+count(p,0,-1,col)==4)
return true;
if(count(p,-1,-1,col)+count(p,1,1,col)==4)
return true;
if(count(p,1,-1,col)+count(p,-1,1,col)==4)
return true;
return false;
}
private int count(Point p,int dx,int dy,int col){
int i=0;
for(;map[p.x+(i+1)*dx][p.y+(i+1)*dy]==col;i++);
return i;
}
}
}
public class Five extends JFrame{
private theFive jin=new theFive(15,30);
public Five(String title){
super(title);
setLayout(null);
jin.setLocation(10,10);
add(jin);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main (String[] args) {
Five play=new Five("jin'S");
play.setSize(580,620);
play.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -