Basic animation - moving cards
Hi all,
As I'm fairly new to java programming for games I thought I would try my hand at something simple like a 2D card game. The card game I picked was Canasta although you probably don't need to know that it might help you understand my method.
The code sample is from one class of many called GameWindow which basically does what it says on the tin. What I want to do is create a simple animation where I can move a card positioned at point A in the frame to point B in x number of seconds, for example if I was dealing a card to a player across the table. In Flash this would be the equivalent of a motion-tween as I want to give the impression the card is moving otherwise I could just repaint the card at Point B on the screen but that wouldn't look all that nice! I had a hunt around Google and found a class called Move which appeared to do just that (http://www.ssw.uni-linz.ac.at/Services/OnlineTools/Games/PSW2/BlackJack/doc/javadoc/Move.html) but I couldn't figure out how to get it to work in my environment. I also hunted around for other ways to create an animation to do this but the only code I found was hundreds of lines long so I'm thinking there must be s simple way to do what I wish to accomplish.
Here is the code for the class. I know it won't compile on its own but it should at least help you to have some sort of visual in your head of what is going on.
import java.awt.*;
import javax.swing.*;
import org.apache.log4j.Logger;
publicclass GameWindowextends JApplet
{
publicstaticint i = 0;
publicstaticint d = 0;
publicstaticboolean newUserName =false;
publicstaticboolean isPainted =false;
publicstaticboolean isAvPainted =false;
publicstaticint comp1_score;
publicstaticint player1_score;
publicstaticint card_count;
static Logger log = Logger.getLogger(GameWindow.class.getName());
public Image loadImage(String name)
{
Image result =null;
MediaTracker tracker =new MediaTracker(this);
Toolkit toolkit = Toolkit.getDefaultToolkit();
result = toolkit.getImage(name);
tracker.addImage(result, 0);
try
{
tracker.waitForID(0);
}catch (InterruptedException e)
{
returnnull;
}
return result;
}
publicvoid paint(Graphics g)
{
log.info("Loading background image into memory");
Image background = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\new_bg.jpg");
log.info("Displaying main image");
g.drawImage(background, 0, 0,null);
if (isAvPainted ==true){
log.info("Load and display avatars");
Image bot_av_bg = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\bot_bg_test_none2.gif");
g.drawImage(bot_av_bg, 192, 4,null);
Image bot_av_norm = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\bot_norm.jpg");
g.drawImage(bot_av_norm, 193, 5,null);
Image player_av_bg = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\player_av_bg.gif");
g.drawImage(player_av_bg, 180, 313,null);
Image player_av_happy = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\player_happy2.jpg");
g.drawImage(player_av_happy, 183, 316,null);
g.drawString("Mr. Meld", 270, 18);
//scores
log.info("Display scores");
String comp1_score_string = Integer.toString(comp1_score);
g.drawString(comp1_score_string, 384, 123);
String player1_score_string = Integer.toString(player1_score);
g.drawString(player1_score_string, 156, 121);
//card count
log.info("Display card count");
String card_count_string = Integer.toString(card_count);
g.drawString("(" + card_count_string +")", 147, 230);
Image card0 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card0.gif");
Image card1 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card1.gif");
Image card2 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card2.gif");
Image card3 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card3.gif");
Image card4 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card4.gif");
Image card5 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card5.gif");
g.drawImage(card0, 180, 175,null);
g.drawImage(card1, 185, 175,null);
g.drawImage(card2, 190, 175,null);
g.drawImage(card3, 195, 175,null);
g.drawImage(card4, 200, 175,null);
g.drawImage(card5, 205, 175,null);
}
g.drawString(StartWindow.getUserName(), 250, 357);
log.info("Image background is now displayed");
log.debug("VALUE OF i BEFORE for loop is " + i +"!!!");
for (; i < 1; i++){
log.debug("VALUE OF i IN FOR loop is " + i +"!!!");
log.info("Loading title image into memory");
Image title = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\title.gif");
g.drawImage(title, 111, 120,null);
log.info("Image title is now displayed");
log.info("Start the opening sound sequence");
//try { Thread.sleep(1000); } catch (InterruptedException e) {} // add initial delay for JBuilder workaround
new SoundApplet("sounds/C_Start2.au");
try{
Thread.sleep(3400);
}catch (InterruptedException e){}
repaint();
}
log.debug("VALUE OF i AFTER IF loop is " + i +"!!!");
if (newUserName ==true && isPainted ==false){
newUserName =false;
isPainted =true;
repaint();
}
if (isPainted ==true && isAvPainted ==false){
isAvPainted =true;
Main.StartGame();
repaint();
}
}
}
This probably isn't the best piece of code in the world so if anyone has some constructive criticism about it, feel free to elaborate.
Thanks in advance
This is about as simple and straightę
orward as I could make it.
It is set up for generics (j2se 1.5+).
If you are using an earlier j2se version then change these statements
List<BufferedImage> cardImages;
cardImages = new ArrayList<BufferedImage>(Arrays.asList(images));
to these
List cardImages;
cardImages = new ArrayList(Arrays.asList(images));
and you'll have to add a cast to BufferedImage for each get statement.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CardMover implements ActionListener {
CardTable cardTable;
CardDealer cardDealer;
public CardMover(BufferedImage[] images) {
cardTable = new CardTable(images);
cardDealer = new CardDealer(cardTable);
}
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if(ac.equals("DEAL"))
cardDealer.dealCard();
if(ac.equals("RELOAD")) {
if(!cardDealer.dealing)
cardTable.resetCards();
}
if(ac.equals("AUTO"))
cardDealer.dealCards();
}
private JPanel getCardTable() {
return cardTable;
}
private Box getControls() {
String[] ids = { "Deal", "Reload", "Auto" };
Box box = Box.createHorizontalBox();
box.add(box.createHorizontalGlue());
for(int j = 0; j < ids.length; j++) {
JButton button = new JButton(ids[j]);
button.setActionCommand(ids[j].toUpperCase());
button.addActionListener(this);
box.add(button);
box.add(box.createHorizontalGlue());
}
return box;
}
public static void main(String[] args) throws IOException {
String path = "playingCards/";
BufferedImage[] images = new BufferedImage[52];
for(int j = 0, k = 1; j < images.length; j++, k++)
images[j] = ImageIO.read(new File(path + k + ".jpg"));
CardMover test = new CardMover(images);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getCardTable());
f.getContentPane().add(test.getControls(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class CardTable extends JPanel {
List<BufferedImage> cardImages;
Rectangle left;
Rectangle right;
int x;
int y;
int lastCard;
int currentCard;
int nextCard;
final int PAD = 20;
public CardTable(BufferedImage[] images) {
cardImages = new ArrayList<BufferedImage>(Arrays.asList(images));
resetCards();
}
public void moveCard(double x, double y) {
this.x = (int)x;
this.y = (int)y;
repaint();
}
public void next() {
if(nextCard+1 > cardImages.size()-1)
resetCards();
lastCard = currentCard;
currentCard = nextCard;
nextCard++;
x = left.x;
y = left.y;
//System.out.printf("nextCard = %d%n", nextCard);
}
public void resetCards() {
shuffle();
lastCard = -2;
currentCard = -1;
nextCard = 0;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if(left == null)
initRects();
g2.draw(left);
g2.draw(right);
g2.drawImage(cardImages.get(nextCard), left.x, left.y, this);
if(lastCard > -1)
g2.drawImage(cardImages.get(lastCard), right.x, right.y, this);
if(currentCard > -1)
g2.drawImage(cardImages.get(currentCard), x, y, this);
}
private void shuffle() {
for(int j = 0; j < 6; j++)
Collections.shuffle(cardImages);
}
private void initRects() {
int w = getWidth();
int h = getHeight();
int iw = cardImages.get(0).getWidth();
int ih = cardImages.get(0).getHeight();
int x = w - iw - PAD;
int y = (h - ih)/2;
left = new Rectangle(PAD,y,iw,ih);
right = new Rectangle(x,y,iw,ih);
this.x = left.x;
this.y = left.y;
}
}
class CardDealer implements Runnable {
CardTable cardTable;
Thread thread;
boolean dealing = false;
int count = 0;
final double SPEED = 3.0;
final long DELAY = 25;
final int MAX_CARDS = 5;
public CardDealer(CardTable ct) {
cardTable = ct;
}
public void dealCard() {
start();
}
public void dealCards() {
if(!dealing) {
Thread thread = new Thread(runner);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
public void run() {
while(dealing) {
try {
Thread.sleep(DELAY);
} catch(InterruptedException e) {
System.out.println("Dealing thread interrupted");
stop();
}
boolean finished = advanceCard();
if(finished) {
stop();
}
}
}
public void start() {
if(!dealing) {
cardTable.next();
count = 0;
dealing = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
private void stop() {
dealing = false;
if(thread != null)
thread.interrupt();
thread = null;
}
private boolean advanceCard() {
Point p1 = cardTable.left.getLocation();
Point p2 = cardTable.right.getLocation();
double distance = p1.distance(p2);
double d = ++count*SPEED;
double x = p1.x + d;
if(d > distance)
x -= d - distance;
cardTable.moveCard(x, p1.y);
return d > distance;
}
private Runnable runner = new Runnable() {
public void run() {
int count = 0;
final int WAIT_FOR_END_OF_DEAL = 50;
while(count++ < MAX_CARDS) {
dealCard();
while(dealing) {
try {
Thread.sleep(WAIT_FOR_END_OF_DEAL);
} catch(InterruptedException e) {
System.out.println("runner thread interrupted");
stop();
}
}
}
}
};
}
Thanks for that. It seems fairly straight-forward to implement.
I just had one question about the code. Is it possible to hide the border of the rectangles where the cards start and finish? I looked at the methods associated with object Rectangle and I couldn't see anything that looked like it might do that.
hide the border of the rectangles where the cards start and finish
Yes, since they are drawn (in paintComponent) either comment them (the g2.draw
(left/right) lines) out or remove them or, if you want to toggle the visibility you can guard
them with a boolean member variable (class scope) that is controlled from your event code, eg,
class CardTable extends JPanel {
boolean showRects = false;
...
protected void paintComponent(Graphics g) {
...
if(showRects) {
g2.draw(left);
g2.draw(right);
}
Yup that worked.
I've now tried integrating the code into my existing frame and everything seems to be fine except when the repaint() command is called from the CardTable class it's using the paint method from GameWindow class and not the paintComponent method from the CardTable class like I would expect it to.
This is the code I now have.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
public class GameWindow extends JApplet{
public static int i = 0;
public static int d = 0;
public static boolean newUserName = false;
public static boolean isPainted = false;
public static boolean isAvPainted = false;
public static int comp1_score;
public static int player1_score;
public static int card_count;
static Logger log = Logger.getLogger(GameWindow.class.getName());
public Image loadImage(String name){
Image result = null;
MediaTracker tracker = new MediaTracker(this);
Toolkit toolkit = Toolkit.getDefaultToolkit();
result = toolkit.getImage(name);
tracker.addImage(result, 0);
try
{
tracker.waitForID(0);
} catch (InterruptedException e)
{
return null;
}
return result;
}
public void paint(Graphics g){
log.info("Loading background image into memory");
Image background = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\new_bg.jpg");
log.info("Displaying main image");
g.drawImage(background, 0, 0, null);
if (isAvPainted == true) {
log.info("Load and display avatars");
Image bot_av_bg = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\bot_bg_test_none2.gif");
g.drawImage(bot_av_bg, 192, 4, null);
Image bot_av_norm = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\bot_norm.jpg");
g.drawImage(bot_av_norm, 193, 5, null);
Image player_av_bg = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\player_av_bg.gif");
g.drawImage(player_av_bg, 180, 313, null);
Image player_av_happy = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\player_happy2.jpg");
g.drawImage(player_av_happy, 183, 316, null);
g.drawString("Mr. Meld", 270, 18);
//scores
log.info("Display scores");
String comp1_score_string = Integer.toString(comp1_score);
g.drawString(comp1_score_string, 384, 123);
String player1_score_string = Integer.toString(player1_score);
g.drawString(player1_score_string, 156, 121);
//card count
log.info("Display card count");
String card_count_string = Integer.toString(card_count);
g.drawString("(" + card_count_string + ")", 147, 230);
Image card0 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card0.gif");
Image card1 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card1.gif");
Image card2 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card2.gif");
Image card3 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card3.gif");
Image card4 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card4.gif");
Image card5 = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\card5.gif");
g.drawImage(card0, 180, 175, null);
g.drawImage(card1, 185, 175, null);
g.drawImage(card2, 190, 175, null);
g.drawImage(card3, 195, 175, null);
g.drawImage(card4, 200, 175, null);
g.drawImage(card5, 205, 175, null);
}
g.drawString(StartWindow.getUserName(), 250, 357);
log.info("Image background is now displayed");
log.debug("VALUE OF i BEFORE for loop is " + i + "!!!");
for (; i < 1; i++) {
log.debug("VALUE OF i IN FOR loop is " + i + "!!!");
log.info("Loading title image into memory");
Image title = loadImage("C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\title.gif");
g.drawImage(title, 111, 120, null);
log.info("Image title is now displayed");
log.info("Start the opening sound sequence");
//try { Thread.sleep(1000); } catch (InterruptedException e) {} // add initial delay for JBuilder workaround
new SoundApplet("sounds/C_Start2.au");
try {
Thread.sleep(3400);
} catch (InterruptedException e) {}
repaint();
}
log.debug("VALUE OF i AFTER IF loop is " + i + "!!!");
if (newUserName == true && isPainted == false) {
newUserName = false;
isPainted = true;
repaint();
}
if (isPainted == true && isAvPainted == false) {
isAvPainted = true;
Main.StartGame();
repaint();
}
}
public static void run() throws IOException {
String path = "C:\\Documents and Settings\\Paulo\\jbproject\\Canasta_Game\\src\\images\\";
BufferedImage[] images = new BufferedImage[10];
for(int j = 0, k = 0; j < images.length; j++, k++)
images[j] = ImageIO.read(new File(path + "card" + k + ".gif"));
CardMover test = new CardMover(images);
Main.frame.add(test.getCardTable());
Main.frame.add(test.getControls(), "Last");
}
}
class CardMover implements ActionListener {
CardTable cardTable;
CardDealer cardDealer;
public CardMover(BufferedImage[] images) {
GameWindow.log.debug("Create new instances of cardTable and cardDealer");
cardTable = new CardTable(images);
cardDealer = new CardDealer(cardTable);
}
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
cardDealer.dealCards();
}
public JPanel getCardTable() {
return cardTable;
}
public Box getControls() {
Box box = Box.createHorizontalBox();
JButton button = new JButton("Deal");
button.addActionListener(this);
box.add(button);
return box;
}
}
class CardTable extends JPanel {
List<BufferedImage> cardImages;
Rectangle left;
Rectangle right;
int x;
int y;
int lastCard;
int currentCard;
int nextCard;
public CardTable(BufferedImage[] images) {
cardImages = new ArrayList<BufferedImage>(Arrays.asList(images));
resetCards();
}
public void moveCard(double x, double y) {
this.x = (int)x;
this.y = (int)y;
repaint();
}
public void next() {
lastCard = currentCard;
currentCard = nextCard;
nextCard++;
if(nextCard == 10)
resetCards();
x = left.x;
y = left.y;
}
public void resetCards() {
lastCard = -2;
currentCard = -1;
nextCard = 0;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if(left == null)
initRects();
g2.drawImage(cardImages.get(nextCard), left.x, left.y, this);
if(lastCard > -1)
g2.drawImage(cardImages.get(lastCard), right.x, right.y, this);
if(currentCard > -1)
g2.drawImage(cardImages.get(currentCard), x, y, this);
}
private void initRects() {
int iw = cardImages.get(0).getWidth();
int ih = cardImages.get(0).getHeight();
left = new Rectangle(100,100,iw,ih);
right = new Rectangle(190,100,iw,ih);
this.x = left.x;
this.y = left.y;
}
}
class CardDealer implements Runnable {
CardTable cardTable;
Thread thread;
boolean dealing = false;
int count = 0;
final double SPEED = 20.0; // speed of cards
final long DELAY = 15; // delay between cards
final int MAX_CARDS = 10; //cards to deal in the sequence
public CardDealer(CardTable ct) {
cardTable = ct;
}
public void dealCard() {
start();
}
public void dealCards() {
if(!dealing) {
Thread thread = new Thread(runner);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
new SoundApplet("sounds/C_Shuffle.au");
}
}
public void run() {
while(dealing) {
try {
Thread.sleep(DELAY);
} catch(InterruptedException e) {
GameWindow.log.error("Dealing thread interrupted");
stop();
}
boolean finished = advanceCard();
if(finished) {
stop();
}
}
}
public void start() {
if(!dealing) {
cardTable.next();
count = 0;
dealing = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
private void stop() {
dealing = false;
if(thread != null)
thread.interrupt();
thread = null;
}
private boolean advanceCard() {
Point p1 = cardTable.left.getLocation();
Point p2 = cardTable.right.getLocation();
double distance = p1.distance(p2);
double d = ++count*SPEED;
double x = p1.x + d;
if(d > distance)
x -= d - distance;
cardTable.moveCard(x, p1.y);
return d > distance;
}
Runnable runner = new Runnable() {
public void run() {
int count = 0;
final int WAIT_FOR_END_OF_DEAL = 50;
while(count++ < MAX_CARDS) {
dealCard();
while(dealing) {
try {
Thread.sleep(WAIT_FOR_END_OF_DEAL);
} catch(InterruptedException e) {
GameWindow.log.error("runner thread interrupted");
stop();
}
}
}
}
};
}
except when the repaint() command is called from the CardTable class it's using the
paint method from GameWindow class and not the paintComponent method from the CardTable
class like I would expect it to
1 - Your paint method has a loop in it with a sleep statement and calls to repaint.
Calling repaint inside a painting method causes an endless loop like a dog chasing its
tail. Your applet will be tied up with endless calls to paint and repaint.
2 - We never put event code inside a paint method, put that in other methods controlled by
user interaction and/or threads/timers.
3 - Don't load images inside any paint method. Load them in init at startup or in a
background thread or in a method called from event code.
A suggestion for convenience:
In java you can specify image paths with a single forward slash. Java takes care of
translating this into the operating system, including windows.
Since you have a long path prefix you could save it in a String for later use.
String prefix = "C:/Documents and Settings/Paulo/jbproject/" +
"Canasta_Game/src/images/images/";
Image background = loadImage(prefix + "new_bg.jpg");
I got your applet to work with minimal changes. The rest of this code belongs in event
code (or somewhere else besides paint)
new SoundApplet("sounds/C_Start2.au");
try {
Thread.sleep(3400);
} catch (InterruptedException e) {}
repaint();
}
log.debug("VALUE OF i AFTER IF loop is " + i + "!!!");
if (newUserName == true && isPainted == false) {
newUserName = false;
isPainted = true;
repaint();
}
if (isPainted == true && isAvPainted == false) {
isAvPainted = true;
Main.StartGame();
repaint();
}
// <applet code="GameWindowRx" width="800" height="600"></applet>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;
public class GameWindowRx extends JApplet {
public static int i = 0;
public static int d = 0;
public static boolean newUserName = false;
public static boolean isPainted = false;
public static boolean isAvPainted = false;
public static int comp1_score;
public static int player1_score;
public static int card_count;
BufferedImage[] images;
BufferedImage[] cards;
public void init() {
CardMover test = new CardMover(getImages());
JPanel cardTable = test.getCardTable();
cardTable.setPreferredSize(new Dimension(400,160));
JPanel south = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
south.add(cardTable, gbc);
south.add(test.getControls(), gbc);
loadImages();
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(graphicsPanel);
cp.add(south, "Last");
}
JPanel graphicsPanel = new JPanel() {
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(images[0], 0, 0, null);
if (isAvPainted != true) {
g.drawImage(images[1], 192, 4, null);
g.drawImage(images[2], 193, 5, null);
g.drawImage(images[3], 180, 313, null);
g.drawImage(images[4], 183, 316, null);
g.drawString("Mr. Meld", 270, 18);
//scores
String comp1_score_string = Integer.toString(comp1_score);
g.drawString(comp1_score_string, 384, 123);
String player1_score_string = Integer.toString(player1_score);
g.drawString(player1_score_string, 156, 121);
//card count
String card_count_string = Integer.toString(card_count);
g.drawString("(" + card_count_string + ")", 147, 230);
g.drawImage(cards[0], 180, 175, null);
g.drawImage(cards[1], 185, 175, null);
g.drawImage(cards[2], 190, 175, null);
g.drawImage(cards[3], 195, 175, null);
g.drawImage(cards[4], 200, 175, null);
g.drawImage(cards[5], 205, 175, null);
}
//g.drawString(StartWindow.getUserName(), 250, 357);
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
private void loadImages() {
String prefix = "C:/Documents and Settings/Paulo/jbproject/" +
"Canasta_Game/src/images/";
//"images/geek";
String[] fileNames = {
//"-c", "-cg--", "-c-h-", "-c--t", "--g--", "h-"
"new_bg.jpg", "bot_bg_test_none2.gif", "bot_norm.jpg",
"player_av_bg.gif", "player_happy2.jpg", "title.gif"
};
images = new BufferedImage[fileNames.length];
for(int j = 0; j < images.length; j++) {
String path = prefix + fileNames[j];
//+ ".gif";
try {
images[j] = ImageIO.read(new File(path));
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
}
cards = new BufferedImage[6];
for(int j = 0; j < cards.length; j++) {
String path = prefix + "card" + j + ".gif";
//"playingCards/" + (j+1) + ".jpg";
try {
cards[j] = ImageIO.read(new File(path));
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
}
}
private BufferedImage[] getImages() {
String path = "C:/Documents and Settings/Paulo/" +
"jbproject/Canasta_Game/src/images/";
BufferedImage[] images = new BufferedImage[10];
for(int j = 0, k = 0; j < images.length; j++, k++) {
try {
images[j] = ImageIO.read(new File(path + "card" + k + ".gif"));
//"playingCards/" + (j+1) + ".jpg"));
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
}
return images;
}
}
