001 /*
002 * Created on 25.02.2005 Filename: JGameCard.java
003 */
004 package jagafa.view;
005
006 import jagafa.flags.TestingFlags;
007
008 import java.awt.Graphics;
009 import java.awt.Graphics2D;
010 import java.awt.Insets;
011 import java.awt.geom.AffineTransform;
012 import java.awt.image.BufferedImage;
013 import java.net.URL;
014
015 import javax.swing.ImageIcon;
016 import javax.swing.JButton;
017
018 /**
019 * A JButton with a Card image
020 */
021 public class CardButton extends JButton {
022
023 private static final long serialVersionUID = -7302102071467457143L;
024
025 private ImageIcon icon_;
026
027 private int orientation_ = 0;
028
029 /**
030 * Constructor: Create a new CardButton with the Card specified (as a string) and
031 * the card orientation (HORIZONTAL or VERTICAL)
032 */
033 public CardButton(String cardName, int orientation) {
034 super();
035
036 if (cardName != null) {
037 icon_ = this.createImageIcon(TestingFlags.RESOURCE_DIR + cardName
038 + TestingFlags.IMAGE_EXTENSION);
039 this.setVisible(true);
040 } else {
041 icon_ = this.createImageIcon(TestingFlags.RESOURCE_DIR + TestingFlags.EMPTY_IMAGE);
042 this.setVisible(false);
043 }
044 this.setContentAreaFilled(false);
045 this.setDefaultCapable(false);
046 this.setFocusPainted(false);
047 this.setMargin(new Insets(0, 0, 0, 0));
048 this.setFocusable(false);
049 this.setOpaque(true);
050 this.setSize(100, 50);
051 //this.setBorderPainted(false);
052
053 this.orientation_ = orientation;
054 this.validate();
055 }
056
057 /**
058 * Set a new cardname and reinitialize the card image
059 */
060 public void setCardName(String name) {
061 if (name != null) {
062 icon_ = this.createImageIcon(TestingFlags.RESOURCE_DIR + name
063 + TestingFlags.IMAGE_EXTENSION);
064 this.setVisible(true);
065 } else {
066
067 icon_ = this.createImageIcon(TestingFlags.RESOURCE_DIR + TestingFlags.EMPTY_IMAGE);
068 this.setVisible(false);
069 }
070
071 this.validate();
072 }
073
074 /**
075 * Paints the component in VERTICAL or HORIZONTAL orientation
076 */
077 @Override
078 protected void paintComponent(Graphics g) {
079 super.paintComponent(g);
080
081 Graphics2D componentG = (Graphics2D) g;
082
083 int iconWidth = this.icon_.getIconWidth();
084 int iconHeight = this.icon_.getIconHeight();
085
086 /* Turn the icon into a more convenient image format */
087 BufferedImage rawImage = new BufferedImage(iconWidth, iconHeight,
088 BufferedImage.TYPE_INT_RGB);
089
090 Graphics2D imageG2D = (Graphics2D) rawImage.getGraphics();
091 this.icon_.paintIcon(this, imageG2D, 0, 0);
092
093 if (orientation_ == VERTICAL) {
094 double xscale = (double) this.getWidth() / (double) rawImage.getHeight(this);
095 double yscale = (double) this.getHeight() / (double) rawImage.getWidth(this);
096
097 /* This image holds the rotated and translated image in original size */
098 BufferedImage rotatedImage = new BufferedImage(iconHeight, iconWidth,
099 BufferedImage.TYPE_INT_RGB);
100 Graphics2D rotatedG = (Graphics2D) rotatedImage.getGraphics();
101
102 /* Rotate the image by 90 degrees and translate */
103 AffineTransform trans = AffineTransform.getRotateInstance(Math.toRadians(90));
104 trans.concatenate(AffineTransform.getTranslateInstance(0, -iconHeight));
105
106 rotatedG.drawImage(rawImage, trans, this);
107
108 /* Scale to fit on the component */
109 AffineTransform trans2 = AffineTransform.getScaleInstance(xscale, yscale);
110
111 /* Transform and then draw the final image onto the component */
112 componentG.drawImage(rotatedImage, trans2, this);
113
114 } else {
115 double xscale = (double) this.getWidth() / (double) rawImage.getWidth(this);
116 double yscale = (double) this.getHeight() / (double) rawImage.getHeight(this);
117
118 /* Just scale the image */
119 AffineTransform trans = AffineTransform.getScaleInstance(xscale, yscale);
120 componentG.drawImage(rawImage, trans, this);
121 }
122 this.validate();
123 }
124
125 /**
126 * Load and Create the ImageIcon from the card image file specified
127 * by path (Format: CardColor CardValue.jpg)
128 */
129 private ImageIcon createImageIcon(String path) {
130 URL imgURL = this.getClass().getResource(path);
131 if (imgURL != null) {
132 return new ImageIcon(imgURL);
133 } else {
134 System.err.println("Image nicht gefunden: " + path);
135 return null;
136 }
137 }
138
139 }