package gargoyle.carteblanche.ui;
import gargoyle.carteblanche.model.Card;
import javax.swing.JComponent;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
public class JCard extends JComponent {
private static final long serialVersionUID = 2554769380922400405L;
private final transient Image back;
private final Card card;
private boolean flipped;
public JCard(Card card, Image back, boolean flipped) {
this.back = back;
this.card = card;
this.flipped = flipped;
}
public JCard(Card card, Image back) {
this(card, back, false);
}
public JCard(Card card) {
this(card, null);
}
private void draw(Graphics2D g2, Rectangle bounds) {
int arc = Math.min(bounds.width, bounds.height) / 10;
int gap = Math.min(bounds.width, bounds.height) / 5;
g2.setColor(Color.WHITE.darker());
g2.fillRoundRect(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1, arc, arc);
g2.setColor(Color.BLACK);
g2.drawRoundRect(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1, arc, arc);
int width = bounds.width / 4;
int height = bounds.height / 4;
char suitSymbol = card.getSuitSymbol();
Color suitColor = card.getColor();
char valueSymbol = card.getValueSymbol();
drawSymbol(g2, new Rectangle(bounds.x + gap, bounds.y + gap, width, height), valueSymbol, suitColor);
drawSymbol(g2, new Rectangle(bounds.x - gap + bounds.width - width, bounds.y + gap, width, height), suitSymbol,
suitColor);
drawSymbol(g2, new Rectangle(bounds.x + gap, bounds.height - gap - height, width, height), suitSymbol,
suitColor);
drawSymbol(g2,
new Rectangle(bounds.x - gap + bounds.width - width, bounds.height - gap - height, width, height),
valueSymbol, suitColor);
}
private void drawBack(Graphics2D g2, Rectangle bounds) {
int arc = Math.min(bounds.width, bounds.height) / 10;
int width = bounds.width - 1;
int height = bounds.height - 1;
if (back != null) {
g2.setColor(Color.WHITE);
g2.fillRoundRect(bounds.x, bounds.y, width, height, arc, arc);
g2.setComposite(AlphaComposite.Src);
g2.fillRoundRect(bounds.x, bounds.y, width, height, arc, arc);
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(back, bounds.x, bounds.y, width, height, 0, 0, back.getWidth(null), back.getHeight(null),
null);
} else {
g2.setColor(Color.WHITE.darker());
g2.fillRoundRect(bounds.x, bounds.y, width, height, arc, arc);
}
g2.setColor(Color.BLACK);
g2.drawRoundRect(bounds.x, bounds.y, width, height, arc, arc);
}
private void drawSymbol(Graphics2D g2, Rectangle bounds, char symbol, Color color) {
g2.setColor(color);
Shape
outline =
new TextLayout(String.valueOf(symbol), g2.getFont(), g2.getFontRenderContext()).getOutline(null);
Rectangle outlineBounds = outline.getBounds();
Shape
scaled =
AffineTransform.getScaleInstance(bounds.getWidth() / outlineBounds.getWidth(),
bounds.getHeight() / outlineBounds.getHeight()).createTransformedShape(outline);
Rectangle scaledBounds = scaled.getBounds();
g2.fill(AffineTransform.getTranslateInstance(bounds.getX() - scaledBounds.getX() / 2,
bounds.getY() - scaledBounds.getY()).createTransformedShape(scaled));
}
public Image getBack() {
return back;
}
public Card getCard() {
return card;
}
public boolean isFlipped() {
return flipped;
}
public void setFlipped(boolean flipped) {
this.flipped = flipped;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color color = g.getColor();
Rectangle bounds = new Rectangle(getSize());
Graphics2D g2 = (Graphics2D) g;
if (flipped) {
drawBack(g2, bounds);
} else {
draw(g2, bounds);
}
g.setColor(color);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
cb.zip_java card_zip
共11个文件
java:8个
jpg:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 37 浏览量
2022-09-24
21:47:04
上传
评论
收藏 10.73MB ZIP 举报
温馨提示
java card game swing
资源推荐
资源详情
资源评论
收起资源包目录
cb.zip (11个子文件)
src
test
resources
pc.jpg 299KB
tex.jpg 5.24MB
java
gargoyle
carteblanche
ui
JCardTest.java 1KB
main
resources
tex.jpg 5.24MB
java
gargoyle
carteblanche
model
Suit.java 671B
Card.java 2KB
Value.java 626B
ui
JCard.java 4KB
util
StringUtil.java 351B
RandomUtil.java 881B
CarteBlanche.java 127B
共 11 条
- 1
资源评论
alvarocfc
- 粉丝: 112
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功