Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Poker Game - can't get the button layout to work :(

setpreferredsize

  • Please log in to reply
5 replies to this topic

#1 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 08 June 2010 - 05:38 PM

Hi. I have been trying to figure this out for several days though I seem stuck and have thus decided to ask for help.

I am currently writing a program to play poker and I am having an issue with the GUI. I cannot get the buttons to align correctly. I created a new class called tablePlay which will run the table's gui and functions. I will create a different class for the AI/NPC that the user will be playing with though for now I was just wondering if I could get some help with GUI making.

This is my code so far.

/*
*by : dawson reid
*to : run the poker table
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Random;

public class TablePlay implements ActionListener{

//Table
JFrame tblFrame;
JPanel contentPane;
JPanel tblPane;
JPanel btnPane;
JButton btnCheck;
JButton btnFold;
JButton btnRaise;
JButton btnMatch;
JButton btnSteal;
JButton btnDeal;
JLabel lblTittle;
JLabel lblTable;

public TablePlay() {

boolean Table = true;

//Play Table
tblFrame = new JFrame("Table");
tblFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Panel for table screen
contentPane = new JPanel();

tblPane = new JPanel();
tblPane.setLayout(new BoxLayout(tblPane, BoxLayout.PAGE_AXIS));
tblPane.setBorder(BorderFactory.createEmptyBorder(150,50,50,50));
tblPane.setBackground(Color.white);

btnPane = new JPanel();
btnPane.setLayout(new BoxLayout(tblPane, BoxLayout.LINE_AXIS));
btnPane.setBorder(BorderFactory.createEmptyBorder(150,50,50,50));
btnPane.setBackground(Color.white);

lblTittle = new JLabel();
lblTittle.setText("Battle Poker");
lblTittle.setVerticalTextPosition(JLabel.TOP);
lblTittle.setHorizontalTextPosition(JLabel.LEFT);
tblPane.add(lblTittle);

lblTable = new JLabel(new ImageIcon("img\\table.jpg"));
lblTable.setPreferredSize(new Dimension(250, 80));
lblTable.setVerticalTextPosition(JLabel.TOP);
lblTable.setHorizontalTextPosition(JLabel.CENTER);
tblPane.add(lblTable);

//Buttons
btnFold = new JButton("Fold");
btnFold.setAlignmentY(Component.BOTTOM_ALIGNMENT);
btnFold.setAlignmentX(Component.LEFT_ALIGNMENT);
btnFold.add(Box.createRigidArea(new Dimension(38, 15)));
btnPane.add(btnFold);
btnFold.setActionCommand("Fold");
btnFold.addActionListener(this);

btnRaise = new JButton("Raise");
btnRaise.setAlignmentY(Component.BOTTOM_ALIGNMENT);
btnRaise.setAlignmentX(Component.LEFT_ALIGNMENT);
btnRaise.add(Box.createRigidArea(new Dimension(38, 15)));
btnPane.add(btnRaise);
btnRaise.setActionCommand("Raise");
btnRaise.addActionListener(this);

btnMatch = new JButton("Match");
btnMatch.setAlignmentY(Component.BOTTOM_ALIGNMENT);
btnMatch.setAlignmentX(Component.CENTER_ALIGNMENT);
btnMatch.add(Box.createRigidArea(new Dimension(38, 15)));
btnPane.add(btnMatch);
btnMatch.setActionCommand("Match");
btnMatch.addActionListener(this);

btnSteal = new JButton("Steal");
btnSteal.setAlignmentY(Component.BOTTOM_ALIGNMENT);
btnSteal.setAlignmentX(Component.RIGHT_ALIGNMENT);
btnSteal.add(Box.createRigidArea(new Dimension(38, 15)));
btnPane.add(btnSteal);
btnSteal.setActionCommand("Steal");
btnSteal.addActionListener(this);

btnDeal = new JButton("Deal");
btnDeal.setAlignmentY(Component.BOTTOM_ALIGNMENT);
btnDeal.setAlignmentX(Component.RIGHT_ALIGNMENT);
btnDeal.add(Box.createRigidArea(new Dimension(38, 15)));
btnPane.add(btnDeal);
btnDeal.setActionCommand("Deal");
btnDeal.addActionListener(this);

//Packing the table
contentPane.add(tblPane, BorderLayout.CENTER);
contentPane.add(btnPane, BorderLayout.PAGE_END);

tblFrame.setContentPane(contentPane);
tblFrame.pack();
tblFrame.setVisible(true);
}
public static String[] cards() {

String[] cards;
cards = new String[9];

String[] out;
out = new String[52];

out[0] = "2C.bmp";
out[1] = "2D.bmp";
out[2] = "2H.bmp";
out[3] = "2S.bmp";
out[4] = "3C.bmp";
out[5] = "3D.bmp";
out[6] = "3H.bmp";
out[7] = "3S.bmp";
out[8] = "4C.bmp";
out[9] = "4D.bmp";
out[10] = "4H.bmp";
out[11] = "4S.bmp";
out[12] = "5C.bmp";
out[13] = "5D.bmp";
out[14] = "5H.bmp";
out[15] = "5S.bmp";
out[16] = "6C.bmp";
out[17] = "6D.bmp";
out[18] = "6H.bmp";
out[19] = "6S.bmp";
out[20] = "7C.bmp";
out[21] = "7D.bmp";
out[22] = "7H.bmp";
out[23] = "7S.bmp";
out[24] = "8C.bmp";
out[25] = "8D.bmp";
out[26] = "8H.bmp";
out[27] = "8S.bmp";
out[28] = "9C.bmp";
out[29] = "9D.bmp";
out[30] = "9H.bmp";
out[31] = "9S.bmp";
out[32] = "10C.bmp";
out[33] = "10D.bmp";
out[34] = "10H.bmp";
out[35] = "10S.bmp";
out[36] = "jackC.bmp";
out[37] = "jackD.bmp";
out[38] = "jackH.bmp";
out[39] = "jackS.bmp";
out[40] = "queenC.bmp";
out[41] = "queenD.bmp";
out[42] = "queenH.bmp";
out[43] = "queenS.bmp";
out[44] = "kingC.bmp";
out[45] = "kingD.bmp";
out[46] = "kingH.bmp";
out[47] = "kingS.bmp";
out[48] = "aceC.bmp";
out[49] = "aceD.bmp";
out[50] = "aceH.bmp";
out[51] = "aceS.bmp";

int j = 52;
int x = 0;
int i;
int q;

do {

Random generator = new Random();
int rnd = generator.nextInt(j);
q = rnd;

cards[x] = out[q];

x++;
i = 1;

do {
out[q] = out[q + i];
i++;
} while (q + i < j);
j--;
}while(x < 9);

System.out.println(cards[0]);
System.out.println(cards[1]);
System.out.println(cards[2]);
System.out.println(cards[3]);
System.out.println(cards[4]);
System.out.println(cards[5]);
System.out.println(cards[6]);
System.out.println(cards[7]);
System.out.println(cards[8]);

return cards;
}

public void actionPerformed(ActionEvent event) {

String eventName = event.getActionCommand();

if (eventName == "Deal") {

cards();
}
}

private static void runGui() {

JFrame.setDefaultLookAndFeelDecorated(false);
TablePlay screen = new TablePlay();
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGui();
}
});
}
}

I was also wondering how I would go about retrieving the array from the cards() method? I want to do this that way I may set labels to the card images.

I was also wondering how I would place the example code above in the nifty little boxes that are used.

Thank You !!
  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 08 June 2010 - 10:42 PM

2 things i notice:

-You forgot to change tblPane into btnPane after the copy paste^^:
btnPane.setLayout(new BoxLayout(tblPane, BoxLayout.LINE_AXIS));

-You use contentPane as it has a borderlayout. But a JPanel's default is a flowLayout. So you should do
contentPane = new JPanel(new BorderLayout());
contentPane.add(tblPane, BorderLayout.CENTER);
contentPane.add(btnPane, BorderLayout.PAGE_END);

Or totally drop this contentPane panel and do:
tblFrame.add(tblPane, BorderLayout.CENTER);
tblFrame.add(btnPane, BorderLayout.PAGE_END);

Which has the same effect, adding to a JFrame will automatically add to the contentPane, and a JFrame's contentPane is BorderLayout by default.
  • 0

#3 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 09 June 2010 - 01:06 PM

Thank you oxano. I was also wondering how I post the code in a box with a scroll bar on the side.

I the function cards() in the above code I have it set to return arrays. I'm not able to use the array after the function has been executed though and I was wondering how I would make that array accessible to the rest of the program ?
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 09 June 2010 - 10:33 PM

Posting code is done with code tags [code=auto:0] <<Code here>> [/ CODE] <<remove space in closing tag. You can also click the #-button to add these tags.

Why do you want to keep the card[] array? :)
The array out[ ] is more important, and i would create a class variable of that one. I would also fill that one in the constructor.
Dealing the cards actually shouldn't be done in the GUI class but in the model class/game class / ai class /...

The GUI class will have an object of the model class/ game .... and at the end of the constructor it calls gameObject.Deal() -> returns array of the player's cards.
And this array combined with the out[] should be enough to get the right images for the player's hand.
  • 0

#5 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 11 June 2010 - 10:45 AM

I don't understand why the out[] array is more important. The card array is the nine cards that will have come out of the deck. out[] is holding the cards. This program will be used to play texas hold'em so 9 cards are taken out of the deck. Two cards for the user, two cards for the computer and five are on the table. The out[] is only being used for selecting the cards. Could you explain?
  • 0

#6 GMVResources

GMVResources

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 71 posts

Posted 13 June 2010 - 05:55 PM

agnl I really think you should take a couple of books before you start to program a game or use some references on the web. Also I don't think you should make more than one request for your Poker Game ^^
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download