I'm making a program in java to emulate a basic deck of cards; no specific game in mind, just a good old deck of cards that you can move around and flip over freely (of course, with the limit of 52 total cards). Here is my current code:
package cards;
import java.awt.Color;
import java.awt.Graphics;
import java.util.List;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class DeckOfCards extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
static final int SIZE = 500;
public static String side = "back";
static final int xSize = 73;
static final int ySize = 98;
static int x = SIZE - xSize;
static int y = 0;
static int i, j = 0;
static int inDeck = 52;
public boolean flipped = false;
static final DeckOfCards m = new DeckOfCards();
static final Color rgb = new Color(0, 180, 10);
static final JFrame frame = new JFrame();
static List<Integer> yList = new ArrayList<Integer>();
static List<Integer> xList = new ArrayList<Integer>();
Random random = new Random();
int randX = random.nextInt(13) * xSize;
int randY = random.nextInt(4) * ySize;
int xRand = randX;
int yRand = randY;
public static void main(String[] args){
frame.setTitle("Virtual Cards");
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(SIZE, SIZE);
frame.setResizable(false);
m.setBackground(rgb);
frame.setVisible(true);
}
public void mouseClick(){
addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
if(((e.getX() >= x && e.getX() <= (x + xSize)) && (e.getY() >= y && e.getY() <= (y + ySize))) && (flipped == false)){
flipped = true;
side = "front";
}else if(((e.getX() >= x && e.getX() <= (x + xSize)) && (e.getY() >= y && e.getY() <= (y + ySize))) && (flipped == true)){
flipped = false;
side = "back";
}else if((e.getX() >= SIZE - xSize && e.getX() <= SIZE) && (e.getY() >= 0 && e.getY() <= SIZE - (SIZE - ySize)) && inDeck > 0){
randCardFace(random);
inDeck--;
side = "back";
x = SIZE - xSize;
y = 0;
}
}
});
}
public void mouseMove(){
addMouseMotionListener(new MouseMotionAdapter(){
@Override
public void mouseDragged(MouseEvent e){
if(((e.getX() >= x && e.getX() <= (x + xSize)) && (e.getY() >= y && e.getY() <= (y + ySize)))){
x = e.getX() - (xSize / 2);
y = e.getY() - (ySize / 2);
}
}
});
}
public DeckOfCards(){
mouseClick();
mouseMove();
}
public void randCardFace(Random random){
randX = random.nextInt(13) * xSize;
randY = random.nextInt(4) * ySize;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
paint(g);
}
public void paint(Graphics g){
try{
if(inDeck > 0){
g.drawImage(ImageIO.read(getClass().getResource("images/deck.png")), SIZE - xSize, 0, null, null);
}
BufferedImage image = ImageIO.read(getClass().getResource("images/"+side+".png"));
if(side != "back"){
image = image.getSubimage(randX, randY, xSize, ySize);
}
g.drawImage(image, x, y, xSize, ySize, null, null);
}catch(IOException e){
e.printStackTrace();
}
m.repaint();
}
}
However, this program will only let me have one card out at a time until I click on the deck in the top right corner and reshuffle(redefine the area of the image to be read with a random number generator). What I would like to do is be able to have any amount of cards from 0 and 52 out at any given time by clicking the deck without creating 51 new variables for the other cards(fyi I have written out some of the code for what I want, but so far it's done nothing but cause problems so I decided to start from scratch and ask the experts what I should do).
edit:
just realized after asking this, this is a slightly older code than I wanted to post, and has probably has plenty of other problems as well. Please ignore those, as they are already fixed.