Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. package ui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.CardLayout;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.FlowLayout;
  8. import java.awt.GridBagConstraints;
  9. import java.awt.GridBagLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.image.BufferedImage;
  13. import java.util.ArrayList;
  14.  
  15. import javax.swing.BorderFactory;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JButton;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20.  
  21. import logic.Card;
  22. import logic.Config;
  23. import logic.Deck;
  24. import io.Img;
  25.  
  26. @SuppressWarnings("serial")
  27. public class table extends JPanel {
  28. public BufferedImage tPile;
  29. public ImageIcon tp;
  30. int i;
  31. JButton[] c = new JButton[24];
  32. JButton d;
  33. JButton toppileCard = new JButton("CARDPILE");
  34. JLabel d1,d2,d3,d4,dd,ddd,p2h,p3h,p4h;
  35. JPanel inHand,t,hand1,Hand,hand2,hand3,player2;
  36. Deck deck;
  37. Card[] h = new Card[24];
  38. JButton nxt = new JButton("NEXT");
  39. JButton prv = new JButton("Prev");
  40. JButton nxt2 = new JButton("NEXT");
  41. JButton prv2 = new JButton("Prev");
  42. public static int handCount = 7;
  43. boolean isDraw = false;
  44. int player2hand = 7,player3hand = 7,player4hand = 7;
  45. public static JLabel p1;
  46.  
  47.  
  48. public table(){
  49. ArrayList<Card> cardpile = new ArrayList<Card>();
  50. deck = new Deck();
  51. toppileCard.setName("ss");
  52. Dimension but = new Dimension(100,150);
  53. nxt.setPreferredSize(but);
  54. prv.setPreferredSize(but);
  55. nxt2.setPreferredSize(but);
  56. prv2.setPreferredSize(but);
  57. t = new JPanel();
  58. Color BG = new Color(73,183,73);
  59. t.setBackground(BG);
  60. this.setBackground(BG);
  61. this.setLayout(new GridBagLayout());
  62. t.setLayout(new GridBagLayout());
  63.  
  64. Hand = new JPanel(new BorderLayout());
  65. inHand = new JPanel(new CardLayout());
  66. inHand.setOpaque(false);
  67. hand1 = new JPanel();
  68. hand1.setLayout(new FlowLayout());
  69. hand1.setBackground(BG);
  70. hand2 = new JPanel();
  71. hand2.setLayout(new FlowLayout());
  72. hand2.setBackground(BG);
  73. hand3 = new JPanel();
  74. hand3.setLayout(new FlowLayout());
  75. hand3.setBackground(BG);
  76.  
  77. addCardToHand(h);
  78.  
  79.  
  80. d = new JButton(new ImageIcon(Img.deck));
  81. d.setBorder(BorderFactory.createEmptyBorder());
  82. d.setContentAreaFilled(false);
  83.  
  84. d.addActionListener(new ActionListener(){
  85.  
  86. @Override
  87. public void actionPerformed(ActionEvent arg0) {
  88. // TODO Auto-generated method stub
  89. //if(!isDraw){
  90. if(handCount>21) return;
  91. handCount++;
  92.  
  93. System.out.println(handCount);
  94. addCardToHand(deck.getCard(handCount));
  95. isDraw = true;
  96. table.p1.setText(Config.playerName+"'s Card(s)"+" ("+table.handCount+")");
  97. //}
  98. }
  99.  
  100. });
  101.  
  102. if(cardpile.size()>=1){
  103. toppileCard = (cardpile.get(cardpile.size()-1)).CardtoJButton();
  104. }
  105.  
  106. JPanel p2 = new JPanel(new BorderLayout());
  107. player2 = new JPanel(new FlowLayout());
  108. player2.setOpaque(false);
  109. JLabel pl2 = new JLabel("Player2");
  110. p2h = new JLabel("("+player2hand+")");
  111. d1 = new JLabel(new ImageIcon(Img.back));
  112. d1.setBorder(BorderFactory.createEmptyBorder());
  113. player2.add(pl2);
  114. player2.add(p2h);
  115. p2.add(player2,BorderLayout.NORTH);
  116. p2.add(d1,BorderLayout.CENTER);
  117. p2.setOpaque(false);
  118. JPanel p3 = new JPanel(new BorderLayout());
  119. JPanel player3 = new JPanel(new FlowLayout());
  120. player3.setOpaque(false);
  121. JLabel pl3 = new JLabel("Player3");
  122. p3h = new JLabel("("+player3hand+")");
  123. d2 = new JLabel(new ImageIcon(Img.back));
  124. d2.setBorder(BorderFactory.createEmptyBorder());
  125. player3.add(pl3);
  126. player3.add(p3h);
  127. p3.add(player3,BorderLayout.NORTH);
  128. p3.add(d2,BorderLayout.CENTER);
  129. p3.setOpaque(false);
  130. dd = new JLabel(new ImageIcon(Img.back));
  131. dd.setBorder(BorderFactory.createEmptyBorder());
  132. dd.setVisible(false);
  133.  
  134. ddd = new JLabel("");
  135.  
  136. JPanel p4 = new JPanel(new BorderLayout());
  137. JPanel player4 = new JPanel(new FlowLayout());
  138. player4.setOpaque(false);
  139. JLabel pl4 = new JLabel("Player4");
  140. p4h = new JLabel("("+player4hand+")");
  141. d3 = new JLabel(new ImageIcon(Img.back));
  142. d3.setBorder(BorderFactory.createEmptyBorder());
  143. player4.add(pl4);
  144. player4.add(p4h);
  145. p4.add(player4,BorderLayout.NORTH);
  146. p4.add(d3,BorderLayout.CENTER);
  147. p4.setOpaque(false);
  148. GridBagConstraints x = new GridBagConstraints();
  149. x.weightx = 2;
  150. x.weighty = 1;
  151. x.gridx = 0;
  152. x.gridy = 1;
  153. //x.anchor = GridBagConstraints.LINE_START;
  154. t.add(p2,x);
  155.  
  156. x.weightx = 0.25;
  157. x.weighty = 1;
  158. x.gridx = 1;
  159. x.gridy = 1;
  160. //x.anchor = GridBagConstraints.CENTER;
  161. t.add(d,x);
  162. x.weightx = 0;
  163. x.weighty = 0;
  164. x.gridx = 2;
  165. x.gridy = 1;
  166. //x.anchor = GridBagConstraints.CENTER;
  167. t.add(dd,x);
  168. x.weightx = 0;
  169. x.weighty = 0.5;
  170. x.gridx = 2;
  171. x.gridy = 2;
  172. //x.anchor = GridBagConstraints.CENTER;
  173. t.add(ddd,x);
  174. x.weightx = 0;
  175. x.weighty = 0.25;
  176. x.gridx = 2;
  177. x.gridy = 0;
  178. //x.anchor = GridBagConstraints.CENTER;
  179. t.add(p3,x);
  180. x.weightx = 0.25;
  181. x.weighty = 1;
  182. x.gridx = 3;
  183. x.gridy = 1;
  184. //x.anchor = GridBagConstraints.CENTER;
  185. t.add(toppileCard,x);
  186.  
  187. x.weightx = 2;
  188. x.weighty = 1;
  189. x.gridx = 4;
  190. x.gridy = 1;
  191. //x.anchor = GridBagConstraints.LINE_END;
  192. t.add(p4,x);
  193.  
  194.  
  195.  
  196.  
  197. p1 = new JLabel("",JLabel.CENTER);
  198. Hand.setOpaque(false);
  199. Hand.add(p1,BorderLayout.NORTH);
  200. Hand.add(inHand,BorderLayout.CENTER);
  201. inHand.add(hand1,"HAND1");
  202. inHand.add(hand2,"HAND2");
  203. inHand.add(hand3,"HAND3");
  204. nxt.addActionListener(new buttonQ());
  205. prv.addActionListener(new buttonQ());
  206. nxt2.addActionListener(new buttonQ());
  207. prv2.addActionListener(new buttonQ());
  208. GridBagConstraints y = new GridBagConstraints();
  209. y.weightx = 3;
  210. y.weighty = 3;
  211. y.fill = GridBagConstraints.BOTH;
  212. y.gridx = 0;
  213. y.gridy = 0;
  214. this.add(t,y);
  215. y.weightx = 1;
  216. y.weighty = 1.5;
  217. y.fill = GridBagConstraints.BOTH;
  218. y.gridx = 0;
  219. y.gridy = 1;
  220. this.add(Hand,y);
  221.  
  222. }
  223. public void addCardToHand(Card[] x){
  224. for(int i = 1;i<=handCount;i++){
  225. x[i] = deck.getCard(i);
  226. c[i] = x[i].CardtoJButton();
  227. c[i].addActionListener(new placeCard());
  228. }
  229.  
  230.  
  231. if(handCount<=7){
  232. for(int i = 1;i<=handCount;i++){
  233. hand1.add(c[i]);
  234. }
  235. }
  236.  
  237. else if(handCount<=14){
  238. for(int i = 1;i<=7;i++){
  239. hand1.add(c[i]);
  240. }
  241. hand1.add(nxt);
  242. hand2.add(prv);
  243. for(int i = 8;i<=handCount;i++){
  244. hand2.add(c[i]);
  245. }
  246. }
  247. else {
  248. for(int i = 1;i<=7;i++){
  249. hand1.add(c[i]);
  250. }
  251. hand1.add(nxt);
  252. hand2.add(prv);
  253. for(int i = 8;i<=14;i++){
  254. hand2.add(c[i]);
  255. }
  256. hand2.add(nxt2);
  257. hand3.add(prv2);
  258. for(int i = 15;i<=handCount;i++){
  259. hand3.add(c[i]);
  260. }
  261. }
  262. }
  263.  
  264. public void addCardToHand(Card a){
  265. JButton n = a.CardtoJButton();
  266. if(handCount<=7){
  267. hand1.add(n);
  268. Hand.validate();
  269. Hand.repaint();
  270. }
  271. else if(handCount==8){
  272. hand1.add(nxt);
  273. hand2.add(prv);
  274. hand2.add(n);
  275. Hand.validate();
  276. Hand.repaint();
  277. }
  278. else if(handCount<=14){
  279. hand2.add(n);
  280. Hand.validate();
  281. Hand.repaint();
  282. }
  283. else if(handCount == 15){
  284. hand2.add(nxt2);
  285. hand3.add(prv2);
  286. hand3.add(n);
  287. Hand.validate();
  288. Hand.repaint();
  289. }
  290. else if(handCount <=22){
  291. hand3.add(n);
  292. Hand.validate();
  293. Hand.repaint();
  294. }
  295. else return;
  296. }
  297.  
  298. public class buttonQ implements ActionListener{
  299.  
  300. @Override
  301. public void actionPerformed(ActionEvent e) {
  302. // TODO Auto-generated method stub
  303. if(e.getSource()==nxt){
  304. CardLayout cardLayout = (CardLayout) inHand.getLayout();
  305. cardLayout.show(inHand, "HAND2");
  306. }
  307. if(e.getSource()==prv){
  308. CardLayout cardLayout = (CardLayout) inHand.getLayout();
  309. cardLayout.show(inHand, "HAND1");
  310. }
  311. if(e.getSource()==nxt2){
  312. CardLayout cardLayout = (CardLayout) inHand.getLayout();
  313. cardLayout.show(inHand, "HAND3");
  314. }
  315. if(e.getSource()==prv2){
  316. CardLayout cardLayout = (CardLayout) inHand.getLayout();
  317. cardLayout.show(inHand, "HAND2");
  318. }
  319. }
  320.  
  321. }
  322.  
  323. public class placeCard implements ActionListener{
  324.  
  325. @Override
  326. public void actionPerformed(ActionEvent a) {
  327. // TODO Auto-generated method stub
  328. // for(i=0;i<=handCount-1;i++)
  329. // if(a.getSource()==c[i])
  330. // if(isEqual(c[i],toppileCard))
  331. toppileCard = c[i];
  332.  
  333. }
  334.  
  335. }
  336.  
  337. public boolean isEqual(JButton card,JButton toppile) {
  338. String b = null,b1 = null;
  339. String a = card.getName().substring(card.getName().length()-1);
  340. String a1 = card.getName().substring(0,card.getName().length()-1);
  341. if(!toppile.getName().equals("ss")){
  342. b = toppile.getName().substring(toppile.getName().length()-1);
  343. b1 = toppile.getName().substring(0,toppile.getName().length()-1);
  344. }
  345. if(a.equalsIgnoreCase(b)) return true;
  346. else if(a1.equalsIgnoreCase(b1)) return true;
  347. else if((b+b1).equalsIgnoreCase("wild") || (b+b1).equalsIgnoreCase("drawFour") ) return true;
  348. else
  349. return false;
  350. }
  351.  
  352.  
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement