标题:小(游戏)程序看看吧!
只看楼主
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
 问题点数:0 回复次数:3 
小(游戏)程序看看吧!

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
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
得分:0 
什么?

win32汇编
病毒 加密
目前兴趣所在
2006-10-17 22:06
yangte
Rank: 1
等 级:新手上路
帖 子:163
专家分:0
注 册:2006-10-2
得分:0 
五子棋哦,不错,有个小问题,这个背景色,和白色棋子一样啊,要改一下好些啊,,呵呵。

2006-10-17 23:41
ysf0181
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:55
帖 子:914
专家分:2385
注 册:2006-10-4
得分:0 
看的不是很清楚,颜色调整下好看

ASP讨论QQ群:251346273
2006-10-18 06:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-97020-1-1.html




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

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