package day01;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
//创建一个窗口,480*850
public class Yang9 {
public static void main(String[] args) {
JFrame frame = new JFrame();//窗口
JPanel panel = new JPanel();//画布
//取消画布默认布局
//取消后,所有放在画布上的内容都要自己指定大小布局
panel.setLayout(null);
//准备一张图片
JLabel background = new JLabel(
new ImageIcon("res/背景.jpg")//在括号里指定图片的路径
);//创建一个用于表示图片的组件
background.setSize(480,800);
//将图片放到画布上
panel.add(background);//add添加
ArrayList<JButton> cards = CreateCards();//洗牌并生成144张牌
//再准备一个集合,存下放的卡槽的七张牌
ArrayList<JButton> through = new ArrayList<>();
//为牌堆中每一张牌都添加点击事件
addClickAction(cards,through,panel);
//发牌
/**
* 第一次发最下层的49张牌
* 取出前49张牌 0,48
* 初始位置30,100
*/
postForCards(cards,panel,49,0,7,30,100);
/**
* 第二层36张 49,85 ,下标,6*6
* 起始位置:60,130
*/
postForCards(cards,panel,36,49,6,60,130);
/**
* 第三层35张85,119 7*5
* 起始位置 30,160
*/
postForCards(cards,panel,35,85,7,30,160);
/**
* 第四层24张 120,143 6*4
* 起始位置 60,190
*/
postForCards(cards,panel,24,120,6,60,190);
//发完后检查压牌情况
checkCovered(cards);
//将画布放在画框上
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//default默认 , close 关闭 ,Operation 操作
frame.setSize(480,850);
frame.setLocationRelativeTo(null);//居中
frame.setVisible(true);//可见
}
//检查牌的压住情况
public static void checkCovered(ArrayList<JButton> cards){
/**
* 测试某一张牌是否被后面的某一张牌压住了
* 判断第一张牌是否被压住
* 1获取这张牌
* 2遍历之后的所有牌是否有压住这张牌的形状
*/
for (int j = 0; j <cards.size() ; j++) {
JButton botton = cards.get(j);
for (int i = j+1; i <cards.size() ; i++) {
JButton top = cards.get(i);
if (covered(top,botton)){
botton.setEnabled(false);
break;
}else {
//不被压住就恢复可点击
botton.setEnabled(true);
}
}}
}
//该方法用于生成144张牌
public static ArrayList<JButton> CreateCards(){
String[] names = {"刷子","剪刀","南瓜","奶瓶","帽子","干草","手套","月饼","树","树桩","栗子",
"毛线","水桶","火堆","白菜","苹果"};
ArrayList<JButton> cards = new ArrayList<>();
//执行九次,生成144张牌
for (int i = 0; i <9*names.length ; i++) {
String name = "res/" + names[i%names.length]+".png";
String name2 = "res/" + names[i%names.length]+"2.png";//不能被点击的图片,被覆盖的图片
JButton card = new JButton(new ImageIcon(name));
//为按钮取名字,点击事件以便知道点了什么图片
card.setName(names[i%names.length]);
card.setDisabledIcon(new ImageIcon(name2));//按钮不能被点击
// card.setDisabledSelectedIcon(new ImageIcon(name2));//按钮不能被点击,被压住是灰色。
card.setSize(59,66);
card.setBorderPainted(false);//边框 绘制 ,不绘制
card.setContentAreaFilled(false);//内容区域是否填充。
cards.add(card);
}
Collections.shuffle(cards);
return cards;
}
//摆牌(发牌操作)
public static void postForCards(ArrayList<JButton> cards,//装着144张牌的集合
JPanel panel,//画布
int sum,//这一层拜多少张牌
int offset,//从第几张开始拿
int cols,//一行几张牌
int x,
int y//这一层第一张的X,Y坐标
){
for (int i = 0; i <sum ; i++) {//会循环35次
JButton card = cards.get(i+offset);
card.setLocation(x+i%cols*59,y+i/cols*60);//一行7个
panel.add(card,0);//按钮放在画布上,0表示放在最开始。
}
}
//判断上面的卡牌top是否压住了下面的卡牌button
public static boolean covered(JButton top,JButton button){
int x1 = button.getX()-59;
int x2 = button.getX()+59;
int y1 = button.getY()-60;
int y2 = button.getY()+60;
boolean xOK=top.getX()>x1&&top.getX()<x2;
boolean yOK=top.getY()>y1&&top.getY()<y2;
return xOK&&yOK;
// if (top.getX()>x1&&top.getX()<x2&&top.getY()>y1&&top.getY()<y2){
// return true;
// }else {
// return false;
// }
}
//为牌堆的每一张牌添加一个点击事件click 点击 action 动作
public static void addClickAction(ArrayList<JButton> cards,ArrayList<JButton> through,JPanel panel){
for (int i = 0; i < cards.size(); i++) {
//取出一张牌
JButton card = cards.get(i);
//添加点击事件
card.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (through.size()<7) {
/**
* 获取事件源,事件由谁产生的
*/
JButton current = (JButton) e.getSource();//获取被点击的按钮。current:当前的
//通过按钮获取他的名字
String name = current.getName();
System.out.println(name + "被点击了");
/**
* 点击后,要:
* 1:将这张牌从上方牌堆移除(从cards中删除)
* 2:存入下方槽子(through)里
* 3:修改这张牌在画布的显示位置,改坐标
*/
cards.remove(current);
System.out.println("牌堆还剩:" + cards.size());
/**
* 目标:
* 将点击的卡牌存入相同卡牌的前一位,没有相同的就添加在末尾(直接add添加)
*
*/
int index = 0;//保存当前卡牌应该插入的位置
for (; index <through.size() ; index++) {
JButton card =through.get(index);
System.out.println(card.getName()+" "+name);
if (card.getName().equals(name)){//找到了相同的卡牌
break;
}
}
through.add(index, current);
System.out.println("槽子有:" + through.size());
//存入槽子的牌将其点击事件删除
//获取按钮的所有事件
//只有一个点击事件,删除第一个
current.removeActionListener(current.getActionListeners()[0]);
/**
* 如果插入位置之后隔一个位置的卡牌名字与当前名字一样,说明有三张卡一样
* 将他们删除
*
*/
if (index+2<through.size()){
JButton card = through.get(index+2);
if (card.getName().equals(name)){
JButton c1 = through.remove(index);
JButton c2 = through.remove(index);
JButton c3 = through.remove(index);
panel.remove(c1);
panel.remove(c2);
panel.remove(c3);
panel.updateUI();
}
}
for (int j = 0; j < through.size(); j++) {
through.get(j).setLocation(20 + 64 * j, 640);
}
//重新检查牌堆的压牌情况
checkCovered(cards);
}else {//槽子满了
//画布上提示一个对话框
JOptionPane.showMessageDialog(panel,"槽子满了,游戏结束!");
}
/**
* 卡牌到七应该结束
* 槽子的卡牌不能被点击
* 槽子的卡牌没有被消除
* 没被压住的卡牌应该恢复能被点击的状态
*/
}
});
}
}
}
java简化版羊了个羊
于 2023-03-16 20:20:58 首次发布