Program Help

Hey,

Hey Im trying to create a solitaire game but I can't figure out how to get a mouse event to work so when someone clicks on a card it'll test what it is and stack it on other appropriate cards. And it'll also stack the cards on the top piles Ace - king. Any ideas on the concept or if you have the code it'd be much appreciated!

import java.util.Vector;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.Image;

import java.awt.Graphics;

publicclass Solitareextends JAppletimplements MouseListener,

MouseMotionListener

{

private Vector<Integer> random =new Vector<Integer>();

private Vector<Integer> placed =new Vector<Integer>();

private Vector<String> aboutCard =new Vector<String>();

privateint[] cardType ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,52};

privateint[] num ={3,52,24,12,16,21,25,32,37,45,9,11,22,28,1,13,33,51,27,7,46,5,41,19,40,14,34,43,39,4,31,49,10,18,47,15,23,38,48,2,44,35,29,6,20,26,42,8,36,17,30};

private Image[] cards =new Image[52];

private Image[] back =new Image[1];

private Image[] noCard =new Image[1];

private Image[] hlight =new Image[1];

private Image[] bgImage =new Image[1];

private String graphic;

private Graphics e;

privateint n;

privateint x, y, i, p, u, c=0;

privateboolean cardClicked = false, Clicked = false, highlight =false;

publicvoid init()

{

addMouseListener(this);

setSize(800,600);

//generates the back of the card

graphic ="200.gif";

back[0] = getImage (getCodeBase(),graphic);

//this is used to set the background image

graphic ="art.jpg";

bgImage[0] = getImage(getCodeBase(), graphic);

/*displays a no-card image. This is used for the starting places where the cards

will be placed Ace - king. Also used for when the person cycles through

the deck.

*/

graphic ="nocard.gif";

noCard[0] = getImage (getCodeBase(),graphic);

graphic ="overlay.gif";

hlight[0]= getImage(getCodeBase(),graphic);

//random cards are generated and put into an array and used later.

for (int x =0; x<51; x++)

{

graphic =""+ (num[x])+".gif";

cards[x] = getImage (getCodeBase(),graphic);

//insert the numbers of the cards into a vector

for(int d =0; d < 51; d++){

random.insertElementAt(d, d);

}

}

}

publicvoid mouseClicked(MouseEvent event)

{}

publicvoid mouseEntered(MouseEvent event)

{}

publicvoid mouseExited(MouseEvent event)

{}

publicvoid mousePressed(MouseEvent event)

{}

publicvoid mouseReleased(MouseEvent event)

{

x = event.getX();

y = event.getY();

testClick(x, y);

}

publicvoid mouseDragged(MouseEvent event)

{

}

publicvoid mouseMoved(MouseEvent event)

{}

publicvoid paint(Graphics e){

super.paint(e);

e.drawImage(bgImage[0], 0, 0,this);

e.drawImage(back[0],0,0,this);

//this is printed once as the applet is loaded.

if(cardClicked ==false){

for(int x =100; x <= 460; x+=120){

e.drawImage(noCard[0],x+140,0,this);

}

int f=0;

for(int h=600; h >= 0; h = h - 100){

f+=10;

for(int k =0; k < 70-f; k+=10){

e.drawImage(back[0],h*1,k+120,this);

}

}

//this is used to place cards on the applet on the individual stacks.

p=10;

u=0;

i=0;

for(int t=600; t >= 0; t-=100){

u+=1;

e.drawImage(cards[random.elementAt(i)],t,190-(u*p),this);

placed.insertElementAt(random.elementAt(i), i);

random.removeElementAt(i);

i++;

}

}

//tests wether the card pile was clicked on.

if(cardClicked){

c+=1;

cardClicked(e, c);

}

}

publicvoid testClick(int x,int y){

if(x >=0 && x <= 72 && y >=0 && y <= 96){

cardClicked =true;

repaint(80, 0, 72, 96);

}

}

//generates a card to the right of the pile.

publicvoid cardClicked(Graphics e,int n){

if(n <= 23){

e.drawImage(cards[random.elementAt(n)], 80, 0,this);

}

else

c=0;

}

}

[9229 byte] By [Jingles87a] at [2007-11-27 3:44:47]
# 1

http://java.sun.com/j2se/1.3/docs/guide/awt/designspec/events.html

If you follow this event model it would help. To be able to follow this, your design would need to change a bit. Create a Card class, which would contain all pertinent information about a single card, i.e. suite, number, image, backImage...etc. Then, you would register the appropriate listener and handle the events appropriately. Very similar to mouse clicks for a button in Swing.

kdajania at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 2
well I was kind of hoping for an example as to how other people have handled doing the events in the game solitaire. I know someone has done this game before so I was just hoping to see a completed version of it.Message was edited by: Jingles87
Jingles87a at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 3
So you could steal his code?
-Kayaman-a at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 4
no so I can understand the concepts behind placing and moving and all that good stuff.
Jingles87a at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 5

I don't have that code, but it shouldn't be too difficult.

If you break down all your objects, their actions, and events down, you should be able to achieve your goal because it makes it a bit simpler to think about and organize. You already know the logic from having played the game. It's really just some of the technicals. I am sure if you attempt it and run into problems, many here will help. Attempting it will also give you a better understanding of how it all works.

kdajania at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 6

I dont mean to be an jerk, but I've obviouly done some of the work Im just trying to finish my program. I know it's the technical stuff that I need to figure out but thats why I posted on this forum because I dont know how I can get it to register where the person clicked what card was placed there. If there is a card that it can be placed on. If its an ace then have it go to the top. Once again im not trying to steal anyones code I'm just trying to figure out how to finish the game. I've obviously done a bit of work on it. I have the layout, the pile is clickable so the only thing I need is the mouse event to move the cards. I have attempted to figure this out for over a week ....

Jingles87a at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 7

That will all go with ur design. Again, you would want to do something like this

public class Card {

private Image backgroundImage;

private Image faceImage;

private int value; // 2-Ace

private Suite mySuite;

/**

* Constructor

*/

public Card () {}

// .. a bunch of ur other code such setters and getters would be done here.

// Note(xxx): refer to the event model link to see a more comprehensive example

}

In your game class, you would then register the listeners ...etc

public class Game implements ActionListener { // or MouseListener

private Card [] myCards = new Card[52];// all 52 cards

// ... any other fields

public Game () {

// add initialization code

// init each card as well and do something along the lines of

card.addListener (this); // this register your object with the listener so it knows when an event on the object has taken place

}

/**

* This method is where you want to check what action was performed and act accordingly

*/

public void ActionPerformed (ActionEvent e) {

// if an ace was clicked, do such ...etc

}

}

Clearly this isn't complete, but it is a quick outline of what you want to do. There is no point in re-iterating what's already in the link provided. It's a pretty simple explanation, easy to follow guide to help you, with examples.

Basically what happens is you would create your card(s) object, and register it to listen for mouse events. Based on the event, you do what it is you need to do.

I understand your frustration, especially since you have been playing with it for a while. To supply all the technicals would be redundant because it is already out there. If there is a specific part of it that you don't understand, or have a problem with code, help is there.

kdajania at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 8
Ok I dont understand how to tell where a person clicks the value of that card. And also how I could test the first card with other random cards that were placed on the Applet.
Jingles87a at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...
# 9

Here is the java sun tutorial, much better then the first link

http://java.sun.com/docs/books/tutorial/uiswing/events/index.html

Each action will have an associated source object. If you check out the event API, there are ways to get the information you need about the event. e.getObject() for example will tell you the source of the event, or where the event was fired from. Once you get that object, you have information in that object to tell you what it is. In your case, it will be a card. Then you can check, myCard.getSutie(), and myCard.getNumber() or whatever the methods will be called.

If you add MouseListeners on your card objects to listen for mouse events, the applet will fire the events on its own, your code receives them, and does the appropriate process afterwards.

kdajania at 2007-7-12 8:48:27 > top of Java-index,Java Essentials,Java Programming...