对于我学期末的项目,我必须在java上做一个纸牌克朗代克游戏。
到目前为止,我已经创建了一个名为Card的类,具有所有属性。我也制作了一个jFrame。它有一张ArrayList
的所有牌,我已经按照随机顺序排列,然后将它们分成其他7个牌组。
问题是在绘制卡片时,只有一个卡片在另一个卡片下方可见一张卡片。对于休息甲板,我似乎无法实现它。而且,我不能把卡面朝下。
这是我的代码和图形界面的图像。
GUI代码
package solitario;
import java.util.ArrayList;
import java.util.Collections;
import java.awt.*;
/**
*
* @author Andres
*/
public class Applet extends javax.swing.JApplet {
//Creo el ArrayList del mazo principal
private ArrayList<Carta> mazoPrincipal;
private ArrayList<Carta> mazoUno;
private ArrayList<Carta> mazoDos;
private ArrayList<Carta> mazoTres;
private ArrayList<Carta> mazoCuatro;
private ArrayList<Carta> mazoCinco;
private ArrayList<Carta> mazoSeis;
private ArrayList<Carta> mazoSiete;
/**
* Initializes the applet Applet
*/
@Override
public void init() {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Applet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
mazoPrincipal = new ArrayList<Carta>();
//Agrego Tréboles
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 1, "/imagenes/treboles/1.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 2, "/imagenes/treboles/49.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 3, "/imagenes/treboles/45.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 4, "/imagenes/treboles/41.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 5, "/imagenes/treboles/37.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 6, "/imagenes/treboles/33.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 7, "/imagenes/treboles/29.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 8, "/imagenes/treboles/25.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 9, "/imagenes/treboles/21.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 10, "/imagenes/treboles/17.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 11, "/imagenes/treboles/13.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 12, "/imagenes/treboles/9.png"));
mazoPrincipal.add(new Carta("Negro", false, "Trebol", 13, "/imagenes/treboles/5.png"));
//Agrego Picas
mazoPrincipal.add(new Carta("Negro", false, "Picas", 1, "/imagenes/picas/2.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 2, "/imagenes/picas/50.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 3, "/imagenes/picas/46.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 4, "/imagenes/picas/42.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 5, "/imagenes/picas/38.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 6, "/imagenes/picas/34.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 7, "/imagenes/picas/30.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 8, "/imagenes/picas/26.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 9, "/imagenes/picas/22.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 10, "/imagenes/picas/18.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 11, "/imagenes/picas/14.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 12, "/imagenes/picas/10.png"));
mazoPrincipal.add(new Carta("Negro", false, "Picas", 13, "/imagenes/picas/6.png"));
//Agrego Corazones
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 1, "/imagenes/corazones/3.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 2, "/imagenes/corazones/51.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 3, "/imagenes/corazones/47.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 4, "/imagenes/corazones/43.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 5, "/imagenes/corazones/39.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 6, "/imagenes/corazones/35.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 7, "/imagenes/corazones/31.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 8, "/imagenes/corazones/27.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 9, "/imagenes/corazones/23.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 10, "/imagenes/corazones/19.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 11, "/imagenes/corazones/15.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 12, "/imagenes/corazones/11.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Corazones", 13, "/imagenes/corazones/7.png"));
//Agrego Diamantes
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 1, "/imagenes/diamantes/4.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 2, "/imagenes/diamantes/52.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 3, "/imagenes/diamantes/48.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 4, "/imagenes/diamantes/44.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 5, "/imagenes/diamantes/40.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 6, "/imagenes/diamantes/36.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 7, "/imagenes/diamantes/32.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 8, "/imagenes/diamantes/28.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 9, "/imagenes/diamantes/24.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 10, "/imagenes/diamantes/20.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 11, "/imagenes/diamantes/16.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 12, "/imagenes/diamantes/12.png"));
mazoPrincipal.add(new Carta("Rojo", false, "Diamantes", 13, "/imagenes/diamantes/8.png"));
//Inicializa mazos
mazoUno = new ArrayList<Carta>();
mazoDos = new ArrayList<Carta>();
mazoTres = new ArrayList<Carta>();
mazoCuatro = new ArrayList<Carta>();
mazoCinco = new ArrayList<Carta>();
mazoSeis = new ArrayList<Carta>();
mazoSiete = new ArrayList<Carta>();
//Desordena mazo
Collections.shuffle(mazoPrincipal);
repartir();
// pinta();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* This method is called from within the init() method to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
labelMazo = new javax.swing.JLabel();
panelMazoDos = new javax.swing.JPanel();
panelMazoUno = new javax.swing.JPanel();
panelMazoTres = new javax.swing.JPanel();
panelMazoCuatro = new javax.swing.JPanel();
panelMazoCinco = new javax.swing.JPanel();
panelMazoSeis = new javax.swing.JPanel();
panelMazoSiete = new javax.swing.JPanel();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.setBackground(new java.awt.Color(0, 153, 0));
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
labelMazo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/b1fv.png"))); // NOI18N
labelMazo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
labelMazoMouseClicked(evt);
}
});
jPanel1.add(labelMazo, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 71, 96));
panelMazoDos.setBackground(new java.awt.Color(0, 153, 0));
panelMazoDos.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
panelMazoDos.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(panelMazoDos, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 180, 70, 160));
panelMazoUno.setBackground(new java.awt.Color(0, 153, 0));
panelMazoUno.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
panelMazoUno.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(panelMazoUno, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, 70, 160));
panelMazoTres.setBackground(new java.awt.Color(0, 153, 0));
panelMazoTres.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
panelMazoTres.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(panelMazoTres, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 180, 70, 160));
panelMazoCuatro.setBackground(new java.awt.Color(0, 153, 0));
panelMazoCuatro.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
panelMazoCuatro.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(panelMazoCuatro, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 180, 70, 160));
panelMazoCinco.setBackground(new java.awt.Color(0, 153, 0));
panelMazoCinco.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
panelMazoCinco.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(panelMazoCinco, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 180, 70, 160));
panelMazoSeis.setBackground(new java.awt.Color(0, 153, 0));
panelMazoSeis.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
panelMazoSeis.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(panelMazoSeis, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 180, 70, 160));
panelMazoSiete.setBackground(new java.awt.Color(0, 153, 0));
panelMazoSiete.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
panelMazoSiete.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(panelMazoSiete, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 180, 70, 160));
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 680, 470));
}// </editor-fold>
private void labelMazoMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel labelMazo;
private javax.swing.JPanel panelMazoCinco;
private javax.swing.JPanel panelMazoCuatro;
private javax.swing.JPanel panelMazoDos;
private javax.swing.JPanel panelMazoSeis;
private javax.swing.JPanel panelMazoSiete;
private javax.swing.JPanel panelMazoTres;
private javax.swing.JPanel panelMazoUno;
// End of variables declaration
public void repartir() {
//Agrega cartas a los demás mazos
for (int i = 0; i < mazoPrincipal.size(); i++) {
if (i == 0) {
mazoUno.add(mazoPrincipal.get(i));
}
if (i >= 1 && i < 3) {
mazoDos.add(mazoPrincipal.get(i));
}
if (i >= 3 && i < 6) {
mazoTres.add(mazoPrincipal.get(i));
}
if (i >= 6 && i < 10) {
mazoCuatro.add(mazoPrincipal.get(i));
}
if (i >= 10 && i < 15) {
mazoCinco.add(mazoPrincipal.get(i));
}
if (i >= 15 && i < 21) {
mazoSeis.add(mazoPrincipal.get(i));
}
if (i >= 21 && i < 28) {
mazoSiete.add(mazoPrincipal.get(i));
}
}//Fin For
//Pinta la carta en el mazo correspondiente.
int i = -1;
for (Carta c : mazoUno) {
c.setBounds(0, i, 50, 100);
panelMazoUno.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1));
}
for (Carta c : mazoDos) {
c.setBounds(0, i, 50, 100);
panelMazoDos.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1));
i += 15;
}
for (Carta c : mazoTres) {
c.setBounds(0, i, 50, 100);
panelMazoTres.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, 0, -1, -1));
i += 15;
}
for (Carta c : mazoCuatro) {
c.setBounds(0, i, 50, 100);
panelMazoCuatro.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1));
i += 15;
}
for (Carta c : mazoCinco) {
c.setBounds(0, i, 50, 100);
panelMazoCinco.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1));
i += 15;
}
for (Carta c : mazoSeis) {
c.setBounds(0, i, 50, 100);
panelMazoSeis.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, i, -1));
i += 15;
i++;
}
for (Carta c : mazoSiete) {
c.setBounds(0, i, 50, 100);
panelMazoSiete.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1));
i += 15;
}
}
}//Fin método repartir
卡类
package solitario;
import javax.swing.JLabel;
/**
*
* @author $Andrés Avila Wille
*/
public class Carta extends JLabel{
private String color;
private boolean con;
private String escudo;
private int num;
public Carta(){
}
public Carta(String color, boolean con, String escudo, int num, String URL) {
this.color = color;
this.con = con;
this.escudo = escudo;
this.num = num;
this.setIcon(new javax.swing.ImageIcon(getClass().getResource(URL)));
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isCon() {
return con;
}
public void setCon(boolean con) {
this.con = con;
}
public String getEscudo() {
return escudo;
}
public void setEscudo(String escudo) {
this.escudo = escudo;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public void vuelta(boolean con){
if(con == true){
this.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/b1fv.png")));
}
}
}
GUI图片
答案 0 :(得分:1)
首选解决方案是使用JLayeredPane
,因为它可以更好地控制组件的z排序,如果失败,您可以使用Container#add(Component, Object, int)
所以,基本上,无论何时向UI添加卡,都需要执行类似......
的操作panelMazoUno.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1), 0);
没有z顺序校正......
使用z顺序校正...
只需使用...即可完成。
for (Carta c : mazoUno) {
c.setBounds(0, i, 50, 100);
panelMazoUno.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1), 0);
}
for (Carta c : mazoDos) {
c.setBounds(0, i, 50, 100);
panelMazoDos.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1), 0);
i += 15;
}
for (Carta c : mazoTres) {
c.setBounds(0, i, 50, 100);
panelMazoTres.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, 0, -1, -1), 0);
i += 15;
}
for (Carta c : mazoCuatro) {
c.setBounds(0, i, 50, 100);
panelMazoCuatro.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1), 0);
i += 15;
}
for (Carta c : mazoCinco) {
c.setBounds(0, i, 50, 100);
panelMazoCinco.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1), 0);
i += 15;
}
for (Carta c : mazoSeis) {
c.setBounds(0, i, 50, 100);
panelMazoSeis.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, i, -1), 0);
i += 15;
i++;
}
for (Carta c : mazoSiete) {
c.setBounds(0, i, 50, 100);
panelMazoSiete.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1), 0);
i += 15;
}
答案 1 :(得分:0)
经过几个小时的测试,我已经找到了解决这个问题的方法。出错的是每个For Each迭代都有相同的i
索引。所以,我创建了一个for,为每个套牌初始化了一个不同的i
索引。另外,我使用i索引将其值设置为c.setBounds()
和AbsoluteConstraints()
中的Y位置。这是我的代码:
for (Carta c : mazoUno) {
int i = 0;
c.setBounds(0, i, 50, 100);
panelMazoUno.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, -1, -1, -1), 0);
}
for (int i = 0; i < mazoDos.size();) {
for (Carta c : mazoDos) {
c.setBounds(0, i, 50, 100);
panelMazoDos.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1), 0);
i += 15;
}
}
for (int i = 0; i < mazoTres.size();) {
for (Carta c : mazoTres) {
c.setBounds(0, i, 50, 100);
panelMazoTres.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1), 0);
i += 15;
}
}
for (int i = 0; i < mazoCuatro.size();) {
for (Carta c : mazoCuatro) {
c.setBounds(0, i, 50, 100);
panelMazoCuatro.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1), 0);
i += 15;
}
}
for (int i = 0; i < mazoCinco.size();) {
for (Carta c : mazoCinco) {
c.setBounds(0, i, 50, 100);
panelMazoCinco.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1), 0);
i += 15;
}
}
for (int i = 0; i < mazoSeis.size();) {
for (Carta c : mazoSeis) {
c.setBounds(0, i, 50, 100);
panelMazoSeis.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1), 0);
i += 15;
}
}
for (int i = 0; i < mazoSiete.size();) {
for (Carta c : mazoSiete) {
c.setBounds(0, i, 50, 100);
panelMazoSiete.add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(-1, i, -1, -1), 0);
i += 15;
}
}
它看起来像这样: