| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 574 人关注过本帖
标题:小(游戏)程序看看吧!
取消只看楼主 加入收藏
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
收藏
 问题点数:0 回复次数:0 
小(游戏)程序看看吧!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;

class MainPanel extends JPanel {
private ArrayList items = new ArrayList(100);
private Line2D.Float[] rect = new Line2D.Float[16];
private Line2D.Float[] vert = new Line2D.Float[16];
private JButton reset = new JButton("Reset");
private JButton quit = new JButton("Quit");
private int[][] data = new int[15][15];
private boolean isBlack = true;
private boolean paintEnabled = true;

public MainPanel() {
initPanel();
setLayout(new BorderLayout());

JPanel panel = new JPanel();
panel.add(reset);
panel.add(quit);
add(panel, BorderLayout.SOUTH);

addMouseListener(new MouseHandler());
}

private void initPanel() {
int startX = 50;
int startY = 50;

for(int i = 0; i < 16; i++) {
rect[i] = new Line2D.Float(startX, startY + 20*i, startX + 300, startY + 20*i);
vert[i] = new Line2D.Float(startX + 20*i, startY, startX + 20*i, startY + 300);
}

for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
data[i][j] = 0;

reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isBlack = true;
paintEnabled = true;

items.clear();
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
data[i][j] = 0;
repaint();
}
});
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;
for(int i = 0; i < 16; i++) {
g2.draw(rect[i]);
g2.draw(vert[i]);
}

boolean black = true;
for(int i = 0; i < items.size(); i++) {
if(black) {
g.setColor(Color.BLACK);
black = false;
} else {
g.setColor(Color.WHITE);
black = true;
}
g2.fill((Ellipse2D)items.get(i));
}
}

private class MouseHandler extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();

if((x > 50 && x < 350 && y > 50 && y < 350) && paintEnabled) {
int i = (y - 50) / 20;
int j = (x - 50) / 20;

if(data[i][j] == 0) {
if(isBlack) {
data[i][j] = 1;
isBlack = false;
} else {
data[i][j] = -1;
isBlack = true;
}


int cx = j * 20 + 60;
int cy = i * 20 + 60;
Ellipse2D ellipse = new Ellipse2D.Float();
ellipse.setFrameFromCenter(cx, cy, cx + 8, cy + 8);
items.add(ellipse);
repaint();

int result = check(data);
int select = -999;
if(result == 1)
select = JOptionPane.showConfirmDialog(
MainPanel.this,
"Black player wins!\nDo you want to play again?",
"Message Box",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
else if(result == -1)
select = JOptionPane.showConfirmDialog(
MainPanel.this,
"White player wins!\nDo you want to play again?",
"Message Box",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);

if(select == JOptionPane.YES_OPTION) {
isBlack = true;
paintEnabled = true;

items.clear();
for(int p = 0; p < 15; p++)
for(int q = 0; q < 15; q++)
data[p][q] = 0;
repaint();
} else if(select == JOptionPane.NO_OPTION) {
paintEnabled = false;
}
}
}
}
}

public static int check(int[][] a) {
int res = 0;
int num = 0;

for(int i = 0; i < 15; i++) {
res = 0;
num = 0;

if((res = a[i][0]) != 0)
num = 1;

for(int j = 1; j < 15; j++) {
if((a[i][j] == res) && res != 0) {
if(++num == 5) //find one
return res;
} else if(a[i][j] == 0) {
res = 0;
num = 0;
} else {
res = a[i][j];
num = 1;
}
}
}

for(int j = 0; j < 15; j++) {
res = 0;
num = 0;

if((res = a[0][j]) != 0)
num = 1;

for(int i = 1; i < 15; i++) {
if((a[i][j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[i][j] == 0) {
res = 0;
num = 0;
} else {
res = a[i][j];
num = 1;
}
}
}

for(int i = 4; i <= 24; i++) {
res = 0;
num = 0;

if(i <= 14) {
if((res = a[0][i]) != 0)
num = 1;

for(int j = 1; j <= i; j++) {
if((a[j][i-j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][i-j] == 0) {
res = 0;
num = 0;
} else {
res = a[j][i-j];
num = 1;
}
}
} else {
if((res = a[i-14][14]) != 0)
num = 1;

for(int j = i - 13; j <= 14; j++) {
if((a[j][i-j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][i-j] == 0) {
res = 0;
num = 0;
} else {
res = a[j][i-j];
num = 1;
}
}
}
}

for(int i = 4; i <= 24; i++) {
res = 0;
num = 0;

if( i <= 14) {
if((res = a[0][14-i]) != 0)
num = 1;

for(int j = 1; j <= i; j++) {
if((a[j][14-i+j] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][14-i+j] == 0) {
res = 0;
num = 0;
} else {
res = a[j][14-i+j];
num = 1;
}
}
} else {
if((res = a[i-14][0]) != 0)
num = 1;

for(int j = i - 13, k = 1; j <= 14; j++, k++) {
if((a[j][k] == res) && res != 0) {
if(++num == 5)
return res;
} else if(a[j][k] == 0) {
res = 0;
num = 0;
} else {
res = a[j][k];
num = 1;
}
}
}
}

return res;
}

}

class MainFrame extends JFrame {
public MainFrame() {
setTitle("Five's Game");
setSize(400, 500);
setResizable(false);

getContentPane().add(new MainPanel());
}
}

public class Five {
public static void main(String[] args) {
JFrame jellen = new MainFrame();
jellen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jellen.show();
}
}

搜索更多相关主题的帖子: 游戏 java awt import 
2006-10-17 21:30
快速回复:小(游戏)程序看看吧!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026836 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved