Java 二十一点摆动 gui ImageIcon [英] Java blackjack swing gui ImageIcon

查看:25
本文介绍了Java 二十一点摆动 gui ImageIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一周前刚刚开始学习 Java,并完成了我的第一个二十一点计划.我想通过使用 Java swing (ImageIcon) 来增强它,这样每次我画一张卡片时,假设一张 ace 它实际上将 ace 显示为屏幕上或我当前手上的图像.我编写了这个程序来显示我想要的卡片:

import java.awt.FlowLayout;导入 javax.swing.*;公共类 TenCards 扩展 JFrame {公共 TenCards() {ImageIcon[] 图像 = 新的 ImageIcon[10];//在我的包中将卡片图像保存为 1,2,3...10.gif//将它们循环到图像列表中for (int i = 1; i 

<块引用>

现在我的问题是,如果我在另一个文件中有我的二十一点类或程序,我如何组合这两个类以便我可以在我的二十一点程序中使用所有这些图像内容?我尝试复制将我的 TenCards 程序粘贴到 Blackjack 中无济于事:(如果有人能帮我一把或指出我下一步的方向,我将不胜感激.

<小时>

编辑我现在已经包含了我的二十一点程序并删除了另一个,因为 stackoverflow 不会接受两者结合,因为代码太多.

import java.util.ArrayList;导入 java.util.Scanner;导入 javax.swing.JLabel;公开课 myblackjack {公共静态无效主(字符串 [] args){//TODO 自动生成的方法存根Scanner sc = new Scanner(System.in);ArrayList<整数>player = new ArrayList();ArrayList<整数>经销商 = 新的 ArrayList();ArrayList<整数>卡片 = 新的 ArrayList();//按照 Amos 的指示,Ace 为 1for ( int i =1; i <= 10; i++){card.add(i);}//代表 10 的 3 张面卡for (int z = 0; z <= 2; z++){卡片添加(10);}player.add(cards.get((int) (Math.random()*12)));player.add(cards.get((int) (Math.random()*12)));经销商添加(cards.get((int)(Math.random()* 12)));经销商添加(cards.get((int)(Math.random()* 12)));System.out.println("你的牌:"+玩家);boolean moreplayer = true,morecomputer = true;System.out.println("打0停止,打1换另一张牌");int s1 = sc.nextInt();while (moreplayer || morecomputer){整数发牌量 = 0, 玩家量 = 0 ;for (int p = 0; p21 && (moreplayer || morecomputer)){更多播放器 = 假;更多计算机=假;System.out.println(dealer + "dealer");System.out.println("你打败了玩家,电脑赢了!");}if (dealersum>21 && (moreplayer || morecomputer)){更多播放器 = 假;更多计算机=假;System.out.println(dealer + "dealer");System.out.println(玩家+玩家");System.out.println("电脑坏了,玩家赢了!");}if (playersum>dealersum && (!morecomputer && !moreplayer) && (dealersum<=21 && playersum <=21)){System.out.println("玩家获胜!");System.out.println(dealer + "dealer");更多播放器 = 假;更多计算机=假;}if (dealersum>playersum && (!morecomputer && !moreplayer) && (dealersum<=21 && playersum <=21)){System.out.println("经销商获胜");System.out.println(dealer + "dealer");更多播放器 = 假;更多计算机=假;} if ((dealersum == playersum) && (!morecomputer && !moreplayer) && (dealersum<=21 && playersum <=21)){System.out.println("领带!");System.out.println(dealer + "dealer");更多播放器 = 假;更多计算机=假;}sc.close() ;}}}/** 我没有考虑花色,A 值为 1,所有 3 张面卡都值 10 分 */

问候,米

解决方案

简短的回答是...不要...

您正在范式之间转换.我的意思是,您的原始程序正在使用命令行/程序驱动范式,事情以明确定义的顺序发生),而当您尝试转向 GUI/事件驱动范式时.

在事件驱动的环境中,事情可以按任意顺序发生,您需要做好处理的准备.

您将面临的最重要的范式转变之一是将游戏的模型/虚拟视图与游戏的物理/UI 视图分开.

这是一个重要的概念,因为它不仅会使您的程序更易于编写,而且更易于更改.

这在 Model-View-Controller 中有最好的描述 和 Swing 实现了这个概念(松散地)

花时间通读使用 JFC/Swing 创建 GUI,设计您的(游戏)模型,并了解您需要告诉视图哪些内容以及控制器可以更改游戏的哪些内容.

I've just started Java a week ago and have finished my first blackjack program. I want to enhance it by using Java swing (ImageIcon) so that each time I draw a card, let's say an ace it actually displays the ace as an image on the screen or my current hand. I have written this program which displays the cards I want:

import java.awt.FlowLayout;
import javax.swing.*;

public class TenCards extends JFrame {

    public TenCards() {

        ImageIcon[] images = new ImageIcon[10];
        // Saved the card images as 1,2,3...10.gif in my package
        // looped them into the images list
        for (int i = 1; i < images.length; i++) {
            images[i] = new ImageIcon((getClass().getResource(i + ".gif")));

            // testing by displaying 7,8 and 9 cards
        }
        setLayout(new FlowLayout(1, 1, 1));
        add(new JLabel(images[7]));
        add(new JLabel(images[8]));
        add(new JLabel(images[9]));

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new TenCards();
        frame.setTitle("Testing!");
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Now my question is, given I have my blackjack class or program in another file, how do I combine both classes so that I can use all this image stuff in my blackjack program? I tried copy pasting my TenCards program into the Blackjack one to no avail :( If anyone could lend me a hand or point me towards the next step I would really appreciate it.


EDIT I have now included my blackjack program and deleted the other because stackoverflow wouldn't accept both combined because there was too much code.

import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JLabel;

public class myblackjack {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);

    ArrayList<Integer> player = new ArrayList<Integer>();
    ArrayList<Integer> dealer = new ArrayList<Integer>();
    ArrayList<Integer> cards = new ArrayList<Integer>();

    // Ace is 1 as per Amos' instructions
    for ( int i =1; i <= 10; i++){
        cards.add(i);}
    // The 3 face cards representing 10
    for (int z = 0; z <= 2; z++){
        cards.add(10);}


    player.add(cards.get((int) (Math.random()*12)));
    player.add(cards.get((int) (Math.random()*12)));
    dealer.add(cards.get((int) (Math.random()*12)));
    dealer.add(cards.get((int) (Math.random()*12)));
    System.out.println("Your cards: "+player);
    boolean moreplayer = true, morecomputer = true;
    System.out.println("Hit 0 to stop, 1 for another card");
    int s1 = sc.nextInt();
    while (moreplayer || morecomputer){
        int dealersum = 0, playersum = 0 ;

        for (int p = 0; p<dealer.size(); p++){
            dealersum = dealersum + dealer.get(p);
        } if (dealersum<15){
            morecomputer = true;
            dealer.add(cards.get((int) (Math.random()*12)));
                dealersum = dealersum + dealer.get(2);
            }else{ morecomputer = false;

        }if (s1 == 1){

            moreplayer = true;
            player.add(cards.get((int) (Math.random()*12)));
            for (int b = 0; b<player.size(); b++){
                playersum = playersum + player.get(b);


            } System.out.println("Your cards: "+player);
            System.out.println("Hit 0 to stop, 1 for another card");
            s1 = sc.nextInt();
            }else if (s1 == 0){ 
                moreplayer = false;
                for (int b = 0; b<player.size(); b++){
                    playersum = playersum + player.get(b);


                }}if (playersum>21 && (moreplayer || morecomputer)){
                moreplayer = false; morecomputer = false;
                System.out.println(dealer + "dealer");
                System.out.println("You've gone bust player, computer wins!");

            }if (dealersum>21 && (moreplayer || morecomputer)){
                moreplayer = false; morecomputer = false;
                System.out.println(dealer + "dealer");
                System.out.println(player + "player");
                System.out.println("Computer has gone bust, player wins!");

            }if (playersum>dealersum && (!morecomputer && !moreplayer) && (dealersum<=21 && playersum <=21)){
                System.out.println("Player wins!");
                System.out.println(dealer + "dealer");


                moreplayer = false; morecomputer = false;
            }if (dealersum>playersum && (!morecomputer && !moreplayer) && (dealersum<=21   && playersum <=21)){
                System.out.println("Dealer wins");
                System.out.println(dealer + "dealer");

                moreplayer = false; morecomputer = false;
            } if ((dealersum == playersum) && (!morecomputer && !moreplayer) && (dealersum<=21 && playersum <=21)){
                System.out.println("TIE!");
                System.out.println(dealer + "dealer");

                moreplayer = false; morecomputer = false;


        }
            sc.close()  ;

    }

    }
    }/** I didn't consider suits, ace is value 1 and all 3 face cards are worth 10 points */

regards, M

解决方案

The short answer is...don't...

You're shifting between paradigms. What I mean is, you're original program is using the command line/procedural driven paradigm things happen in a well defined order), where as you're attempting to move to a GUI/event driven paradigm.

In a event driven environment, things can happen in any number of order and you need to be prepared to handle it.

One of the most significant paradigm shifts you will face is separating your model/virtual view of the game from the physical/UI view of the game.

This is an important concept as it will not only make your program easier to write, but also to change.

This is best described in Model-View-Controller and Swing implements this concept (loosely)

Take the time to read through Creating a GUI With JFC/Swing, design your model (of the game) and understand what things you will need to tell the view and what things the controller can change about the game.

这篇关于Java 二十一点摆动 gui ImageIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆