- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个绘制扑克牌的程序。每次创建类的对象时都会绘制图表。现在,我有五个该类的对象,但只绘制了一张卡片。我当然想知道为什么?
我还想知道如何改进我的纸牌移动。
Cards.java
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Cards extends JPanel implements MouseMotionListener, MouseListener{
Random rnd = new Random();
int X, posX;
int Y, posY;
int line;
int col;
String card;
boolean faceUp;
boolean pressOut;
static int numOfCards;
String [][] icons = {
{"img/c1.gif", "img/d1.gif", "img/h1.gif" , "img/s1.gif"},
{"img/c2.gif", "img/d2.gif", "img/h2.gif" , "img/s2.gif"},
{"img/c3.gif", "img/d3.gif", "img/h3.gif" , "img/s3.gif"},
{"img/c4.gif", "img/d4.gif", "img/h4.gif" , "img/s4.gif"},
{"img/c5.gif", "img/d5.gif", "img/h5.gif" , "img/s5.gif"},
{"img/c6.gif", "img/d6.gif", "img/h6.gif" , "img/s6.gif"},
{"img/c7.gif", "img/d7.gif", "img/h7.gif" , "img/s7.gif"},
{"img/c8.gif", "img/d8.gif", "img/h8.gif" , "img/s8.gif"},
{"img/c9.gif", "img/d9.gif", "img/h9.gif" , "img/s9.gif"},
{"img/c10.gif", "img/d10.gif", "img/h10.gif" , "img/s10.gif"},
{"img/cj.gif", "img/dj.gif", "img/hj.gif" , "img/sj.gif"},
{"img/cq.gif", "img/dq.gif", "img/hq.gif" , "img/sq.gif"},
{"img/ck.gif", "img/dk.gif", "img/hk.gif" , "img/sk.gif"}
};
public Cards() {
numOfCards++;
addMouseListener(this);
addMouseMotionListener(this);
this.X = rnd.nextInt(300);
this.Y = rnd.nextInt(300);
this.line = rnd.nextInt(13);
this.col = rnd.nextInt(4);
this.card = icons[line][col];
this.faceUp = rnd.nextBoolean();
this.pressOut = false;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(faceUp == true){
ImageIcon icon = new ImageIcon(this.getClass().getResource(card));
g.drawImage(icon.getImage(), this.X, this.Y, this);
} else {
ImageIcon icon = new ImageIcon(this.getClass().getResource("/img/b1fv.gif"));
g.drawImage(icon.getImage(), this.X, this.Y, this);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if(this.faceUp == false){
this.faceUp = true;
this.repaint();
} else {
this.faceUp = false;
this.repaint();
}
}
@Override
public void mousePressed(MouseEvent e) {
this.posX = this.getX() - e.getX();
this.posY = this.getY() - e.getY();
if (this.card != null) {
updateLocation(e);
}else{
pressOut = true;
}
}
@Override
public void mouseReleased(MouseEvent e) {
updateLocation(e);
}
@Override
public void mouseDragged(MouseEvent e) {
if (!pressOut) {
updateLocation(e);
}
}
public void updateLocation(MouseEvent e){
this.setLocation(this.posX + e.getX(), this.posY + e.getY());
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Table.java
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JFrame;
public class Table{
public static void main(String[] args) {
JFrame frame = new JFrame("Cards");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(null);
frame.addMouseMotionListener(null);
frame.setBackground(Color.GREEN);
frame.setSize(400,600);
Cards c1 = new Cards();
Cards c2 = new Cards();
Cards c3 = new Cards();
Cards c4 = new Cards();
Cards c5 = new Cards();
System.out.println(c1.card);
System.out.println(c2.card);
System.out.println(c3.card);
System.out.println(c4.card);
System.out.println(c5.card);
System.out.println(c5.numOfCards);
frame.add(c1);
frame.add(c2);
frame.add(c3);
frame.add(c4);
frame.add(c5);
frame.setVisible(true);
}
}
最佳答案
JFrame
的默认布局是 BorderLayout
。
例如,尝试使用GridLayout
frame.setLayout(new GridLayout(0, 3));
不要在paintComponent
方法中加载图像,该方法应该尽快退出,而是预加载图像...
public class Cards extends JPanel implements MouseMotionListener, MouseListener{
//...
private Image face;
private Image back;
//...
public Cards() {
//...
face = new ImageIcon(this.getClass().getResource(card)).getImage();
back = ImageIcon icon = new ImageIcon(this.getClass().getResource("/img/b1fv.gif")).getImage();
//...
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(faceUp){
g.drawImage(face, this.X, this.Y, this);
} else {
g.drawImage(back , this.X, this.Y, this);
}
}
您还应该重写 JPanel
的 getPreferredSize
方法,该方法与布局管理器 API 配合使用,使其能够决定如何最好地布局组件。 .
public Dimension getPreferredSize() {
return face == null ? super.getPreferredSize() : new Dimension(face.getWidth(this), face.getHeight(this));
}
现在允许您使用 JFrame#pack
而不是 JFrame#setSize
关于java - 绘制 iconimage java swing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22478249/
如何控制 SymbolLayer 中每个要素的 icon? 我在文档中看到了这个,但我不知道如何使用它; public static PropertyValue> iconImage(Functio
我正在和我的 friend 一起制作西洋跳棋游戏,我负责棋子、棋盘等。我通过另一个也在开发西洋跳棋游戏的 friend 得到了使用 iconImages 的想法,并且它可以工作。由于某种原因,检查器图
我正在尝试创建一个绘制扑克牌的程序。每次创建类的对象时都会绘制图表。现在,我有五个该类的对象,但只绘制了一张卡片。我当然想知道为什么? 我还想知道如何改进我的纸牌移动。 Cards.java impo
我想用图标替换单选按钮列表中的文本。 我已经试过了: rotateButton = new JRadioButton(rotateIcon.getImage()); 但这用图标替换了单选按钮和文本。我
如何为 MGLSymbolStyleLayer 添加自定义图像。下面是我的代码, let symbolGraphicsLayer = MGLSymbolStyleLayer(ident
我是一名优秀的程序员,十分优秀!