Advertisement
Guest User

Untitled

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