感想:
一开始不了解或者说是对JAVA的特性不熟悉,不知道类与类之间的联系,对JAVA的封装,继承,多态不熟悉和没有更好的理解,在以后的学习过程中要掌握并熟悉JAVA的开发习惯,
在写代码过程中还有众多东西没有掌握,靠着老师和同学的帮助完成这次学习,初步了解了JAVA面向对象开发的基本要领。写这个博客以便于自己今后学习,也算是一段美好的回忆吧!
上面是一个类图 表示着写这个程序的只要思路。
Card类:他的下级是cardview上级是player,有着一定的关系
package com.cqvie;
public class Card
{
public int rank,suit;
public int cardIdx;
public CardView cardView;
public Player owner;
public boolean selected=false;
public static String suits[]
=new String[]{"","♠","♥","♣","♦"};
public static String ranks[]
=new String[]{"J","Q","K","A","2","小王","大王"};
public Card(int rank,int suit)
{
this.rank=rank; this.suit=suit;
this.cardView=new CardView(this); //在产生Card对象时,要同步产生CardView对象
}
public String toString()
{
if(rank<=10) //10点或以下
return suits[suit]+rank;
else //10点以上
return suits[suit]+ ranks[rank-11];
}
}
CardView类:
package com.cqvie;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
public class CardView {
private Card card; // 关联的Card
private JButton btn;
//private static JFrame win; // 依附的窗口
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public JButton getBtn() {
return btn;
}
public void setBtn(JButton btn) {
this.btn = btn;
}
public CardView(final Card card) {
this.card = card;
btn = new JButton();
btn.setSize(50, 80);
btn.setMargin(new Insets(0, 0, 0, 0)); //文字和控件的边距
btn.setFont(new Font(btn.getFont().getName(), Font.BOLD, 16)); //字体设置
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(card.toString());
card.selected=!card.selected;
JButton b=(JButton)e.getSource();
if(card.selected) b.setLocation(b.getX(),b.getY()-20);
else b.setLocation(b.getX(),b.getY()+20);
}
});
}
public void show()
{
btn.setText(card.toString());
int x=card.cardIdx*btn.getWidth()+50;
//y=card.owner.payerIdx*150+50;
btn.setLocation(x,20);
this.card.owner.playerView.panel.add(btn);
}
public void refresh() //刷新按钮位置
{
int x=card.cardIdx*btn.getWidth()+50;
btn.setLocation(x,20);
}
}
Player类:
package com.cqvie;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class Player {
//public String id;
public int payerIdx;
public boolean isLandlord;
public List<Card> cards;
public PlayerView playerView;
public Player(int payerIdx)
{
this.payerIdx=payerIdx;
isLandlord=false;
cards=new LinkedList<Card>();
this.playerView=new PlayerView(this);
}
public void putCards() //出牌
{
for(int i=cards.size()-1;i>=0;i--)
if(cards.get(i).selected)
{
//从cards集合中删除card对象
Card c= cards.remove(i);
this.playerView.panel.remove(c.cardView.getBtn());
//c.cardView.getBtn().setVisible(false);
}
//出牌后调整按钮位置
for(int i=0;i<cards.size();i++) //更新cardIdx
{
cards.get(i).cardIdx=i;
cards.get(i).cardView.refresh();
}
this.playerView.panel.repaint(); //刷新面板
}
public void sortCards() //排序
{
Collections.sort(cards,new CardComparetor());
for(int i=0;i<cards.size();i++) //更新cardIdx
cards.get(i).cardIdx=i;
}
}
class CardComparetor implements Comparator<Card>
{
@Override
public int compare(Card o1, Card o2) {
if(o1.rank!=o2.rank) return o2.rank-o1.rank;
else return o1.suit-o2.suit;
}
}
PlayerView类:
package com.cqvie;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PlayerView {
public Player player;
public JPanel panel;
public PlayerView(Player p)
{
this.player=p;
panel=new JPanel();
panel.setLayout(null); //绝对布局
panel.setSize(1300, 160);
panel.setBackground(new Color(255, 255, 0));
}
public void show()
{
panel.setLocation(50, (panel.getHeight()+5)*player.payerIdx);
GameManager.win.add(panel);
for(int i=0;i<this.player.cards.size();i++)
this.player.cards.get(i).cardView.show();
//显示玩家信息
JLabel label=new JLabel();
label.setSize(100,50);
label.setLocation(50, 100);
label.setText("玩家"+(this.player.payerIdx+1));
panel.add(label);
//添加“出牌”按钮
JButton b=new JButton();
b.setSize(80,40);
b.setLocation(150, 120);
b.setText("出牌");
panel.add(b);
b.addMouseListener(
new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
player.putCards(); //player是外部类的属性
}
});
}
}
GameManager类:
package com.cqvie;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
public class GameManager {
public static JFrame win; //所属的窗口(公用)
public Player[] players=null; //玩家集合
public GameManager()
{
win=new MyWindow();
players=new Player[3];
for(int i=0;i<players.length;i++)
players[i]=new Player(i);
}
public void startGame()
{
List<Card> cards=new LinkedList<Card>(); //扑克牌
for(int i=3;i<=15;i++)
for(int j=1;j<=4;j++)
cards.add(new Card(i,j));
cards.add(new Card(16,0));
cards.add(new Card(17,0));
Random r=new Random(); //随机数产生器
for(int i=0;i<players.length;i++)
{
for(int j=0;j<17;j++)
{
int k=r.nextInt(cards.size());
cards.get(k).owner=players[i];
cards.get(k).cardIdx=j;
players[i].cards.add(cards.get(k));
cards.remove(k);
}
players[i].sortCards();
}
int t = (int)(Math.random() *players.length);
players[t].isLandlord=true;
if(players[t].isLandlord)
for(int m=17;m<=19;m++){
int k = (int)(Math.random() * cards.size());
cards.get(k).owner=players[t];
cards.get(k).cardIdx=m;
players[t].cards.add(cards.get(k));
cards.remove(k);
}
players[t].sortCards();
// for(int i=0;i<players.length;i++)
// {
// System.out.print(players[i].id+players[i].isLandlord+"的牌是");
// players[i].cards.print();
// System.out.print("\n------------------------\n");
// }
}
public void show()
{
// for(int i=0;i<players.length;i++)
// for(int j=0;j<players[i].cards.size();j++)
// {
// players[i].cards.get(j).cardView.show();
// }
for(int i=0;i<players.length;i++)
players[i].playerView.show();
win.setVisible(true);
}
}
窗口(win)类:
package com.cqvie;
import javax.swing.*;
public class MyWindow extends JFrame {
public MyWindow() {
this.setSize(1500, 800);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Test类:
package com.cqvie;
public class Test {
public static void main(String[] args) {
GameManager gm=new GameManager();
gm.startGame();
gm.show();
}
}