绘制图标java swing?
我正在尝试创建一个绘制扑克牌的程序。每次创建类对象时都会绘制该图表。现在,我有五个类对象,但只画了一张卡片。我当然想知道为什么 我还想知道如何改进我的牌的移动 Cards.java绘制图标java swing?,java,Java,我正在尝试创建一个绘制扑克牌的程序。每次创建类对象时都会绘制该图表。现在,我有五个类对象,但只画了一张卡片。我当然想知道为什么 我还想知道如何改进我的牌的移动 Cards.java import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListen
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
}
}
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);
}
}
Table.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
}
}
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
您的解决方案显示进度=)谢谢。但是,感觉对象比卡片大。如果我拿了一张卡片,然后把它放在另一张卡片的上面,那么在照片实际被拍摄之前很久,这张卡片就被覆盖在下面了。卡片上的外观周围有一个巨大的框架:S@user2734182尝试更改布局,可能使用不同的布局管理器,例如GridBagLayout