Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Integer;
- import java.util.ArrayList;
- import java.util.Comparator;
- /**
- * This class is used to keep track of playing cards.
- *
- * @author (Put your name here.)
- * @version (Put the date here.)
- */
- public class Card
- {
- // instance variables - replace the example below with your own
- private String denom, suit;
- final static int BEFORE = -1;
- final static int EQUAL = 0;
- final static int AFTER = 1;
- /**
- * Card is to be passed in as a denomination and suit
- */
- public Card(String description)
- {
- description = description.toUpperCase();
- if( description.length() == 2)
- {
- suit = description.substring(1);
- denom = description.substring(0,1);
- } else if(description.length() == 3) {
- suit = description.substring(2);
- denom = description.substring(0,2);
- } else {
- System.out.print("Error: An invalid card code was given.");
- }
- }
- public String getDenom() {return this.denom;}
- public String getSuit() {return this.suit;}
- /**
- * This will give a string that states a discription of the card.
- * @return Card discription as a string.
- */
- public String getDis()
- {
- //get the description
- String denomMessage = denom;
- if(denom.equals("A"))
- denomMessage = "Ace";
- else if(denom.equals("K"))
- denomMessage = "King";
- else if(denom.equals("Q"))
- denomMessage = "Queen";
- else if(denom.equals("J"))
- denomMessage = "Jack";
- //get the suit
- String suitMessage = suit;
- if(suit.equals("S"))
- suitMessage = "Spades";
- else if(suit.equals("D"))
- suitMessage = "Dimonds";
- else if(suit.equals("C"))
- suitMessage = "Clubs";
- else if(suit.equals("H"))
- suitMessage = "Hearts";
- else
- suitMessage = "There was a problem";
- return denomMessage + " of " + suitMessage;
- }
- /**
- * This was written for the purpose of helping to select a card image.
- * @return clubs are 1, hearts are 2, spades are 3, diamonds are 4
- */
- public int numSuit()
- {
- int value = 0;
- if(suit.equals("C"))
- value = 1;
- else if(suit.equals("H"))
- value = 2;
- else if(suit.equals("S"))
- value = 3;
- else if(suit.equals("D"))
- value = 4;
- return value;
- }
- /**
- * This class was written for the purpose of selecting a card image
- * @return ace is a 1, jack is a 11, queen is a 12, king is a 13
- */
- public int numDenom()
- {
- int value = 0;
- if(denom.equals("A"))
- value = 1;
- else if(denom.equals("J"))
- value = 11;
- else if(denom.equals("Q"))
- value = 12;
- else if(denom.equals("K"))
- value = 13;
- else
- value = Integer.parseInt(denom);
- return value;
- }
- /**
- * Are the two cards the same suit and demonination?
- * @return true or false
- */
- public boolean equals(Card a)
- {
- if(denom.equals(a.denom) && suit.equals(a.suit))
- return true;
- else
- return false;
- }
- /**
- * Compare Denom
- */
- public int denomCompareTo(Card a)
- {
- int result = EQUAL;
- if(a!=null) {
- if(this.numDenom() < a.numDenom()) result = BEFORE;
- else if(this.numDenom() > a.numDenom()) result = AFTER;
- }
- return result;
- }
- /**
- * Compare Suit
- */
- public int suitCompareTo(Card a)
- {
- int result = EQUAL;
- if(this.numSuit() < a.numSuit()) result = BEFORE;
- else if(this.numSuit() > a.numSuit()) result = AFTER;
- return result;
- }
- /*Comparator for sorting the list by suit*/
- public static Comparator<Card> SuitComparator = new Comparator<Card>() {
- public int compare(Card c1, Card c2) {
- //ascending order
- return c1.suitCompareTo(c2);
- //descending order
- }};
- /*Comparator for sorting the list by denom*/
- public static Comparator<Card> DenomComparator = new Comparator<Card>() {
- public int compare(Card c1, Card c2) {
- /*For ascending order*/
- return c1.denomCompareTo(c2);
- /*For descending order*/
- //rollno2-rollno1;
- }};
- public static ArrayList<Card> sortBySuit(ArrayList<Card> cardList, String sortSelected)
- {
- ArrayList<Card> selectedSuitList = new ArrayList<Card>();
- ArrayList<Card> restSuitList = new ArrayList<Card>();
- String suitKey = sortSelected.substring(0,1);
- for(int i=0; i<cardList.size(); i++) {
- Card currCard = cardList.get(i);
- String currSuit = currCard.getSuit();
- if(currSuit.equalsIgnoreCase(suitKey)){
- selectedSuitList.add(currCard);
- }else {
- restSuitList.add(currCard);
- }
- }
- cardList = new ArrayList<Card>(selectedSuitList);
- cardList.addAll(restSuitList);
- return cardList;
- }
- }
- import java.awt.*;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.applet.*;
- import java.awt.image.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /**
- * This applet is currently working. It will run in a webpage. It will take in card codes such as
- * 2h for the two of heart and "paint" the image of five cards when all five cards
- * card coding entered into them. Your assignment will be to write the part of the code
- * that will sort the cards once they have been entered so that the will be "painted" from smallest
- * to largest denomination regardless of suit as long "None" is selected in the drop down box. In
- * the event one of the suits is selected that suit will be placed in order first then followed by
- * the rest of the cards in order. To complete the assignment you should read through this class' code
- * but you will only need to change the section that state that you should change it and possibly
- * the card class.
- *
- * @author (Put your name here.)
- * @version (Put the date here.)
- */
- public class CardApplet extends Applet {
- Image ic1,ic2,ic3,ic4,ic5;
- Card c1,c2,c3,c4,c5;
- private TextField cardIn1,cardIn2,cardIn3,cardIn4,cardIn5;
- private String message;
- private Button enter,sort;
- private ButtonListener buttonListen;
- private Choice trump;
- static final int CARD_WIDTH = 73;
- static final int CARD_HEIGHT = 98;
- private ArrayList<Card> cardList;
- private ArrayList<Card> cards;
- /**
- * This is called an inner class as it is a class writen inside of another class. It is here so
- * that the buttons will be able to trigger an event.
- */
- class ButtonListener implements ActionListener
- {
- /**
- * The name of this method is important and should not be changed. This will take in the
- * "action" of a button being pushed and store reference to it in the object variable e.
- */
- public void actionPerformed(ActionEvent e)
- {
- String action = e.paramString();
- if( action.indexOf("Enter") >= 0)
- {
- //get text
- String text1 = cardIn1.getText();
- String text2 = cardIn2.getText();
- String text3 = cardIn3.getText();
- String text4 = cardIn4.getText();
- String text5 = cardIn5.getText();
- //Get rid of whitespace before and after string
- text1 = text1.trim();
- text2 = text2.trim();
- text3 = text3.trim();
- text4 = text4.trim();
- text5 = text5.trim();
- message = "Cards Entered";
- //setup cards and card images
- c1 = new Card(text1);
- ic1 = getCardImage(c1);
- c2 = new Card(text2);
- ic2 = getCardImage(c2);
- c3 = new Card(text3);
- ic3 = getCardImage(c3);
- c4 = new Card(text4);
- ic4 = getCardImage(c4);
- c5 = new Card(text5);
- ic5 = getCardImage(c5);
- // Add cards to cardList
- cardList = new ArrayList<Card>();
- cardList.add(c1);
- cardList.add(c2);
- cardList.add(c3);
- cardList.add(c4);
- cardList.add(c5);
- Collections.sort(cardList, Card.DenomComparator);
- //this method call is to this class and tells the applet to follow the code of paint again.
- repaint();
- }
- else if( action.indexOf("Sort") >= 0)
- {
- //ADD YOUR CODE HERE.
- String sortSelected = trump.getSelectedItem();
- if("None".equals(sortSelected)) {
- Collections.sort(cardList, Card.DenomComparator);
- mapCardToImage();
- }else {
- cardList = Card.sortBySuit(cardList, sortSelected);
- mapCardToImage();
- }
- //DO NOT CHANGE CODE PAST THIS LINE.
- message = "Sorted";
- repaint();
- }
- }
- } //end of inner class.
- /**
- * This method is called when the applet is first started. It will setup the layout of the applet.
- */
- public void init() {
- //This is the text that prints in the gray box towards the bottem of the applet.
- message="Let us get started ";
- //Sets the back ground color of the the applet
- setBackground(Color.GREEN);
- // Set default layout manager
- setLayout(new FlowLayout() );
- //setup textboxes for entering in cards
- cardIn1 = new TextField("Enter",4);
- add(cardIn1);
- cardIn2 = new TextField("cards ",4);
- add(cardIn2);
- cardIn3 = new TextField("using",4);
- add(cardIn3);
- cardIn4 = new TextField("Chap 5",4);
- add(cardIn4);
- cardIn5 = new TextField("coding",4);
- add(cardIn5);
- //place buttons
- buttonListen = new ButtonListener();
- enter = new Button("Enter");
- enter.addActionListener(buttonListen);
- add(enter);
- sort = new Button("Sort");
- sort.addActionListener(buttonListen);
- add(sort);
- //setup dropdown
- trump = new Choice();
- trump.addItem("None");
- trump.addItem("Hearts");
- trump.addItem("Diamonds");
- trump.addItem("Spades");
- trump.addItem("Clubs");
- add(trump);
- //Since the card object variables are null each image object variable
- //will hold reference to a card back image.
- ic1 = getCardImage(c1);
- ic2 = getCardImage(c2);
- ic3 = getCardImage(c3);
- ic4 = getCardImage(c4);
- ic5 = getCardImage(c5);
- }
- /*
- * This class is used to place graphics on an applet.
- */
- public void paint(Graphics g) {
- //places cards on applet
- int linePos = 70;
- g.drawImage(ic1,19,linePos,this);
- g.drawImage(ic2,19+CARD_WIDTH,linePos,this);
- g.drawImage(ic3,19+2*CARD_WIDTH,linePos,this);
- g.drawImage(ic4,19+3*CARD_WIDTH,linePos,this);
- g.drawImage(ic5,19+4*CARD_WIDTH,linePos,this);
- // simple text displayed on applet
- g.setColor(Color.lightGray);
- g.draw3DRect(2, 175, 200, 20, true);
- g.fillRect(2, 175, 200, 20);
- g.setColor(Color.black);
- g.drawString(message, 4, 190);
- }
- /**
- * This will select either the correct portion of the cards image based on the suit and denomination or
- * the card back image.
- * @param a The card object holds the suit and denomination in state.
- * @return It returns an image object variable with holds reference to a image that was created for a card that was passed in.
- * @throws MalformedURLException
- */
- public Image getCardImage(final Card a)
- {
- int cardDenom,cardSuit;
- Image playingCards = null;
- ImageFilter cardFilter;
- ImageProducer cardProducer;
- if( a == null)
- {
- playingCards = getImage(getCodeBase(), "cardBack.png");
- }else{
- playingCards = getImage(getCodeBase(),"cards.png");
- cardDenom = (a.numDenom()*CARD_WIDTH)- CARD_WIDTH;
- cardSuit = (a.numSuit()*CARD_HEIGHT) - CARD_HEIGHT;
- cardFilter = new CropImageFilter(cardDenom,cardSuit,CARD_WIDTH,CARD_HEIGHT);
- cardProducer = new FilteredImageSource(playingCards.getSource(),cardFilter);
- playingCards = createImage(cardProducer);
- }
- return playingCards;
- }
- private void mapCardToImage() {
- c1 = cards.get(0);
- ic1 = getCardImage(c1);
- c2 = cards.get(1);
- ic2 = getCardImage(c2);
- c3 = cards.get(2);
- ic3 = getCardImage(c3);
- c4 = cards.get(3);
- ic4 = getCardImage(c4);
- c5 = cards.get(4);
- ic5 = getCardImage(c5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement