📄 araneid.java
字号:
// main object
package dujid.araneid;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.util.Vector;
public class Araneid extends JFrame {
private String version = "1.1.0";
public int cardWidth = 71; // the width of card
public int cardHeight = 96; // the height of card
public int poolSpace = 5; // the primariy pool distance
public Color backgroundcolor = new Color(0,112,26); // the background color
public Pools[] pool; // the pool to store the cards
public Vector cardlist; // eight deck of card
Random rand = new Random();
public BackgroundPanel pane;
public PoolMouseListener poolmouse;
public PoolMouseMotionListener poolmousemotion;
public Pools mousepanel;
public DealPool dealpool;
public CompletedPool completedpool;
public StatusPanel statuspanel;
public StatusPanelListener statusmouse;
public Player player;
public VictoryPanel victorypanel;
private JCheckBoxMenuItem mnuSimple;
private JCheckBoxMenuItem mnuAdvanced;
private JCheckBoxMenuItem mnuMiddle;
private int DIFF;
public static final int SIMPLE_DIFF = 1;
public static final int MIDDLE_DIFF = 2;
public static final int ADVANCED_DIFF = 3;
// constructor for araneid game
public Araneid(int diff) {
super("Araneid Game");
// format this frame
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create menubar for araneid game
JMenuBar mb = new JMenuBar();
JMenu mnuGame = new JMenu("游戏");
mb.add(mnuGame);
// use a action to construct a menu item for start a new game
Action action = new AbstractAction("新游戏(N)") {
public void actionPerformed(ActionEvent evt) {
RestartAraneid();
}
};
JMenuItem mnuANewGame = new JMenuItem(action);
mnuGame.add(mnuANewGame);
mnuGame.addSeparator();
action = new AbstractAction("低级难度") {
public void actionPerformed(ActionEvent evt) {
if (((JCheckBoxMenuItem)evt.getSource()).getState()) {
mnuMiddle.setState(false);
mnuAdvanced.setState(false);
DIFF = SIMPLE_DIFF;
RestartAraneid();
} else {
mnuSimple.setState(true);
}
}
};
mnuSimple = new JCheckBoxMenuItem(action);
if (diff==SIMPLE_DIFF) mnuSimple.setState(true);
mnuGame.add(mnuSimple);
action = new AbstractAction("中级难度") {
public void actionPerformed(ActionEvent evt) {
if (((JCheckBoxMenuItem)evt.getSource()).getState()) {
mnuSimple.setState(false);
mnuAdvanced.setState(false);
DIFF = MIDDLE_DIFF;
RestartAraneid();
} else {
mnuMiddle.setState(true);
}
}
};
mnuMiddle = new JCheckBoxMenuItem(action);
if (diff==MIDDLE_DIFF) mnuMiddle.setState(true);
mnuGame.add(mnuMiddle);
action = new AbstractAction("高级难度") {
public void actionPerformed(ActionEvent evt) {
if (((JCheckBoxMenuItem)evt.getSource()).getState()) {
mnuSimple.setState(false);
mnuMiddle.setState(false);
DIFF = ADVANCED_DIFF;
RestartAraneid();
} else {
mnuAdvanced.setState(true);
}
}
};
mnuAdvanced = new JCheckBoxMenuItem(action);
if (diff==ADVANCED_DIFF) mnuAdvanced.setState(true);
mnuGame.add(mnuAdvanced);
this.DIFF = diff;
// use a action to construct a menu item for exit menu
action = new AbstractAction("退出") {
public void actionPerformed(ActionEvent evt) {
dispose();
System.exit(0);
}
};
JMenuItem mnuExit = new JMenuItem(action);
mnuGame.addSeparator();
mnuGame.add(mnuExit);
JMenu mnuHelp = new JMenu("帮助");
action = new AbstractAction("关于...") {
public void actionPerformed(ActionEvent evt) {
AboutFrame aboutframe = new AboutFrame(getVersion());
aboutframe.show();
}
};
JMenuItem mnuAbout = new JMenuItem(action);
mnuHelp.add(mnuAbout);
mb.add(mnuHelp);
this.setJMenuBar(mb);
this.addKeyListener(new BackgroundKeyListener(this));
// display me
show();
// start a new game
initialize();
}
// method: initialize the game when a new game starting
public void initialize() {
// get system user name
String uid = System.getProperty("user.name");
if (uid.length()<=0) uid = "player1";
this.player = new Player(uid);
// create eight deck of card
cardlist = new Vector();
//int dealcount = this.DIFF==this.SIMPLE_DIFF?8:(this.DIFF==this.MIDDLE_DIFF?4:2);
switch(this.DIFF) {
case MIDDLE_DIFF:
for (int a=0;a<4;a++) {
for (int b=1;b<14;b++) {
Card card = new Card(Integer.toString(b));
this.cardlist.add(card);
}
for (int b=21;b<34;b++) {
Card card = new Card(Integer.toString(b));
this.cardlist.add(card);
}
}
break;
case ADVANCED_DIFF:
for (int a=0;a<8;a++) {
for (int b=1;b<14;b++) {
Card card = new Card(Integer.toString(b));
this.cardlist.add(card);
}
for (int b=21;b<34;b++) {
Card card = new Card(Integer.toString(b));
this.cardlist.add(card);
}
for (int b=41;b<54;b++) {
Card card = new Card(Integer.toString(b));
this.cardlist.add(card);
}
for (int b=61;b<74;b++) {
Card card = new Card(Integer.toString(b));
this.cardlist.add(card);
}
}
break;
case SIMPLE_DIFF:
default:
for (int a=0;a<8;a++) {
for (int b=1;b<14;b++) {
Card card = new Card(Integer.toString(b));
this.cardlist.add(card);
}
}
}
pane = new BackgroundPanel(this);
// construct temp drag-on-drop panel
this.mousepanel = new Pools(this);
this.mousepanel.setVisible(false);
pane.add(mousepanel);
// construct status panel
this.statuspanel = new StatusPanel(this);
this.statusmouse = new StatusPanelListener(this);
this.statuspanel.addMouseListener(this.statusmouse);
pane.add(statuspanel);
// addition mouse listener
this.poolmouse = new PoolMouseListener(this);
this.poolmousemotion = new PoolMouseMotionListener(this);
// construct 10 pool for store some of cards
pool= new Pools[10];
for (int k=0;k<5;k++) {
for (int i=0;i<pool.length;i++) {
if (pool[i]==null) {
pool[i] = new Pools(i,this);
pane.add(pool[i]);
pool[i].addMouseListener(poolmouse);
pool[i].addMouseMotionListener(poolmousemotion);
}
deal(pool[i]);
if (k==4 && i<4) deal(pool[i]);
}
}
// construct a dealpool for wait player deal cards
dealpool = new DealPool(this);
pane.add(dealpool);
for(int i=0;i<5*pool.length;i++) {
int r = rand.nextInt(cardlist.size());
Card card = (Card)cardlist.get(r);
cardlist.remove(r);
dealpool.putCard(card);
}
dealpool.deal();
// construct the pool for restore the completed cards
this.completedpool = new CompletedPool(this);
pane.add(completedpool);
this.getContentPane().add(pane);
this.victorypanel = new VictoryPanel(this);
}
// method: deal to every last of pool
public void deal(Pools targetPool) {
int r = rand.nextInt(cardlist.size());
Card card = (Card)cardlist.get(r);
cardlist.remove(r);
targetPool.putCard(card);
}
public static void main(String args[]) {
// change my appearance
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ignore) {}
// construct a new game
Araneid thisgame = new Araneid(Araneid.MIDDLE_DIFF);
thisgame.setVisible(true);
System.out.println("\nAraneid Game " + thisgame.getVersion()+ "\n" +
"作者 dujid <dujid@idr.gov.cn, dujid@mail.suun.com.cn> 2002-11-6\n\n" +
"如有问题,请发信到上述地址。");
}
public String getVersion() {
return this.version;
}
public void RestartAraneid() {
int response = JOptionPane.showConfirmDialog(null,
"立即开始一个新游戏吗?",
"Araneid 消息",
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (response == JOptionPane.NO_OPTION) return;
dispose();
Araneid newgame = new Araneid(this.DIFF);
newgame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -