Clipped image when g.drawImage(img, -3, -4, this)  
Author Message
A. Farber





PostPosted: 2007-7-19 7:19:00 Top

java-programmer, Clipped image when g.drawImage(img, -3, -4, this) Hello,

I've created a class (full source code at the bottom)
which extends java.awt.Component and is supposed to
represent a playing card. If the card is being dragged,
I'd like to draw a shadow underneath it. And the card
itself should be drawn a little bit displaced - to make
the impression that it has been lifted:

public void paint(Graphics g) {
if (this == dragged) {
g.drawImage(shadow, 0, 0, this);
g.drawImage(cardImg, -3, -4, this);
} else {
g.drawImage(cardImg, 0, 0, this);
}
}

My problem is however that the cardImg is being
clipped. Does anybody please have an idea how
to workaround this?

Thank you
Alex

PS: Here is the full source code:

// $Id: Card.java,v 1.2 2007/07/18 10:10:33 afarber Exp $

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

class Card extends Component implements MouseListener,
MouseMotionListener
{
public static final int WIDTH = 70;
public static final int HEIGHT = 100;

public static final int SPADE = 0;
public static final int CLUB = 1;
public static final int DIAMOND = 2;
public static final int HEART = 3;
public static final int NOTRUMP = 4;

private static Card dragged;

private static Image[][] faces;
private static Image back;
private static Image shadow;

public byte rank, suit;
public boolean opened;
public int whose;

public Card(int rank, int suit) {
setSize(WIDTH, HEIGHT);
addMouseListener(this);
addMouseMotionListener(this);

this.rank = (byte) rank;
this.suit = (byte) suit;
}

public Card(char ch) {
setSize(WIDTH, HEIGHT);
addMouseListener(this);
addMouseMotionListener(this);

rank = (byte) (ch >> 8);
suit = (byte) ch;
}

public boolean equals(Card card) {
return (rank == card.rank && suit == card.suit);
}

public char toChar() {
return (char) ((rank << 8) | suit);
}

public void paint(Graphics g) {
if (this == dragged) {
g.drawImage(shadow, 0, 0, this);

// XXX the image below is clipped :-(
g.drawImage(opened ? faces[rank][suit] : back,
-3, -4, this);
} else {
g.drawImage(opened ? faces[rank][suit] : back,
0, 0, this);
}
}

public static void prepImages(Image big) {
ImageProducer source = big.getSource();

// create 32 card images
faces = new Image[8][4];
for (int rank = 0; rank < 8; rank++)
for (int suit = SPADE; suit <= HEART; suit++) {
ImageFilter filter =
new CropImageFilter(rank * WIDTH,
suit * HEIGHT, WIDTH, HEIGHT);
ImageProducer producer = new
FilteredImageSource(source, filter);
faces[rank][suit] = Toolkit.getDefaultToolkit()
.createImage(producer);
}
// create the image of a card's back
ImageFilter filter =
new CropImageFilter(560, 0, WIDTH, HEIGHT);
ImageProducer producer =
new FilteredImageSource(source, filter);
back = Toolkit.getDefaultToolkit().createImage(producer);
// use a card shape to create shadow
int[] pixels = new int[WIDTH * HEIGHT];
PixelGrabber grabber = new PixelGrabber(big, 0, 0,
WIDTH, HEIGHT, pixels, 0, WIDTH);
try {
grabber.grabPixels();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
// turn non-transparent pixels to shadow
for (int i = 0; i < pixels.length; i++)
if (0 != (pixels[i] & 0xFF000000))
pixels[i] = 0x60000000;
shadow = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(WIDTH, HEIGHT, pixels, 0, WIDTH));
}

public void mouseExited(MouseEvent event) {
System.out.println("mouseExited:" + event);
dragged = null;
getParent().repaint();
}

public void mouseReleased(MouseEvent event) {
System.out.println("mouseReleased" + event);
dragged = null;
getParent().repaint();
}

public void mousePressed(MouseEvent event) {
System.out.println("mousePressed" + event);
dragged = this;
getParent().repaint();
}

public void mouseEntered(MouseEvent event) {
System.out.println("mouseEntered" + event);
}

public void mouseClicked(MouseEvent event) {
System.out.println("mouseClicked" + event);
}

public void mouseMoved(MouseEvent event) {
System.out.println("mouseMoved" + event);
}

public void mouseDragged(MouseEvent event) {
System.out.println("mouseDragged" + event);
}

public static int randomRank() {
return (int) Math.floor(8.0 * Math.random());
}

public static int randomSuit() {
return (int) Math.floor(4.0 * Math.random());
}

public static void main(String args[]) {
Frame frame = new Frame("Card Test");
frame.setForeground(Color.white);
frame.setBackground(Color.gray);
frame.setLayout(null);

final String PATH = "media/cards.gif";
Image big = Toolkit.getDefaultToolkit().getImage(PATH);
MediaTracker tracker = new MediaTracker(frame);
tracker.addImage(big, 0);
try {
tracker.waitForAll();
} catch (Exception ex) {
ex.printStackTrace();
}
if (tracker.isErrorAny()) {
System.err.println("Image " + PATH + " not found");
return;
}

Card.prepImages(big);
Card card = new Card(randomRank(), randomSuit());
card.opened = true;
frame.add(card);
frame.validate();
card.setLocation(200, 100);

frame.setSize(400, 300);
frame.setVisible(true);
}
}

 
Roedy Green





PostPosted: 2007-7-19 11:38:00 Top

java-programmer >> Clipped image when g.drawImage(img, -3, -4, this) On Wed, 18 Jul 2007 23:18:54 -0000, "A. Farber"
<email***@***.com> wrote, quoted or indirectly quoted
someone who said :

>frame.validate();
> card.setLocation(200, 100);
>
> frame.setSize(400, 300);
You need to force the size of the card.

Dimension d = new Dimension( width, height );
this.setPreferredSize( d );
this.setMinimumSize( d );
this.setMaximumSize( d );
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
A. Farber





PostPosted: 2007-7-20 17:03:00 Top

java-programmer >> Clipped image when g.drawImage(img, -3, -4, this) On Jul 19, 5:38 am, Roedy Green <email***@***.com>
wrote:
> >frame.validate();
> > card.setLocation(200, 100);
>
> > frame.setSize(400, 300);
>
> You need to force the size of the card.
>
> Dimension d = new Dimension( width, height );
> this.setPreferredSize( d );
> this.setMinimumSize( d );
> this.setMaximumSize( d );

No, I'd like the parent frame to be bigger,
so that I can drag my Card Component around.

Or have I misunderstood you?

Regards
Alex