In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article Xiaobian for you detailed introduction of "based on Java how to achieve classic spider card game", the content is detailed, the steps are clear, the details are handled properly, I hope this "how to achieve classic spider card game based on Java" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
Effect display
The previous import process will not be said here, will not be able to ask Duniang. After import, select the Spider.java class and run it directly. The following is a screenshot of the game running:
Game structure
The core code AboutDialog.java class import javax.swing.*;import java.awt.*;/* "about" window * / public class AboutDialog extends JDialog {JPanel jMainPane = new JPanel (); JTabbedPane jTabbedPane = new JTabbedPane (); private JPanel jPanel1 = new JPanel (); private JPanel jPanel2 = new JPanel (); private JTextArea jt1 = new JTextArea ("arrange the cards distributed to you many times by the computer according to the same design and color from big to small. Until all the cards on the table disappear. ") ; private JTextArea jt2 = new JTextArea ("in this game, the picture of cards comes from Windows XP's card game, and the right to the picture belongs to the original author!") / * Constructor * / public AboutDialog () {setTitle ("spider"); setSize (300200); setResizable (false); setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE); Container c = this.getContentPane () Jt1.setSize (260200); jt2.setSize (260200); jt1.setEditable (false); jt2.setEditable (false); jt1.setLineWrap (true); jt2.setLineWrap (true) Jt1.setFont (new Font ("new Font _ GB2312", java.awt.Font.BOLD, 13); jt1.setForeground (Color.blue); jt2.setFont (new Font ("java.awt.Font.BOLD _ GB2312", java.awt.Font.BOLD, 13)); jt2.setForeground (Color.black); jPanel1.add (jt1) JPanel2.add (jt2); jTabbedPane.setSize (300200); jTabbedPane.addTab (rules of the Game, null, jPanel1, null); jTabbedPane.addTab (null, jPanel2, null); jMainPane.add (jTabbedPane); c.add (jMainPane) Pack (); this.setVisible (true);}} PKCard.java class import java.awt.*;import java.awt.event.*;import javax.swing.*;public class PKCard extends JLabel implements MouseListener, MouseMotionListener {/ / position of cards Point point = null; Point initPoint = null; int value = 0; int type = 0; String name = null Container pane = null; Spider main = null; boolean canMove = false; boolean isFront = false; PKCard previousCard = null; public void mouseClicked (MouseEvent arg0) {} public void flashCard (PKCard card) {/ / start Flash thread new Flash (card) .start () / / keep getting the next card until if (main.getNextCard (card)! = null) {card.flashCard (main.getNextCard (card));}} class Flash extends Thread {private PKCard card = null Public Flash (PKCard card) {this.card = card } / * * Thread's run () method * * sets a white picture for the front of the card * / public void run () {boolean is = false ImageIcon icon = new ImageIcon ("images/white.gif"); for (int I = 0; I
< 4; i++){ try{ Thread.sleep(200); } catch (InterruptedException e){ e.printStackTrace(); } if (is){ this.card.turnFront(); is = !is; } else{ this.card.setIcon(icon); is = !is; } // 根据当前外观将card的UI属性重置 card.updateUI(); } } } /** **点击鼠标 */ public void mousePressed(MouseEvent mp){ point = mp.getPoint(); main.setNA(); this.previousCard = main.getPreviousCard(this); } /** **释放鼠标 */ public void mouseReleased(MouseEvent mr){ Point point = ((JLabel) mr.getSource()).getLocation(); //判断可行列 int n = this.whichColumnAvailable(point); if (n == -1 || n == this.whichColumnAvailable(this.initPoint)){ this.setNextCardLocation(null); main.table.remove(this.getLocation()); this.setLocation(this.initPoint); main.table.put(this.initPoint, this); return; } point = main.getLastCardLocation(n); boolean isEmpty = false; PKCard card = null; if (point == null){ point = main.getGroundLabelLocation(n); isEmpty = true; } else{ card = (PKCard) main.table.get(point); } if (isEmpty || (this.value + 1 == card.getCardValue())){ point.y += 40; if (isEmpty) point.y -= 20; this.setNextCardLocation(point); main.table.remove(this.getLocation()); point.y -= 20; this.setLocation(point); main.table.put(point, this); this.initPoint = point; if (this.previousCard != null){ this.previousCard.turnFront(); this.previousCard.setCanMove(true); } this.setCanMove(true); } else{ this.setNextCardLocation(null); main.table.remove(this.getLocation()); this.setLocation(this.initPoint); main.table.put(this.initPoint, this); return; } point = main.getLastCardLocation(n); card = (PKCard) main.table.get(point); if (card.getCardValue() == 1){ point.y -= 240; card = (PKCard) main.table.get(point); if (card != null && card.isCardCanMove()){ main.haveFinish(n); } } } /* **方法:放置纸牌 */ public void setNextCardLocation(Point point){ PKCard card = main.getNextCard(this); if (card != null){ if (point == null){ card.setNextCardLocation(null); main.table.remove(card.getLocation()); card.setLocation(card.initPoint); main.table.put(card.initPoint, card); } else{ point = new Point(point); point.y += 20; card.setNextCardLocation(point); point.y -= 20; main.table.remove(card.getLocation()); card.setLocation(point); main.table.put(card.getLocation(), card); card.initPoint = card.getLocation(); } } } /** **返回值:int **方法:判断可用列 */ public int whichColumnAvailable(Point point){ int x = point.x; int y = point.y; int a = (x - 20) / 101; int b = (x - 20) % 101; if (a != 9){ if (b >Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.