?? araneid.java
字號:
// main object
package spider.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 static final long serialVersionUID = 1765741821700801949L;
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("蜘蛛紙牌");
this.DIFF = diff;
// 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("新游戲(NEW)") {
private static final long serialVersionUID = -5996901378181784617L;
public void actionPerformed(ActionEvent evt) {
RestartAraneid();
}
};
JMenuItem mnuANewGame = new JMenuItem(action);
mnuGame.add(mnuANewGame);
mnuGame.addSeparator();
action = new AbstractAction("低級難度") {
private static final long serialVersionUID = -3488449640576577740L;
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("中級難度") {
private static final long serialVersionUID = -7059955518802174349L;
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("高級難度") {
private static final long serialVersionUID = -4646192299554935592L;
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);
// use a action to construct a menu item for exit menu
action = new AbstractAction("退出") {
private static final long serialVersionUID = 0L;
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("關于...") {
private static final long serialVersionUID = 0L;
public void actionPerformed(ActionEvent evt) {
AboutFrame aboutframe = new AboutFrame(getVersion());
aboutframe.setVisible(true);
}
};
JMenuItem mnuAbout = new JMenuItem(action);
mnuHelp.add(mnuAbout);
mb.add(mnuHelp);
this.setJMenuBar(mb);
this.addKeyListener(new BackgroundKeyListener(this));
// display me
this.setVisible(true);
// 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<2;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];//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.SIMPLE_DIFF);
thisgame.setVisible(true);
}
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 + -