羊了个羊基础版本
笔者第一篇博客,今日开始正式学习Java,目前在达内教育进行培训,后期会持续更新所学新内容
此篇内容是前期训练营的成果(五天)
羊了个羊实际图
实际展示的效果
基础版本只具备基本内容,不具有复活和打乱重构
创建牌的方法
16张种类的card,主方法中调用了6次,所以一共是96张card
/*
* 定义一个方法,封装牌的创建过程,返回值是创建好的全部牌
* round 参数代表轮次,本案例中需要使用3的倍数
* @return 一堆牌(按钮)
* */
public static ArrayList<JButton> createCards(int round){
String[] names = {"刷子","剪刀","叉子","奶瓶","干草",
"手套","树桩","棉花","毛线","水桶"
,"火","玉米","白菜","草","萝卜","铃铛",};
ArrayList<JButton> cards = new ArrayList<>();
for(int i=0; i<round; i++){
for(int j=0; j<names.length; j++){
String filename = "images/" + names[j] + ".png";
String filename2 = "images/" + names[j] + "2.png";
JButton card = new JButton(new ImageIcon(filename));
card.setDisabledIcon(new ImageIcon(filename2));
card.setSize(59,66);
card.setName(names[j]);
cards.add(card);
}
}
return cards;
}
显示牌的方法
/*
* 打印全部的牌
* @param cards 牌的集合
* */
public static void print(ArrayList<JButton> cards){
for(int i=0; i<cards.size(); i++){
JButton card = cards.get(i);
System.out.print(card.getName()+" ");
if((i+1)%7 == 0){
System.out.println();
}
}
System.out.println();
}
摆放牌
一共96张牌摆放,读者可以更改此处来更改牌面布局
/*
* 摆放牌
* panel 摆放牌的面板
* cards 牌的集合
* start 开始,是只从那个cards位置开始摆放 0 42
* rows 行数
* cols 列数
* x, y 是第一张牌左上角的位置
* deal(panel, cards, 0, 6, 7, 30, 100)
* */
public static void deal(JPanel panel, ArrayList<JButton> cards,
int start, int rows, int cols, int x, int y){
for(int i=0; i<rows * cols; i++){
/*
* i+72
* 0+72 72 < 96 true
* 1+72 73 < 96 true
* ...
* 23+72 95 < 96 true
* 24+72 96 < 96 false
* */
// //96 == 96 到达cards
if (i+start == cards.size()){
//到达cards末尾就结束
return;
}
int x1 = x + i % cols * 59;
int y1 = y + i / cols * 66;
JButton card = cards.get(i + start);
card.setLocation(x1, y1);
card.setEnabled(false);
panel.add(card,0);
}
}
检查牌是否在上层
/*
* 检查每个牌是否被压住,如果压住就设置为disable,在上层的牌设置为enable
* */
public static void checkCards(ArrayList<JButton> cards){
for(int i=0; i<cards.size(); i++){
JButton card1 = cards.get(i);
card1.setEnabled(true);
for (int j=i+1; j<cards.size(); j++){
JButton card2 = cards.get(j);
int x1 = card1.getX() - 59;
int x2 = card1.getX() + 59;
int y1 = card1.getY() - 66;
int y2 = card1.getY() + 66;
if ((card2.getX() > x1 && card2.getX() < x2) &&
(card2.getY() > y1 && card2.getY() < y2)){
card1.setEnabled(false);
}
}
}
}
为每个牌的点击添加事件
这个槽子就是图片下方的框框
/*
* 为每个牌的点击添加事件
* @param cards 全部的牌
* @param though 槽子
* */
//为每个牌添加点击事件
public static void addAction(ArrayList<JButton> cards,
ArrayList<JButton> though){
for(int i=0; i<cards.size(); i++){
JButton card = cards.get(i);
card.addActionListener(e -> {
JButton selected = (JButton) e.getSource();
System.out.println("点击: " + selected.getName());
if(though.size() == 7){
// show 显示
// message 消息
// dialog 对话框
JOptionPane.showMessageDialog(selected,"槽子满了!");
return;
}
// // 让进了槽子里的按钮不可被调用,但是屏幕还是会输出
// if(though.contains(selected)){
// return;
// }
cards.remove(selected);
// 让进了槽子里的按钮不可被点击
ActionListener l = selected.getActionListeners()[0];
selected.removeActionListener(l);
selected.setLocation(20+though.size()*63,642);
// though.add(selected);
checkCards(cards);
updateThough(though, selected);
});
}
}
更新槽子中牌的显示
/*
* 更新槽子中牌的显示
* */
public static void updateThough(ArrayList<JButton> though,
JButton selected){
//1.对槽子中的牌进行排序
// though.sort((c1,c2)->c1.getName().compareTo(c2.getName()));
int found = -1;
for(int i=0; i<though.size(); i++){
JButton card = though.get(i);
if (card.getName().equals(selected.getName())){
though.add(i, selected);
found = i;
break;
}
}
if (found == -1){
though.add(selected);
}
//2.消除连续的3张牌
if(found != -1){
//如果found不是-1,也就是中间插入,时候开始消除
//当前位置开始,到最后至少有3张牌
if(though.size()-found >= 3){
JButton nextNextCard = though.get(found+2);
if(selected.getName().equals(nextNextCard.getName())){
// Parent 父,是当前按钮所在的面板
JPanel panel = (JPanel)selected.getParent();
panel.remove(though.remove(found));
panel.remove(though.remove(found));
panel.remove(though.remove(found));
//重新绘制panel,解决删除的残影
panel.repaint();
}
}
}
//3.根据排序的结果,重新设置 每个牌的 Location
for(int i=0; i<though.size(); i++){
JButton card = though.get(i);
int x = 25 + 62 * i;
int y = 642;
card.setLocation(x, y);
}
}
主方法
主方法中的1~5步都是在调用前面发的方法,否则主方法过于臃肿
public class yangend {
public static void main(String[] args) {
JFrame frame = new JFrame("羊了个羊");
JPanel panel = new JPanel();
JLabel background = new JLabel(new ImageIcon("images/背景.jpg"));
background.setSize(480,800);
background.setLocation(0,0);
panel.setLayout(null);
panel.add(background);
ArrayList<JButton> though = new ArrayList<>();
//1.创建一副牌
ArrayList<JButton> cards= createCards(6);
print(cards);
//2.洗牌
Collections.shuffle(cards);
System.out.println();
print(cards);
//3.摆放
deal(panel, cards, 0,6,7,30,100);
deal(panel, cards, 42,5,6,60,133);
deal(panel, cards,42+30, 5, 7, 30,166);
//4.检查是否在最上层
checkCards(cards);
//5.为每个牌增加动作事件
addAction(cards, though);
frame.add(panel);
frame.setSize(495,835);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
完整案例
图片附件
链接:https://pan.baidu.com/s/1d2-4rcIK8MXceZXrpwT44Q
提取码:ocsv
package DAY05;
import com.sun.javafx.stage.FocusUngrabEvent;
import com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory;
import com.sun.scenario.effect.impl.sw.java.JSWBlend_COLOR_BURNPeer;
import javax.sound.midi.Soundbank;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collections;
public class yangend {
public static void main(String[] args) {
JFrame frame = new JFrame("羊了个羊");
JPanel panel = new JPanel();
JLabel background = new JLabel(new ImageIcon("images/背景.jpg"));
background.setSize(480,800);
background.setLocation(0,0);
panel.setLayout(null);
panel.add(background);
ArrayList<JButton> though = new ArrayList<>();
//1.创建一副牌
ArrayList<JButton> cards= createCards(6);
print(cards);
//2.洗牌
Collections.shuffle(cards);
System.out.println();
print(cards);
//3.摆放
deal(panel, cards, 0,6,7,30,100);
deal(panel, cards, 42,5,6,60,133);
deal(panel, cards,42+30, 5, 7, 30,166);
//4.检查是否在最上层
checkCards(cards);
//5.为每个牌增加动作事件
addAction(cards, though);
frame.add(panel);
frame.setSize(495,835);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/*
* 为每个牌的点击添加事件
* @param cards 全部的牌
* @param though 槽子
* */
//为每个牌添加点击事件
public static void addAction(ArrayList<JButton> cards,
ArrayList<JButton> though){
for(int i=0; i<cards.size(); i++){
JButton card = cards.get(i);
card.addActionListener(e -> {
JButton selected = (JButton) e.getSource();
System.out.println("点击: " + selected.getName());
if(though.size() == 7){
// show 显示
// message 消息
// dialog 对话框
JOptionPane.showMessageDialog(selected,"槽子满了!");
return;
}
// // 让进了槽子里的按钮不可被调用,但是屏幕还是会输出
// if(though.contains(selected)){
// return;
// }
cards.remove(selected);
// 让进了槽子里的按钮不可被点击
ActionListener l = selected.getActionListeners()[0];
selected.removeActionListener(l);
selected.setLocation(20+though.size()*63,642);
// though.add(selected);
checkCards(cards);
updateThough(though, selected);
});
}
}
/*
* 更新槽子中牌的显示
* */
public static void updateThough(ArrayList<JButton> though,
JButton selected){
//1.对槽子中的牌进行排序
// though.sort((c1,c2)->c1.getName().compareTo(c2.getName()));
int found = -1;
for(int i=0; i<though.size(); i++){
JButton card = though.get(i);
if (card.getName().equals(selected.getName())){
though.add(i, selected);
found = i;
break;
}
}
if (found == -1){
though.add(selected);
}
//3.消除连续的3张牌
if(found != -1){
//如果found不是-1,也就是中间插入,时候开始消除
//当前位置开始,到最后至少有3张牌
if(though.size()-found >= 3){
JButton nextNextCard = though.get(found+2);
if(selected.getName().equals(nextNextCard.getName())){
// Parent 父,是当前按钮所在的面板
JPanel panel = (JPanel)selected.getParent();
panel.remove(though.remove(found));
panel.remove(though.remove(found));
panel.remove(though.remove(found));
//重新绘制panel,解决删除的残影
panel.repaint();
}
}
}
//2.根据排序的结果,重新设置 每个牌的 Location
for(int i=0; i<though.size(); i++){
JButton card = though.get(i);
int x = 25 + 62 * i;
int y = 642;
card.setLocation(x, y);
}
}
/*
* 检查每个牌是否被压住,如果压住就设置为disable,在上层的牌设置为enable
* */
public static void checkCards(ArrayList<JButton> cards){
for(int i=0; i<cards.size(); i++){
JButton card1 = cards.get(i);
card1.setEnabled(true);
for (int j=i+1; j<cards.size(); j++){
JButton card2 = cards.get(j);
int x1 = card1.getX() - 59;
int x2 = card1.getX() + 59;
int y1 = card1.getY() - 66;
int y2 = card1.getY() + 66;
if ((card2.getX() > x1 && card2.getX() < x2) &&
(card2.getY() > y1 && card2.getY() < y2)){
card1.setEnabled(false);
}
}
}
}
/*
* 摆放牌
* panel 摆放牌的面板
* cards 牌的集合
* start 开始,是只从那个cards位置开始摆放 0 42
* rows 行数
* cols 列数
* x, y 是第一张牌左上角的位置
* deal(panel, cards, 0, 6, 7, 30, 100)
* */
public static void deal(JPanel panel, ArrayList<JButton> cards,
int start, int rows, int cols, int x, int y){
for(int i=0; i<rows * cols; i++){
/*
* i+72
* 0+72 72 < 96 true
* 1+72 73 < 96 true
* ...
* 23+72 95 < 96 true
* 24+72 96 < 96 false
* */
// //96 == 96 到达cards
if (i+start == cards.size()){
//到达cards末尾就结束
return;
}
int x1 = x + i % cols * 59;
int y1 = y + i / cols * 66;
JButton card = cards.get(i + start);
card.setLocation(x1, y1);
card.setEnabled(false);
panel.add(card,0);
}
}
/*
* 打印全部的牌
* @param cards 牌的集合
* */
public static void print(ArrayList<JButton> cards){
for(int i=0; i<cards.size(); i++){
JButton card = cards.get(i);
System.out.print(card.getName()+" ");
if((i+1)%7 == 0){
System.out.println();
}
}
System.out.println();
}
/*
* 定义一个方法,封装牌的创建过程,返回值是创建好的全部牌
* round 参数代表轮次,本案例中需要使用3的倍数
* @return 一堆牌(按钮)
* */
public static ArrayList<JButton> createCards(int round){
String[] names = {"刷子","剪刀","叉子","奶瓶","干草",
"手套","树桩","棉花","毛线","水桶"
,"火","玉米","白菜","草","萝卜","铃铛",};
ArrayList<JButton> cards = new ArrayList<>();
for(int i=0; i<round; i++){
for(int j=0; j<names.length; j++){
String filename = "images/" + names[j] + ".png";
String filename2 = "images/" + names[j] + "2.png";
JButton card = new JButton(new ImageIcon(filename));
card.setDisabledIcon(new ImageIcon(filename2));
card.setSize(59,66);
card.setName(names[j]);
cards.add(card);
}
}
return cards;
}
}