Jump to content

Help Java Card Game

- - - - -

  • Please log in to reply
4 replies to this topic

#1
macFs89H

macFs89H

    Newbie

  • Members
  • Pip
  • 2 posts
Hey im new to this website....

Im making a Uno card game in java as an application(not an applet), my first challenge is figuring out what layout
i will be using. I decided to use the Border layout, since it deals with the directions: North,South,East,West. This is the layout and the positioning as planned: player 1 (north,top of the screen),player 2(bottom, south of the screen),and deck of cards (east, side of screen).


this is where i have my problem, I want to display each Unocard as a JButton, so when i click it, it is played.....
when I use a boarder layout alone, the Jbuttons are too big, i need the buttons to be smaller individual buttons ... what can i do to change this.

Please be specific, im pretty much starting from ground zero, with the layout first.
Be specific please....
Examples or anything would help...:eek:


HEREs CODE:
_____________________________________________________

import javax.swing.*;

import javax.swing.JFrame;

import java.awt.*;




public class Layout {


	public static void main(String [] args){

		

		JFrame frame = new JFrame("UNo"); // sets JFrame to say UNO


		frame.setSize(600 ,400 ); // set width and height of frame


		frame.setLocationRelativeTo(null); // center it in middle

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


		LayoutManager border = new BorderLayout(1,1); // border layout

		frame.setLayout(border);


		JButton button = new JButton("click"); // button text // Just a test

						

					

											

		frame.add(button, BorderLayout.NORTH); // Jbutton is too big, need it be smaller

	                                                              //for each button


		frame.setVisible(true); // 



}


}

Edited by ZekeDragon, 29 April 2011 - 10:24 AM.
Please use [CODE] tags (the # button) when posting code.


#2
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
You probably need multiple layouts.
For the main layout you can use the BorderLayout, but every panel (east, west, etc) may get it's own panel with it's own layout...

For example you can give the top panel a GridLayout, with 1 row (it will divide the components evenly over the row), or you can even setLayout to null and set the bounds of the components yourself.
Or you can leave the default layout and let Java decide how to size the components;
import java.awt.BorderLayout;

import java.awt.LayoutManager;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;


public class Layout {


	public static void main(String[] args) {


		JFrame frame = new JFrame("UNo"); // sets JFrame to say UNO


		frame.setSize(600, 400); // set width and height of frame


		frame.setLocationRelativeTo(null); // center it in middle

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		LayoutManager border = new BorderLayout(1, 1); // border layout

		frame.setLayout(border);

		

		JButton button = new JButton("click"); // button text // Just a test

		JPanel topCards = new JPanel();

//		topCards.setLayout(new GridLayout(1, 0));

		topCards.add(button);


		frame.add(topCards, BorderLayout.NORTH); // Jbutton is too big, need it be

												// smaller

		// for each button


		frame.setVisible(true); //

	}

}


#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I wouldn't try to retrofit the JButton to be a card in this way. Instead, I'd just use a JPanel that I override the paintComponent() method in, then pass that JPanel a MouseListener which, when activated, checks the coordinates of the mouse click and determines if that field is where the card is in the JPanel. It's a little more custom, will look better, and probably end up being less complicated and more maintainable.
Wow I changed my sig!

#4
macFs89H

macFs89H

    Newbie

  • Members
  • Pip
  • 2 posts
Im back again....

The: topCards.setLayout(new GridLayout(1, 0)); didnt make the buttons smaller...it stayed the same...

So, I tryed using a JPanel and flowlayout inside the border layout to make the buttons smaller... and it worked

But, ive run into another problem...for the card game i want three buttons at the top(north position) and three and the bottom(south position).
I wrote 3 buttons for each area, with coordinates, but only 1 button shows in each area...

I looked up BoxLayouts,GridLayouts, CardsLayouts, gridLayouts...ects

But im still confused on how to make it work...
Can someone point me towards the right direction...
OR give me example...

CODE HERE:________________________________

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Layout {

public static void main(String[] args) {

JFrame frame = new JFrame("UNO"); // sets JFrame to say UNO

frame.setSize(850, 400); // set width and height of frame

frame.setLocationRelativeTo(null); // center it in middle
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

LayoutManager border = new BorderLayout(1, 1); // border layout
frame.setLayout(border);

//JButton button = new JButton("TOP Cards");

//__TOp Cards________________________//
JPanel topCard1 = new JPanel(); // area for card 1
topCard1.setLayout(new FlowLayout(FlowLayout.RIGHT,620,3));
topCard1.add(new JButton(" Card1 "));
frame.add(topCard1, BorderLayout.NORTH); // should i add something here to be able to add mutiple buttons ??

JPanel topCard2 = new JPanel(); // Area for card 2
topCard2.setLayout(new FlowLayout(FlowLayout.RIGHT,340,3));
topCard2.add(new JButton(" Card2 "));
frame.add(topCard2, BorderLayout.NORTH);

JPanel topCard3 = new JPanel();
topCard3.setLayout(new FlowLayout(FlowLayout.RIGHT,220,3)); // Area for card 3
topCard3.add(new JButton(" Card3 "));
frame.add(topCard3, BorderLayout.NORTH);



//___Bottom Cards________________________________//

//JButton button2 = new JButton("Bottom Cards");
JPanel bottomCard1 = new JPanel();
bottomCard1.setLayout(new FlowLayout(FlowLayout.RIGHT,620,3)); // hgap(horizontal), vgap(vertical) used a flowlayout for button
bottomCard1.add(new JButton(" Card 1 "));
frame.add(bottomCard1, BorderLayout.SOUTH);



JPanel bottomCard2 = new JPanel();
bottomCard2.setLayout(new FlowLayout(FlowLayout.RIGHT,340,3));
//bottomCards2.setLayout(new BoxLayout(bottomCards2, BoxLayout.Y_AXIS)); // box layout
bottomCard2.add(new JButton(" Card 2 "));
frame.add(bottomCard2 ,BorderLayout.SOUTH);

JPanel bottomCard3 = new JPanel();
bottomCard3.setLayout(new FlowLayout(FlowLayout.RIGHT,220,3));
bottomCard3.add(new JButton(" Card 3 "));
frame.add(bottomCard3, BorderLayout.SOUTH);


//JPanel content = new JPanel();
//content.setLayout(new FlowLayout());
//content.add(new JButton());
//content.add(new JButton("2"));
//content.add(new JButton("This is button three"));
//content.add(new JButton("four"));
//frame.add(bottomCards, BorderLayout.SOUTH);
// for each button

frame.setVisible(true); //
}
}

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
BorderLayout.NORTHonly has space for 1 element. If you add 3 things, the 2nd will overwrite the 1st, and the 3th will overwrite the 2nd.
What you need to do, is first add the 3 buttons to a JPanel, then add this JPanel to the BorderLayout.NORTH




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users