Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ui;
- import java.awt.BorderLayout;
- import java.awt.CardLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import javax.swing.BorderFactory;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import logic.Card;
- import logic.Config;
- import logic.Deck;
- import io.Img;
- @SuppressWarnings("serial")
- public class table extends JPanel {
- public BufferedImage tPile;
- public ImageIcon tp;
- int i;
- JButton[] c = new JButton[24];
- JButton d;
- JButton toppileCard = new JButton("CARDPILE");
- JLabel d1,d2,d3,d4,dd,ddd,p2h,p3h,p4h;
- JPanel inHand,t,hand1,Hand,hand2,hand3,player2;
- Deck deck;
- Card[] h = new Card[24];
- JButton nxt = new JButton("NEXT");
- JButton prv = new JButton("Prev");
- JButton nxt2 = new JButton("NEXT");
- JButton prv2 = new JButton("Prev");
- public static int handCount = 7;
- boolean isDraw = false;
- int player2hand = 7,player3hand = 7,player4hand = 7;
- public static JLabel p1;
- public table(){
- ArrayList<Card> cardpile = new ArrayList<Card>();
- deck = new Deck();
- toppileCard.setName("ss");
- Dimension but = new Dimension(100,150);
- nxt.setPreferredSize(but);
- prv.setPreferredSize(but);
- nxt2.setPreferredSize(but);
- prv2.setPreferredSize(but);
- t = new JPanel();
- Color BG = new Color(73,183,73);
- t.setBackground(BG);
- this.setBackground(BG);
- this.setLayout(new GridBagLayout());
- t.setLayout(new GridBagLayout());
- Hand = new JPanel(new BorderLayout());
- inHand = new JPanel(new CardLayout());
- inHand.setOpaque(false);
- hand1 = new JPanel();
- hand1.setLayout(new FlowLayout());
- hand1.setBackground(BG);
- hand2 = new JPanel();
- hand2.setLayout(new FlowLayout());
- hand2.setBackground(BG);
- hand3 = new JPanel();
- hand3.setLayout(new FlowLayout());
- hand3.setBackground(BG);
- addCardToHand(h);
- d = new JButton(new ImageIcon(Img.deck));
- d.setBorder(BorderFactory.createEmptyBorder());
- d.setContentAreaFilled(false);
- d.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- //if(!isDraw){
- if(handCount>21) return;
- handCount++;
- System.out.println(handCount);
- addCardToHand(deck.getCard(handCount));
- isDraw = true;
- table.p1.setText(Config.playerName+"'s Card(s)"+" ("+table.handCount+")");
- //}
- }
- });
- if(cardpile.size()>=1){
- toppileCard = (cardpile.get(cardpile.size()-1)).CardtoJButton();
- }
- JPanel p2 = new JPanel(new BorderLayout());
- player2 = new JPanel(new FlowLayout());
- player2.setOpaque(false);
- JLabel pl2 = new JLabel("Player2");
- p2h = new JLabel("("+player2hand+")");
- d1 = new JLabel(new ImageIcon(Img.back));
- d1.setBorder(BorderFactory.createEmptyBorder());
- player2.add(pl2);
- player2.add(p2h);
- p2.add(player2,BorderLayout.NORTH);
- p2.add(d1,BorderLayout.CENTER);
- p2.setOpaque(false);
- JPanel p3 = new JPanel(new BorderLayout());
- JPanel player3 = new JPanel(new FlowLayout());
- player3.setOpaque(false);
- JLabel pl3 = new JLabel("Player3");
- p3h = new JLabel("("+player3hand+")");
- d2 = new JLabel(new ImageIcon(Img.back));
- d2.setBorder(BorderFactory.createEmptyBorder());
- player3.add(pl3);
- player3.add(p3h);
- p3.add(player3,BorderLayout.NORTH);
- p3.add(d2,BorderLayout.CENTER);
- p3.setOpaque(false);
- dd = new JLabel(new ImageIcon(Img.back));
- dd.setBorder(BorderFactory.createEmptyBorder());
- dd.setVisible(false);
- ddd = new JLabel("");
- JPanel p4 = new JPanel(new BorderLayout());
- JPanel player4 = new JPanel(new FlowLayout());
- player4.setOpaque(false);
- JLabel pl4 = new JLabel("Player4");
- p4h = new JLabel("("+player4hand+")");
- d3 = new JLabel(new ImageIcon(Img.back));
- d3.setBorder(BorderFactory.createEmptyBorder());
- player4.add(pl4);
- player4.add(p4h);
- p4.add(player4,BorderLayout.NORTH);
- p4.add(d3,BorderLayout.CENTER);
- p4.setOpaque(false);
- GridBagConstraints x = new GridBagConstraints();
- x.weightx = 2;
- x.weighty = 1;
- x.gridx = 0;
- x.gridy = 1;
- //x.anchor = GridBagConstraints.LINE_START;
- t.add(p2,x);
- x.weightx = 0.25;
- x.weighty = 1;
- x.gridx = 1;
- x.gridy = 1;
- //x.anchor = GridBagConstraints.CENTER;
- t.add(d,x);
- x.weightx = 0;
- x.weighty = 0;
- x.gridx = 2;
- x.gridy = 1;
- //x.anchor = GridBagConstraints.CENTER;
- t.add(dd,x);
- x.weightx = 0;
- x.weighty = 0.5;
- x.gridx = 2;
- x.gridy = 2;
- //x.anchor = GridBagConstraints.CENTER;
- t.add(ddd,x);
- x.weightx = 0;
- x.weighty = 0.25;
- x.gridx = 2;
- x.gridy = 0;
- //x.anchor = GridBagConstraints.CENTER;
- t.add(p3,x);
- x.weightx = 0.25;
- x.weighty = 1;
- x.gridx = 3;
- x.gridy = 1;
- //x.anchor = GridBagConstraints.CENTER;
- t.add(toppileCard,x);
- x.weightx = 2;
- x.weighty = 1;
- x.gridx = 4;
- x.gridy = 1;
- //x.anchor = GridBagConstraints.LINE_END;
- t.add(p4,x);
- p1 = new JLabel("",JLabel.CENTER);
- Hand.setOpaque(false);
- Hand.add(p1,BorderLayout.NORTH);
- Hand.add(inHand,BorderLayout.CENTER);
- inHand.add(hand1,"HAND1");
- inHand.add(hand2,"HAND2");
- inHand.add(hand3,"HAND3");
- nxt.addActionListener(new buttonQ());
- prv.addActionListener(new buttonQ());
- nxt2.addActionListener(new buttonQ());
- prv2.addActionListener(new buttonQ());
- GridBagConstraints y = new GridBagConstraints();
- y.weightx = 3;
- y.weighty = 3;
- y.fill = GridBagConstraints.BOTH;
- y.gridx = 0;
- y.gridy = 0;
- this.add(t,y);
- y.weightx = 1;
- y.weighty = 1.5;
- y.fill = GridBagConstraints.BOTH;
- y.gridx = 0;
- y.gridy = 1;
- this.add(Hand,y);
- }
- public void addCardToHand(Card[] x){
- for(int i = 1;i<=handCount;i++){
- x[i] = deck.getCard(i);
- c[i] = x[i].CardtoJButton();
- c[i].addActionListener(new placeCard());
- }
- if(handCount<=7){
- for(int i = 1;i<=handCount;i++){
- hand1.add(c[i]);
- }
- }
- else if(handCount<=14){
- for(int i = 1;i<=7;i++){
- hand1.add(c[i]);
- }
- hand1.add(nxt);
- hand2.add(prv);
- for(int i = 8;i<=handCount;i++){
- hand2.add(c[i]);
- }
- }
- else {
- for(int i = 1;i<=7;i++){
- hand1.add(c[i]);
- }
- hand1.add(nxt);
- hand2.add(prv);
- for(int i = 8;i<=14;i++){
- hand2.add(c[i]);
- }
- hand2.add(nxt2);
- hand3.add(prv2);
- for(int i = 15;i<=handCount;i++){
- hand3.add(c[i]);
- }
- }
- }
- public void addCardToHand(Card a){
- JButton n = a.CardtoJButton();
- if(handCount<=7){
- hand1.add(n);
- Hand.validate();
- Hand.repaint();
- }
- else if(handCount==8){
- hand1.add(nxt);
- hand2.add(prv);
- hand2.add(n);
- Hand.validate();
- Hand.repaint();
- }
- else if(handCount<=14){
- hand2.add(n);
- Hand.validate();
- Hand.repaint();
- }
- else if(handCount == 15){
- hand2.add(nxt2);
- hand3.add(prv2);
- hand3.add(n);
- Hand.validate();
- Hand.repaint();
- }
- else if(handCount <=22){
- hand3.add(n);
- Hand.validate();
- Hand.repaint();
- }
- else return;
- }
- public class buttonQ implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- if(e.getSource()==nxt){
- CardLayout cardLayout = (CardLayout) inHand.getLayout();
- cardLayout.show(inHand, "HAND2");
- }
- if(e.getSource()==prv){
- CardLayout cardLayout = (CardLayout) inHand.getLayout();
- cardLayout.show(inHand, "HAND1");
- }
- if(e.getSource()==nxt2){
- CardLayout cardLayout = (CardLayout) inHand.getLayout();
- cardLayout.show(inHand, "HAND3");
- }
- if(e.getSource()==prv2){
- CardLayout cardLayout = (CardLayout) inHand.getLayout();
- cardLayout.show(inHand, "HAND2");
- }
- }
- }
- public class placeCard implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent a) {
- // TODO Auto-generated method stub
- // for(i=0;i<=handCount-1;i++)
- // if(a.getSource()==c[i])
- // if(isEqual(c[i],toppileCard))
- toppileCard = c[i];
- }
- }
- public boolean isEqual(JButton card,JButton toppile) {
- String b = null,b1 = null;
- String a = card.getName().substring(card.getName().length()-1);
- String a1 = card.getName().substring(0,card.getName().length()-1);
- if(!toppile.getName().equals("ss")){
- b = toppile.getName().substring(toppile.getName().length()-1);
- b1 = toppile.getName().substring(0,toppile.getName().length()-1);
- }
- if(a.equalsIgnoreCase(b)) return true;
- else if(a1.equalsIgnoreCase(b1)) return true;
- else if((b+b1).equalsIgnoreCase("wild") || (b+b1).equalsIgnoreCase("drawFour") ) return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement