Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. import java.lang.Integer;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4.  
  5. /**
  6. * This class is used to keep track of playing cards.
  7. *
  8. * @author (Put your name here.)
  9. * @version (Put the date here.)
  10. */
  11.  
  12. public class Card
  13. {
  14. // instance variables - replace the example below with your own
  15. private String denom, suit;
  16.  
  17. final static int BEFORE = -1;
  18. final static int EQUAL = 0;
  19. final static int AFTER = 1;
  20.  
  21. /**
  22. * Card is to be passed in as a denomination and suit
  23. */
  24. public Card(String description)
  25. {
  26. description = description.toUpperCase();
  27.  
  28. if( description.length() == 2)
  29. {
  30. suit = description.substring(1);
  31. denom = description.substring(0,1);
  32. } else if(description.length() == 3) {
  33. suit = description.substring(2);
  34. denom = description.substring(0,2);
  35. } else {
  36. System.out.print("Error: An invalid card code was given.");
  37. }
  38. }
  39.  
  40. public String getDenom() {return this.denom;}
  41. public String getSuit() {return this.suit;}
  42.  
  43. /**
  44. * This will give a string that states a discription of the card.
  45. * @return Card discription as a string.
  46. */
  47. public String getDis()
  48. {
  49. //get the description
  50. String denomMessage = denom;
  51. if(denom.equals("A"))
  52. denomMessage = "Ace";
  53. else if(denom.equals("K"))
  54. denomMessage = "King";
  55. else if(denom.equals("Q"))
  56. denomMessage = "Queen";
  57. else if(denom.equals("J"))
  58. denomMessage = "Jack";
  59.  
  60.  
  61. //get the suit
  62. String suitMessage = suit;
  63. if(suit.equals("S"))
  64. suitMessage = "Spades";
  65. else if(suit.equals("D"))
  66. suitMessage = "Dimonds";
  67. else if(suit.equals("C"))
  68. suitMessage = "Clubs";
  69. else if(suit.equals("H"))
  70. suitMessage = "Hearts";
  71. else
  72. suitMessage = "There was a problem";
  73.  
  74. return denomMessage + " of " + suitMessage;
  75. }
  76.  
  77. /**
  78. * This was written for the purpose of helping to select a card image.
  79. * @return clubs are 1, hearts are 2, spades are 3, diamonds are 4
  80. */
  81. public int numSuit()
  82. {
  83. int value = 0;
  84. if(suit.equals("C"))
  85. value = 1;
  86. else if(suit.equals("H"))
  87. value = 2;
  88. else if(suit.equals("S"))
  89. value = 3;
  90. else if(suit.equals("D"))
  91. value = 4;
  92.  
  93. return value;
  94. }
  95.  
  96. /**
  97. * This class was written for the purpose of selecting a card image
  98. * @return ace is a 1, jack is a 11, queen is a 12, king is a 13
  99. */
  100. public int numDenom()
  101. {
  102. int value = 0;
  103. if(denom.equals("A"))
  104. value = 1;
  105. else if(denom.equals("J"))
  106. value = 11;
  107. else if(denom.equals("Q"))
  108. value = 12;
  109. else if(denom.equals("K"))
  110. value = 13;
  111. else
  112. value = Integer.parseInt(denom);
  113.  
  114. return value;
  115. }
  116.  
  117.  
  118. /**
  119. * Are the two cards the same suit and demonination?
  120. * @return true or false
  121. */
  122. public boolean equals(Card a)
  123. {
  124. if(denom.equals(a.denom) && suit.equals(a.suit))
  125. return true;
  126. else
  127. return false;
  128. }
  129.  
  130. /**
  131. * Compare Denom
  132. */
  133. public int denomCompareTo(Card a)
  134. {
  135. int result = EQUAL;
  136.  
  137. if(a!=null) {
  138. if(this.numDenom() < a.numDenom()) result = BEFORE;
  139. else if(this.numDenom() > a.numDenom()) result = AFTER;
  140. }
  141.  
  142. return result;
  143. }
  144.  
  145. /**
  146. * Compare Suit
  147. */
  148. public int suitCompareTo(Card a)
  149. {
  150. int result = EQUAL;
  151.  
  152. if(this.numSuit() < a.numSuit()) result = BEFORE;
  153. else if(this.numSuit() > a.numSuit()) result = AFTER;
  154.  
  155. return result;
  156. }
  157.  
  158.  
  159. /*Comparator for sorting the list by suit*/
  160. public static Comparator<Card> SuitComparator = new Comparator<Card>() {
  161.  
  162. public int compare(Card c1, Card c2) {
  163.  
  164. //ascending order
  165. return c1.suitCompareTo(c2);
  166.  
  167. //descending order
  168. }};
  169.  
  170. /*Comparator for sorting the list by denom*/
  171. public static Comparator<Card> DenomComparator = new Comparator<Card>() {
  172.  
  173. public int compare(Card c1, Card c2) {
  174.  
  175. /*For ascending order*/
  176. return c1.denomCompareTo(c2);
  177.  
  178. /*For descending order*/
  179. //rollno2-rollno1;
  180. }};
  181.  
  182. public static ArrayList<Card> sortBySuit(ArrayList<Card> cardList, String sortSelected)
  183. {
  184. ArrayList<Card> selectedSuitList = new ArrayList<Card>();
  185. ArrayList<Card> restSuitList = new ArrayList<Card>();
  186. String suitKey = sortSelected.substring(0,1);
  187.  
  188. for(int i=0; i<cardList.size(); i++) {
  189.  
  190. Card currCard = cardList.get(i);
  191. String currSuit = currCard.getSuit();
  192.  
  193. if(currSuit.equalsIgnoreCase(suitKey)){
  194. selectedSuitList.add(currCard);
  195. }else {
  196. restSuitList.add(currCard);
  197. }
  198. }
  199.  
  200. cardList = new ArrayList<Card>(selectedSuitList);
  201. cardList.addAll(restSuitList);
  202.  
  203. return cardList;
  204. }
  205.  
  206. }
  207.  
  208. import java.awt.*;
  209. import java.io.*;
  210. import java.util.ArrayList;
  211. import java.util.Collections;
  212. import java.applet.*;
  213. import java.awt.image.*;
  214. import java.awt.event.ActionEvent;
  215. import java.awt.event.ActionListener;
  216.  
  217.  
  218. /**
  219. * This applet is currently working. It will run in a webpage. It will take in card codes such as
  220. * 2h for the two of heart and "paint" the image of five cards when all five cards
  221. * card coding entered into them. Your assignment will be to write the part of the code
  222. * that will sort the cards once they have been entered so that the will be "painted" from smallest
  223. * to largest denomination regardless of suit as long "None" is selected in the drop down box. In
  224. * the event one of the suits is selected that suit will be placed in order first then followed by
  225. * the rest of the cards in order. To complete the assignment you should read through this class' code
  226. * but you will only need to change the section that state that you should change it and possibly
  227. * the card class.
  228. *
  229. * @author (Put your name here.)
  230. * @version (Put the date here.)
  231. */
  232. public class CardApplet extends Applet {
  233.  
  234. Image ic1,ic2,ic3,ic4,ic5;
  235. Card c1,c2,c3,c4,c5;
  236. private TextField cardIn1,cardIn2,cardIn3,cardIn4,cardIn5;
  237. private String message;
  238. private Button enter,sort;
  239. private ButtonListener buttonListen;
  240. private Choice trump;
  241. static final int CARD_WIDTH = 73;
  242. static final int CARD_HEIGHT = 98;
  243.  
  244. private ArrayList<Card> cardList;
  245. private ArrayList<Card> cards;
  246.  
  247. /**
  248. * This is called an inner class as it is a class writen inside of another class. It is here so
  249. * that the buttons will be able to trigger an event.
  250. */
  251. class ButtonListener implements ActionListener
  252. {
  253. /**
  254. * The name of this method is important and should not be changed. This will take in the
  255. * "action" of a button being pushed and store reference to it in the object variable e.
  256. */
  257. public void actionPerformed(ActionEvent e)
  258. {
  259.  
  260. String action = e.paramString();
  261.  
  262. if( action.indexOf("Enter") >= 0)
  263. {
  264. //get text
  265. String text1 = cardIn1.getText();
  266. String text2 = cardIn2.getText();
  267. String text3 = cardIn3.getText();
  268. String text4 = cardIn4.getText();
  269. String text5 = cardIn5.getText();
  270.  
  271. //Get rid of whitespace before and after string
  272. text1 = text1.trim();
  273. text2 = text2.trim();
  274. text3 = text3.trim();
  275. text4 = text4.trim();
  276. text5 = text5.trim();
  277.  
  278. message = "Cards Entered";
  279.  
  280. //setup cards and card images
  281. c1 = new Card(text1);
  282. ic1 = getCardImage(c1);
  283. c2 = new Card(text2);
  284. ic2 = getCardImage(c2);
  285. c3 = new Card(text3);
  286. ic3 = getCardImage(c3);
  287. c4 = new Card(text4);
  288. ic4 = getCardImage(c4);
  289. c5 = new Card(text5);
  290. ic5 = getCardImage(c5);
  291.  
  292. // Add cards to cardList
  293. cardList = new ArrayList<Card>();
  294. cardList.add(c1);
  295. cardList.add(c2);
  296. cardList.add(c3);
  297. cardList.add(c4);
  298. cardList.add(c5);
  299.  
  300. Collections.sort(cardList, Card.DenomComparator);
  301.  
  302. //this method call is to this class and tells the applet to follow the code of paint again.
  303. repaint();
  304. }
  305. else if( action.indexOf("Sort") >= 0)
  306. {
  307. //ADD YOUR CODE HERE.
  308. String sortSelected = trump.getSelectedItem();
  309.  
  310. if("None".equals(sortSelected)) {
  311. Collections.sort(cardList, Card.DenomComparator);
  312.  
  313. mapCardToImage();
  314. }else {
  315. cardList = Card.sortBySuit(cardList, sortSelected);
  316. mapCardToImage();
  317. }
  318.  
  319. //DO NOT CHANGE CODE PAST THIS LINE.
  320. message = "Sorted";
  321. repaint();
  322. }
  323.  
  324. }
  325. } //end of inner class.
  326.  
  327.  
  328. /**
  329. * This method is called when the applet is first started. It will setup the layout of the applet.
  330. */
  331. public void init() {
  332.  
  333. //This is the text that prints in the gray box towards the bottem of the applet.
  334. message="Let us get started ";
  335.  
  336. //Sets the back ground color of the the applet
  337. setBackground(Color.GREEN);
  338.  
  339. // Set default layout manager
  340. setLayout(new FlowLayout() );
  341.  
  342. //setup textboxes for entering in cards
  343. cardIn1 = new TextField("Enter",4);
  344. add(cardIn1);
  345. cardIn2 = new TextField("cards ",4);
  346. add(cardIn2);
  347. cardIn3 = new TextField("using",4);
  348. add(cardIn3);
  349. cardIn4 = new TextField("Chap 5",4);
  350. add(cardIn4);
  351. cardIn5 = new TextField("coding",4);
  352. add(cardIn5);
  353.  
  354. //place buttons
  355. buttonListen = new ButtonListener();
  356. enter = new Button("Enter");
  357. enter.addActionListener(buttonListen);
  358. add(enter);
  359. sort = new Button("Sort");
  360. sort.addActionListener(buttonListen);
  361. add(sort);
  362.  
  363. //setup dropdown
  364. trump = new Choice();
  365. trump.addItem("None");
  366. trump.addItem("Hearts");
  367. trump.addItem("Diamonds");
  368. trump.addItem("Spades");
  369. trump.addItem("Clubs");
  370. add(trump);
  371.  
  372. //Since the card object variables are null each image object variable
  373. //will hold reference to a card back image.
  374. ic1 = getCardImage(c1);
  375. ic2 = getCardImage(c2);
  376. ic3 = getCardImage(c3);
  377. ic4 = getCardImage(c4);
  378. ic5 = getCardImage(c5);
  379.  
  380. }
  381.  
  382. /*
  383. * This class is used to place graphics on an applet.
  384. */
  385. public void paint(Graphics g) {
  386.  
  387. //places cards on applet
  388. int linePos = 70;
  389. g.drawImage(ic1,19,linePos,this);
  390. g.drawImage(ic2,19+CARD_WIDTH,linePos,this);
  391. g.drawImage(ic3,19+2*CARD_WIDTH,linePos,this);
  392. g.drawImage(ic4,19+3*CARD_WIDTH,linePos,this);
  393. g.drawImage(ic5,19+4*CARD_WIDTH,linePos,this);
  394.  
  395. // simple text displayed on applet
  396. g.setColor(Color.lightGray);
  397. g.draw3DRect(2, 175, 200, 20, true);
  398. g.fillRect(2, 175, 200, 20);
  399. g.setColor(Color.black);
  400. g.drawString(message, 4, 190);
  401. }
  402.  
  403. /**
  404. * This will select either the correct portion of the cards image based on the suit and denomination or
  405. * the card back image.
  406. * @param a The card object holds the suit and denomination in state.
  407. * @return It returns an image object variable with holds reference to a image that was created for a card that was passed in.
  408. * @throws MalformedURLException
  409. */
  410. public Image getCardImage(final Card a)
  411. {
  412.  
  413.  
  414.  
  415.  
  416. int cardDenom,cardSuit;
  417. Image playingCards = null;
  418. ImageFilter cardFilter;
  419. ImageProducer cardProducer;
  420.  
  421. if( a == null)
  422. {
  423. playingCards = getImage(getCodeBase(), "cardBack.png");
  424.  
  425.  
  426. }else{
  427. playingCards = getImage(getCodeBase(),"cards.png");
  428.  
  429. cardDenom = (a.numDenom()*CARD_WIDTH)- CARD_WIDTH;
  430. cardSuit = (a.numSuit()*CARD_HEIGHT) - CARD_HEIGHT;
  431. cardFilter = new CropImageFilter(cardDenom,cardSuit,CARD_WIDTH,CARD_HEIGHT);
  432. cardProducer = new FilteredImageSource(playingCards.getSource(),cardFilter);
  433. playingCards = createImage(cardProducer);
  434. }
  435.  
  436. return playingCards;
  437. }
  438.  
  439. private void mapCardToImage() {
  440. c1 = cards.get(0);
  441. ic1 = getCardImage(c1);
  442. c2 = cards.get(1);
  443. ic2 = getCardImage(c2);
  444. c3 = cards.get(2);
  445. ic3 = getCardImage(c3);
  446. c4 = cards.get(3);
  447. ic4 = getCardImage(c4);
  448. c5 = cards.get(4);
  449. ic5 = getCardImage(c5);
  450. }
  451.  
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement