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;
}
}

