public class App {
public static void main(String[] args) {
new PokerGame();
}
}
import java.util.Objects;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(username, user.username) && Objects.equals(password, user.password);
}
public String toString() {
return "User{username = " + username + ", password = " + password + "}";
}
}
import com.itheima.domain.Poker;
import java.awt.*;
import java.util.ArrayList;
public class Common {
public static void move(Poker poker, Point from, Point to) {
if (to.x != from.x) {
double k = (1.0) * (to.y - from.y) / (to.x - from.x);
double b = to.y - to.x * k;
int flag = 0;
if (from.x < to.x)
flag = 20;
else {
flag = -20;
}
for (int i = from.x; Math.abs(i - to.x) > 20; i += flag) {
double y = k * i + b;
poker.setLocation(i, (int) y);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
poker.setLocation(to);
}
public static void rePosition(GameJFrame m,ArrayList list, int flag) {
Point p = new Point();
if (flag == 0) {
p.x = 50;
p.y = (450 / 2) - (list.size() + 1) * 15 / 2;
}
if (flag == 1) {
p.x = (800 / 2) - (list.size() + 1) * 21 / 2;
p.y = 450;
}
if (flag == 2) {
p.x = 700;
p.y = (450 / 2) - (list.size() + 1) * 15 / 2;
}
int len = list.size();
for (int i = 0; i < len; i++) {
Poker poker = list.get(i);
move(poker, poker.getLocation(), p);
m.container.setComponentZOrder(poker, 0);
if (flag == 1)
p.x += 21;
else
p.y += 15;
}
}
}
import com.itheima.domain.Poker;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class GameJFrame extends JFrame implements ActionListener {
public static Container container = null;
JButton[] landlord = new JButton[2];
JButton[] publishCard = new JButton[2];
JLabel dizhu;
ArrayList> currentList = new ArrayList<>();
ArrayList> playerList = new ArrayList<>();
ArrayList lordList = new ArrayList<>();
ArrayList pokerList = new ArrayList();
JTextField time[] = new JTextField[3];
public GameJFrame() {
setIconImage(Toolkit.getDefaultToolkit().getImage("farmerandlord\\image\\poker\\dizhu.png"));
initJframe();
initView();
this.setVisible(true);
initCard();
initGame();
}
public void initCard() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 13; j++) {
if ((i == 5) && (j > 2)) {
break;
} else {
Poker poker = new Poker(i + "-" + j, false);
poker.setLocation(350, 150);
pokerList.add(poker);
container.add(poker);
}
}
}
Collections.shuffle(pokerList);
ArrayList player0 = new ArrayList<>();
ArrayList player1 = new ArrayList<>();
ArrayList player2 = new ArrayList<>();
for (int i = 0; i < pokerList.size(); i++) {
Poker poker = pokerList.get(i);
if (i <= 2) {
lordList.add(poker);
Common.move(poker, poker.getLocation(), new Point(270 + (75 * i), 10));
continue;
}
if (i % 3 == 0) {
Common.move(poker, poker.getLocation(), new Point(50, 60 + i * 5));
player0.add(poker);
} else if (i % 3 == 1) {
Common. move(poker, poker.getLocation(), new Point(180 + i * 7, 450));
player1.add(poker);
poker.turnFront();
} else if (i % 3 == 2) {
Common. move(poker, poker.getLocation(), new Point(700, 60 + i * 5));
player2.add(poker);
}
playerList.add(player0);
playerList.add(player1);
playerList.add(player2);
container.setComponentZOrder(poker, 0);
}
for (int i = 0; i < 3; i++) {
order(playerList.get(i));
Common.rePosition(this,playerList.get(i),i);
}
}
public void order(ArrayList list) {
Collections.sort(list, new Comparator() {
@Override
public int compare(Poker o1, Poker o2) {
int color1 = Integer.parseInt(o1.getName().substring(0, 1));
int value1 = getValue(o1);
int color2 = Integer.parseInt(o2.getName().substring(0, 1));
int value2 = getValue(o2);
int flag = value2 - value1;
if (flag == 0){
return color2 - color1;
}else {
return flag;
}
}
});
}
public int getValue(Poker poker) {
String name = poker.getName();
int color = Integer.parseInt(poker.getName().substring(0, 1));
int value = Integer.parseInt(name.substring(2));
if (color == 5){
return value += 100;
}
if (value == 1){
return value += 20;
}
if (value == 2){
return value += 30;
}
return value;
}
private void initGame() {
for (int i = 0; i < 3; i++) {
ArrayList list = new ArrayList<>();
currentList.add(list);
}
landlord[0].setVisible(true);
landlord[1].setVisible(true);
for (JTextField field : time) {
field.setText("倒计时30秒");
field.setVisible(true);
}
}
@Override
public void actionPerformed(ActionEvent e) {
}
public void initView() {
JButton robBut = new JButton("抢地主");
robBut.setBounds(320, 400, 75, 20);
robBut.addActionListener(this);
robBut.setVisible(false);
landlord[0] = robBut;
container.add(robBut);
JButton noBut = new JButton("不 抢");
noBut.setBounds(420, 400, 75, 20);
noBut.addActionListener(this);
noBut.setVisible(false);
landlord[1] = noBut;
container.add(noBut);
JButton outCardBut = new JButton("出牌");
outCardBut.setBounds(320, 400, 60, 20);
outCardBut.addActionListener(this);
outCardBut.setVisible(false);
publishCard[0] = outCardBut;
container.add(outCardBut);
JButton noCardBut = new JButton("不要");
noCardBut.setBounds(420, 400, 60, 20);
noCardBut.addActionListener(this);
noCardBut.setVisible(false);
publishCard[1] = noCardBut;
container.add(noCardBut);
for (int i = 0; i < 3; i++) {
time[i] = new JTextField("倒计时:");
time[i].setEditable(false);
time[i].setVisible(false);
container.add(time[i]);
}
time[0].setBounds(140, 230, 60, 20);
time[1].setBounds(374, 360, 60, 20);
time[2].setBounds(620, 230, 60, 20);
dizhu = new JLabel(new ImageIcon("images/dizhu.png"));
dizhu.setVisible(false);
dizhu.setSize(40, 40);
container.add(dizhu);
}
public void initJframe() {
this.setTitle("斗地主");
this.setSize(830, 620);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
container = this.getContentPane();
container.setLayout(null);
container.setBackground(Color.LIGHT_GRAY);
}
}
import com.itheima.domain.User;
import com.itheima.util.CodeUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class LoginJFrame extends JFrame implements MouseListener {
static ArrayList allUsers = new ArrayList<>();
static {
allUsers.add(new User("zhangsan","123"));
allUsers.add(new User("lisi","1234"));
}
JButton login = new JButton();
JButton register = new JButton();
JTextField username = new JTextField();
JPasswordField password = new JPasswordField();
JTextField code = new JTextField();
JLabel rightCode = new JLabel();
public LoginJFrame() {
initJFrame();
initView();
this.setVisible(true);
}
public void initView() {
Font usernameFont = new Font(null,1,16);
JLabel usernameText = new JLabel("用户名");
usernameText.setForeground(Color.white);
usernameText.setFont(usernameFont);
usernameText.setBounds(140, 55, 55, 22);
this.getContentPane().add(usernameText);
username.setBounds(223, 46, 200, 30);
this.getContentPane().add(username);
JLabel passwordText = new JLabel("密码");
Font passwordFont = new Font(null,1,16);
passwordText.setForeground(Color.white);
passwordText.setFont(passwordFont);
passwordText.setBounds(197, 95, 40, 22);
this.getContentPane().add(passwordText);
password.setBounds(263, 87, 160, 30);
this.getContentPane().add(password);
JLabel codeText = new JLabel("验证码");
Font codeFont = new Font(null,1,16);
codeText.setForeground(Color.white);
codeText.setFont(codeFont);
codeText.setBounds(215, 142, 55, 22);
this.getContentPane().add(codeText);
code.setBounds(291, 133, 100, 30);
this.getContentPane().add(code);
String codeStr = CodeUtil.getCode();
Font rightCodeFont = new Font(null,1,15);
rightCode.setForeground(Color.RED);
rightCode.setFont(rightCodeFont);
rightCode.setText(codeStr);
rightCode.addMouseListener(this);
rightCode.setBounds(400, 133, 100, 30);
this.getContentPane().add(rightCode);
login.setBounds(123, 310, 128, 47);
login.setIcon(new ImageIcon("farmerandlord\\image\\login\\登录按钮.png"));
login.setBorderPainted(false);
login.setContentAreaFilled(false);
login.addMouseListener(this);
this.getContentPane().add(login);
register.setBounds(256, 310, 128, 47);
register.setIcon(new ImageIcon("farmerandlord\\image\\login\\注册按钮.png"));
register.setBorderPainted(false);
register.setContentAreaFilled(false);
register.addMouseListener(this);
this.getContentPane().add(register);
JLabel background = new JLabel(new ImageIcon("farmerandlord\\image\\login\\background.png"));
background.setBounds(0, 0, 633, 423);
this.getContentPane().add(background);
}
public void initJFrame() {
this.setSize(633, 423);
this.setTitle("斗地主游戏 V1.0登录");
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true);
this.setLayout(null);
}
@Override
public void mouseClicked(MouseEvent e) {
Object obj = e.getSource();
if (obj == login) {
String usernameInput = username.getText();
String passwordInput = password.getText();
String codeInput = code.getText();
if (codeInput.length() == 0) {
showJDialog("验证码不能为空");
return;
}
if (usernameInput.length() == 0 || passwordInput.length() == 0) {
showJDialog("用户名或者密码为空");
return;
}
if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
showJDialog("验证码输入错误");
return;
}
User userInfo = new User(usernameInput, passwordInput);
if (allUsers.contains(userInfo)) {
this.setVisible(false);
new GameJFrame();
} else {
showJDialog("用户名或密码错误");
}
} else if (obj == register) {
System.out.println("点击了注册按钮");
} else if (obj == rightCode) {
String code = CodeUtil.getCode();
rightCode.setText(code);
}
}
public void showJDialog(String content) {
JDialog jDialog = new JDialog();
jDialog.setSize(200, 150);
jDialog.setAlwaysOnTop(true);
jDialog.setLocationRelativeTo(null);
jDialog.setModal(true);
JLabel warning = new JLabel(content);
warning.setBounds(0, 0, 200, 150);
jDialog.getContentPane().add(warning);
jDialog.setVisible(true);
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon("farmerandlord\\image\\login\\登录按下.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon("farmerandlord\\image\\login\\注册按下.png"));
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon("farmerandlord\\image\\login\\登录按钮.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon("farmerandlord\\image\\login\\注册按钮.png"));
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
import java.util.ArrayList;
import java.util.Random;
public class CodeUtil {
public static String getCode(){
ArrayList list = new ArrayList<>();
for (int i = 0; i < 26; i++) {
list.add((char)('a' + i));
list.add((char)('A' + i));
}
System.out.println(list);
String result = "";
Random r = new Random();
for (int i = 0; i < 4; i++) {
int randomIndex = r.nextInt(list.size());
char c = list.get(randomIndex);
result = result + c;
}
int number = r.nextInt(10);
result = result + number;
char[] chars = result.toCharArray();
int index = r.nextInt(chars.length);
char temp = chars[4];
chars[4] = chars[index];
chars[index] = temp;
String code = new String(chars);
return code;
}
}