I coded this a few weeks ago:
public Image[] getCards(Image img) {
Image[] temp = new Image[52];
int j = 0;
ImageFilter filter;
ImageProducer prod = img.getSource();
for (int y = 0;y < 384;y+=96) {
for (int x = 0;x < 923;x+=71) {
filter = new CropImageFilter(x,y,71,96);
temp[j] = createImage(new FilteredImageSource(prod,filter));
//tracker.addImage(temp[j],j);
j++;
}
}
return temp;
}
Just use it like:
Image[] cards = getCards(getCodeBase(),"cards.gif");
and then you use cards[number] to access the image. the tracker.addImage that's commented out is used for a mediatracker, becuase on some systems it takes a little bit to crop all of it.
what do you want to be static...? Here's a full-working example:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
// <applet code="CardSolitaire.class" width=400 height=400></applet>
// cards.gif must be in same directory!
public class CardSolitaire extends Applet implements Runnable,KeyListener,MouseListener,MouseMotionListener {
Image b,cards[],img;
Graphics gfx;
MediaTracker tracker;
int card,w,h,x,y;
Dimension d;
boolean loaded,over;
Rectangle r;
public void init() {
loaded = false;
cards = new Image[52];
card = 0;
tracker = new MediaTracker(this);
d = getSize();
w = d.width;
h = d.height;
b = createImage(w,h);
r = new Rectangle(10,100,71,96);
x = 10;
y = 100;
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
new Thread(this).start();
}
public Image[] getCards() {
Image[] temp = new Image[52];
int j = 0;
ImageFilter filter;
ImageProducer prod = img.getSource();
for (int y = 0;y < 384;y+=96) {
for (int x = 0;x < 923;x+=71) {
filter = new CropImageFilter(x,y,71,96);
temp[j] = createImage(new FilteredImageSource(prod,filter));
tracker.addImage(temp[j],j);
j++;
}
}
return temp;
}
public void run() {
while (true) {
if (!loaded) {
img = getImage(getCodeBase(),"cards.gif");
tracker.addImage(img,0);
try {
tracker.waitForAll();
}
catch (InterruptedException e) {
break;
}
cards = getCards();
try {
tracker.waitForAll();
}
catch (InterruptedException e) {
break;
}
loaded = true;
}
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
break;
}
}
}
public void paint(Graphics g) {
gfx = b.getGraphics();
gfx.setColor(new Color(0,150,0));
gfx.fillRect(0,0,w,h);
gfx.setColor(Color.white);
if (!loaded) {
FontMetrics f = gfx.getFontMetrics();
String s = "Loading...";
gfx.drawString(s,w/2-(f.stringWidth(s)/2),h/2);
}
else {
gfx.drawString(""+card,10,10);
gfx.drawImage(cards[card],r.x,r.y,null);
}
g.drawImage(b,0,0,null);
}
public void update(Graphics g) {
paint(g);
}
public void keyPressed(KeyEvent e) {
if (loaded) switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
card++;
if (card == cards.length) card = 0;
break;
case KeyEvent.VK_DOWN:
card--;
if (card < 0) card = cards.length-1;
break;
default:
break;
}
}
public void mousePressed(MouseEvent e) {
if (loaded && r.contains(e.getX(),e.getY())) {
over = true;
x = e.getX();
y = e.getY();
}
}
public void mouseDragged(MouseEvent e) {
if (loaded && over) {
r.x += (x < e.getX()?e.getX()-x:-(x-e.getX()));
r.y += (y < e.getY()?e.getY()-y:-(y-e.getY()));
x = e.getX();
y = e.getY();
}
}
public void mouseReleased(MouseEvent e) {
over = false;
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}
> just curious, is there a special snippet of code that
> will separate the cards into the ones i want, when i
> want, or do i have to separate the image by hand and
> use 52 separate images?
no need to seperate em by hand... well kinda... but the image stays the way it is :)
g.drawImage can do the trick.
g.drawImage(cardsImage,
on_screen_up_x, on_screen_up_y, //upper left
on_screen_dn_x, on_screen_dn_y, //lower right
card_up_x, card_up_y, //upper left on the image
card_dn_x, card_dn_y, //lower right on the image
null);
after u figured out the dimension of the cards, u can get the desired one with muliplicating.
eg:
card_up_x=col*CARD_WIDTH;
card_up,y=row*CARD_HEIGHT;
card_dn_x=col*CARD_WIDTH+CARD_WIDTH;
card_dn_y=col*CARD_HEIGHT+CARD_HEIGHT;
row is from 0 to 3
col is from 0 to 12 (shifting this one up might be a good idea)
btw i dont know if made any mistakes :x
but it should work ;)
Well, now that i know how to get the images, could you tell me how to use them with a GUI application, as in, where do i get the Graphics? I mean, i have to create a fame, and add buttons to it(this whole idea is a video poker game) and add listeners to the buttons, but how do i add the images? Can i use something like add(image) or... what do i do?
i am a bit confused
thanx!
AWT... well... fine... ok. it's quite simple :)
your class with the drawing action should simply extend Canvas (Canvas is usually taken for that cuz it's an emty component)
public class MySexyCanvas extens Canvas
{
//[...]
public MySexyCanvas()
{
//setup
//[...]
}
public void paint(Graphics g) //overwrite the paint method
{
//all your painting stuff goes here
}
public void update(Graphics g) //overwrite the update method
{
paint(g); //leave it this way... no more needed here
}
}
well it isnt doublebuffered this way. that can be changed quite easily once u got it working (but i dont wanna confuse at this point) :)