Ysaaltar | Je développe (pour l'école) une interface graphique pour un jeu (à savoir, le jeu "Cartagena" ). Je dois afficher les cartes d'un joueur (sa "main" ). Mais quoi que je fasse, les cartes ne veulent pas s'afficher. Tout le reste fonctionne, sauf ces cartes.
Comme vous pourrez le constater en lisant mon code, j'ai déjà testé les pack(), repaint() et autres activate(), sans succès.
Quelqu'un pourrait-il me dire ce qui ne va pas, s'il vous plaît ?
Voici le code de mon composant qui affiche une carte :
Citation :
package g31075.cartagene.vue.gui;
import g31075.cartagene.modele.Symbole;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
public class CarteVue extends javax.swing.JPanel {
/** Creates new form CarteVue */
public CarteVue() {
this(Symbole.BOUTEILLE);
}
public CarteVue(Symbole symb) {
initComponents();
this.bordureNonSelectionne = this.getBorder();
this.bordureSelectionne = new EtchedBorder();
this.symbole = symb;
this.image = ConversionVersGUI.getGrandeImageDuSymbole(symb);
this.selectionnable = false;
this.selectionne = false;
this.setPreferredSize(this.dimPreferee);
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
estCliquee();
}
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet." );
}
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet." );
}
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet." );
}
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet." );
}
});
}
/** This method is called from within the constructor 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() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify // End of variables declaration private Border bordureNonSelectionne;
private Border bordureSelectionne;
private boolean selectionne;
private boolean selectionnable;
private Image image;
private Symbole symbole;
private Dimension dimPreferee = new Dimension(80, 80);;
public static final String CARTE_CHANGE_SELECTIONNABLE = "carte_selectionnable";
public static final String CARTE_CHANGE_SELECTIONNE = "carte_selectionne";
private void estCliquee() {
if (this.selectionnable) {
setSelectionne(!this.selectionne);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image, 1, 1, this);
}
public boolean isSelectionnable() {
return selectionnable;
}
public void setSelectionnable(boolean selectionnable) {
boolean old = this.selectionnable;
this.selectionnable = selectionnable;
if (old != this.selectionnable) {
firePropertyChange(CARTE_CHANGE_SELECTIONNABLE, old, this.selectionnable);
}
repaint();
}
public boolean isSelectionne() {
return selectionne;
}
public void setSelectionne(boolean selectionne) {
if (this.selectionnable) {
boolean old = this.selectionne;
this.selectionne = selectionne;
if (this.selectionne) {
setBorder(bordureSelectionne);
} else {
setBorder(bordureNonSelectionne);
}
if (old != this.selectionne) {
firePropertyChange(CARTE_CHANGE_SELECTIONNE, old, this.selectionne);
}
}
repaint();
}
public Symbole getSymbole() {
return symbole;
}
public void setSymbole(Symbole symbole) {
this.symbole = symbole;
this.image = ConversionVersGUI.getGrandeImageDuSymbole(symbole);
repaint();
}
}
|
Voici le code qui affiche la main :
Citation :
package g31075.cartagene.vue.gui;
import g31075.cartagene.modele.Carte;
import g31075.cartagene.modele.Couleur;
import g31075.cartagene.modele.Main;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
* @author Administrateur
*/
public class MainVue extends javax.swing.JPanel {
/** Creates new form MainVue */
public MainVue() {
this(new Main());
}
public MainVue(Main main) {
initComponents();
this.setMain(main);
this.selectionne = null;
}
/** This method is called from within the constructor 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() {
setBorder(javax.swing.BorderFactory.createTitledBorder("Cartes en main" ));
setPreferredSize(new java.awt.Dimension(600, 100));
}// </editor-fold>
// Variables declaration - do not modify // End of variables declaration private Main main;
private List<CarteVue> cartes = new ArrayList<CarteVue>(6);
private CarteVue selectionne;
public List<CarteVue> getCartes() {
return cartes;
}
public void setCartes(List<CarteVue> cartes) {
this.cartes = cartes;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
System.out.println("" + main);
this.main = main;
clear();
Iterator<Carte> it = this.main.iterator();
while (it.hasNext()) {
addCarteVue(it.next());
}
repaint();
}
public CarteVue getSelectionne() {
return selectionne;
}
public void setSelectionne(CarteVue selectionne) {
this.selectionne.setSelectionne(false);
this.selectionne = selectionne;
}
private void carteCliquee(PropertyChangeEvent evt) {
if ((Boolean)evt.getNewValue()) {
if (this.selectionne != null) {
this.selectionne = (CarteVue)evt.getSource();
}
for (CarteVue carteVue : this.cartes) {
if (carteVue.isSelectionne()) {
this.selectionne = carteVue;
break;
}
}
} else {
this.selectionne = null;
}
}
private void addCarteVue(Carte carte) {
//if (this.main.size() <= 6) {
System.out.println("Carte ajoutée : " + carte.getSymbole());
CarteVue carteVue = new CarteVue(carte.getSymbole());
carteVue.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(CarteVue.CARTE_CHANGE_SELECTIONNE)) {
carteCliquee(evt);
}
}
});
this.add(carteVue);
repaint();
//}
}
private void clear() {
this.removeAll();
repaint();
}
public void setColor(Couleur couleur) {
setBackground(ConversionVersGUI.convertirCouleur(couleur));
repaint();
}
/*@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (CarteVue carteVue : this.cartes) {
carteVue.repaint();
}
}*/
}
|
Et voici le code qui me permet de tester l'affichage de la main :
Citation :
package g31075.cartagene.vue.gui;
import g31075.cartagene.modele.Carte;
import g31075.cartagene.modele.Main;
import g31075.cartagene.modele.Symbole;
/**
*
* @author Administrateur
*/
public class TestMainVue extends javax.swing.JFrame {
/** Creates new form TestMainVue */
public TestMainVue() {
initComponents();
}
/** This method is called from within the constructor 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() {
mainVue1 = new g31075.cartagene.vue.gui.MainVue();
jComboBox1 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout mainVue1Layout = new javax.swing.GroupLayout(mainVue1);
mainVue1.setLayout(mainVue1Layout);
mainVue1Layout.setHorizontalGroup(
mainVue1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 584, Short.MAX_VALUE)
);
mainVue1Layout.setVerticalGroup(
mainVue1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 70, Short.MAX_VALUE)
);
for (Symbole symb : Symbole.values()) {
jComboBox1.addItem(symb);
}
jButton1.setText("Ajouter carte" );
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Retirer carte sélectionnée" );
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
.addComponent(mainVue1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(mainVue1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addContainerGap(131, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ajouterCarte((Symbole)this.jComboBox1.getSelectedItem());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
retirerCarte((CarteVue)this.mainVue1.getSelectionne());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestMainVue().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private g31075.cartagene.vue.gui.MainVue mainVue1;
// End of variables declaration
private void ajouterCarte(Symbole symbole) {
Main main = this.mainVue1.getMain();
main.ajouter(new Carte(symbole));
this.mainVue1.setMain(main);
repaint();
pack();
//this.mainVue1.setVisible(true);
//this.validate();
}
private void retirerCarte(CarteVue carteVue) {
Main main = this.mainVue1.getMain();
main.retirer(new Carte(carteVue.getSymbole()));
this.mainVue1.setMain(main);
repaint();
pack();
//this.mainVue1.setVisible(true);
}
}
|
Comme je développe à l'aide de NetBeans, une partie du code est autogénéré. Je l'ai quand même inclus, au cas où il serait nécessaire pour comprendre mon problème.
Merci d'avance à qui m'aider. |