Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Eclipse Understand the Classes and Problem We wish to move our Card classes

ID: 3668468 • Letter: J

Question

Java Eclipse

Understand the Classes and Problem

We wish to move our Card classes from the realm of console apps to that of GUI apps. We'll do this in stages.

Read and display Card pictures - Read .gif image files as Icons, and attach them to JLabels that we can display on a JFrame.

Encapsulate the Card Icons in a class GUICard - Once we debug imagery for cards, above, we can move it into its own class, GUICard.

Create a CardTable class - This JFrame class will embody the JPanels and Layout(s) needed for our application. This is where all the cards and controls will be placed. (It plays the same role as class Calculator of your RPN lecture.)

The first phase (item 1) will allow you to debug the problem of reading the .gif files and displaying them on a JFrame without any excess logic or class complexity. The second phase (items 2 and 3) will let you turn what you did in the first phase and what you read in the modules into a multi-class project.

The three bullets will be done in two phases.  You should hand in two complete programs, one for each phase. You may attach two files or place them all in one file, well delimited. The main, public class of each program must be named Foothill so I can easily run it, and other classes must not be public.  You do not submit any runs - I will run your programs.

Download the Card .gifs

The GNU Free Software Foundation provides code and images for playing-cards that are public domain. I prepared a .zip file which contains a .gif for every card with some minor name changes. Download it here:

Card Images Download .https://www.fgamedia.org/faculty/loceff/cs_courses/cs_1b/resources/images_for_card_assignment.zip

After you unzip them, you will have a single folder called images that contains all the .gif files. Move that folder to your [java workspace]/[project name] directory. If your project is named Assignment_5, and your Eclipse workspace is named workspace, then move images folder to workspace/Assignment_5. Since your program considers Assignment_5 to be the root directory, all .gif files can be referenced from your program using names like "images/3S.gif" for, say, 3 of spades.

In addition to the 52 standard cards, there are four jokers and a card-back image which can be used to display dealer or other player cards that you don't want the user to see yet.

(The card on the far right is joker 2, not a jack.)

Phase 1: Reading and Displaying the .gif Files

At the end of Lesson 5A.7, you learned how to instantiate an Icon object to represent any .gif, .jpg or other image file on your disk and then place that Icon on a JLabel. In Phase 1, we simply create an array of 57 JLabels, attach the 57 .gif files to them, and display the labels, unstructured, in a single JFrame. Here is a possible main() that you can use as a starting point. You don't have to use my main() but yours should be no longer or more complicated. Look at your images folder to see the names of each .gif file: you have to be able to construct their names in a loop (you'll get a 0 on the assignment if you list all 52 cards named literally). Where is the card-back image stored? Find it in the images folder.

Hand in a main class that accomplishes this as the first of two programs.

Phase 2: Encapsulating Layout and Icons into CardTable and GUICard Classes

The second part creates a separate CardTable class that extends JFrame. This class will control the positioning of the panels and cards of the GUI. We also create a new GUICardclass that manages the reading and building of the card image Icons. As a result, some of the machinery and statics that we debugged in the first phase of the main, Foothill class, will be moved into one or the other of these two new classes.

CardTable Class (subclassed from JFrame)

Include four members:

These members are needed is to establish the grid layout for the JPanels, the organization of which depends on how many cards and players will be displayed. We'll provide an accessor, but no mutator (other than the constructor) for these members. Here are the public instance methods:

CardTable(String title, int numCardsPerHand, int numPlayers) - The constructor filters input, adds any panels to the JFrame, and establishes layouts according to the general description below.

Accessors for the two instance members.

Suggestion: Use three JPanels, one for each hand (player-bottom and computer-top) and a middle "playing" JPanel. The client (below) will generate the human's cards at random and those will be visible in the bottom JPanel, while the computer's cards will be chosen (again, by the client) to be all back-of-card images in the top JPanel. The middle JPanel will display cards that are "played" by the computer and human during the conflict. Let's assume that each player plays one card per round, so for a 2-person game (computer + human) there will be exactly two cards played in the central region per round of battle. My client chose a joker for the two central cards, just so we would have something to see in the playing region. No game is being played in this assignment, so the cards to be displayed in the center are immaterial.

GUICard Class

This class is the benefactor of most of the GUI machinery we tested in Phase 1. It will read the image files and store them in a static Icon array. Rather than a 1-D array of Phase 1 (if you followed my earlier outline), this will be a 2-D array to facilitate addressing the value and suit of a Card in order get its Icon. While simple in principle (just read the Icons and store them in an array for client use), the details are subtle. We have to be able to convert from chars and suits to ints, and back again, in order to find the Icon for any given Card object. The overview of the class data and methods, shown below, will suggest the right approach and should take the mystery out of this class.

Static Members

The 52 (+ 4 jokers opptional) Icons will be read and stored into the iconCards[][] array. The card-back image in the iconBack member. None of these data need to be stored more than once, so this is a class without instance data. This class is used is to produce an image icon when the client needs one.

Helper Static Arrays

The first two arrays are the ones we used in phase 1, but moved into this class. The third is a new array used to convert ints into actual enum Suits, as you'll see.

Static Methods

To begin, we need a method that generates the image Icon array from files:

static void loadIcons() - the code for this was fundamentally done in Phase 1. The difference here is that we are storing the Icons in a 2-D array. So you have to use a nestedfor-loop, suits and values, to generate the 2-D index for each Icon in the array. Another suggestion: I'd like you to not require the client to call this method. Think about where you would need to call it and how can you avoid having the method reload the icons after it has already loaded them once. The hint is in the static boolean iconsLoaded = false;, above.  Hint:  Call this method any time you might need an Icon, but make sure that it loads the entire array the first time it is called, and does nothing any later time.

The use of loadIcons() should be clearer after you take a look at the primary public method offered by this class:

static public Icon getIcon(Card card) - This method takes a Card object from the client, and returns the Icon for that card. It would be used when the client needs to instantiate or change a JLabel. Here is what the getIcon() method definition might look like:

I just realized I wrote the entire method for you. Well, there are three method calls packed into this definition, and those, I did not write. Now you see where loadIcons() is called, and you see why we don't want it to do anything except return if the cards are already loaded. There is another method that returns the card-back image:

static public Icon getBackCardIcon() - this one is even simpler than getIcon().

The above three methods comprise the essential part of the GUICard class. Everything else is support for these three, so you can work off my implied suggestions, or you can build the class from scratch as you wish. Just make sure you are efficient.

Main Foothill Client

Static Methods

This class now only has the main() and one other static helper method:

generateRandomCard() - This method uses Java's Math.random() utility as a helper, and from it, generates a random card. We don't care if it generates the same card more than once - that's fine. There are some public helpers, now in the GUICard class, that will also be useful -- invoke them as needed to make this a short method:  turnIntIntoSuit() andturnIntIntoCardValueChar().

main() for Phase 2

The main, Foothill, class needs to define the specific JLabel arrays that will go into each of CardTable's JPanels. You will need NUM_CARDS_PER_HAND JLabels for the player and the computer (each), even though the computer only uses one Icon (back-of-card). We also want two Icon JLabels for the central JPanel (these are the two cards played by computer and human, each turn). But we also need to some text below each of the two center icons to we know who played which card ("Computer" or "You"), so, we'll really needfour labels in this central play JPanel : two for card images and two for text "Computer" and "You". Since we want the text directly below the icon, one way to do this is to make your central playing panel a 2x2 Grid Layout, where the top two positions will be images and the bottom two will be text that describe the images. Hint: to center text in a label, use

The net result should be cards we can see (our hand) in the lower JPanel , cards that we can't see -- except for the card backs -- in the upper JPanel (the computer's hand) and a central playing region which would represent two cards, one each played by the user and the computer. These two cards depend on what game we are playing, the rules, and the goal. Based on these two cards played, either we or the computer win that round and then we go on to the next round. For this assignment, we don't worry about strategy or rules or winning -- we just want to see two cards in the central JPanel so we know they are correctly positioned for later program development. Here's a partial picture of a basic solution:

You job is to simply produce this output using the classes and methods suggested. Here is an idea for a main() that you can use to get started:

Explanation / Answer

CardTable myCardTable = new CardTable("CS 1B CardTable", NUM_CARDS_PER_HAND, NUM_PLAYERS);
myCardTable.setSize(800, 600);
myCardTable.setLocationRelativeTo(null);
myCardTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myCardTable.setVisible(true);
myCardTable.getGamePanel().setBorder(new TitledBorder("Playing Area"));
myCardTable.getGamePanel().add(playerCard, JLabel.CENTER);
myCardTable.getGamePanel().add(computerCard, JLabel.CENTER);
for(int i = 0; i < NUM_PLAYERS; i++)
{
JLabel temp = new JLabel(GUICard.getIcon(generateRandomCard()));
myCardTable.getGamePanel().add(temp);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote