Forme Modifiée JButton

Nouyou Messages postés 51 Statut Membre -  
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   -
Salut j'ai une matrice de boutons (Centre[x][y]) que j'essaye de modifier la forme de ses boutons mais je n'arrive pas . Le programme m'affiche cette exception

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - anonymous class implements interface; cannot have arguments

voici le code

  private JButton createCell(int x, int y) {
        Icon icon=new Icon("RondG.png") {

            @Override
            public void paintIcon(Component c, Graphics g, int x, int y) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public int getIconWidth() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public int getIconHeight() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
        centre[x][y] = new RondButton(icon);
        centre[x][y].addActionListener(new MatriceListener(this,x,y));
        return centre[x][y];
    }

et voici la classe RondButton

public class RondButton extends JButton {
     public RondButton(Icon icon) {
    super(icon);
    setBorderPainted(false);
    setFocusPainted(false);
    setContentAreaFilled(false);
  }
 
/**
 * détermine si le point (x, y) est à l'intérieur de l'icône circulaire
 */
  public boolean contains(int x, int y) {
    Dimension size = getSize();
    float x0 = size.width / 2F;
    float y0 = size.height / 2F;
 
    Icon icon = getIcon();
    float w = icon.getIconWidth() / 2F;
    float h = icon.getIconHeight() / 2F;
 
    return (x - x0) * (x - x0) + (y - y0) * (y - y0) <= w * h;
  }
}

Merci de m'aider , c'est urgent :)

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Bonjour,

Je n'ai jamais eu cette erreur, mais vu sa description et le code je dirais que le problème est sur
Icon icon=new Icon("RondG.png") {


Tu construis un objet avec un constructeur qui prend en paramètre un String, or c'est fait dans une classe anonyme qui ne peut avoir que le constructeur par défaut (sans argument).

Pour t'en sortir, il faut déplacer le code pour ne plus avoir de classe anonyme, mais soit une vraie classe, soit une classe interne, selon tes autres contraintes.

class Icon2 implements Icon {

    Icon2(String fileName) {
    }

    @Override
    // ...
}

private JButton createCell(int x, int y) {
       Icon icon=new Icon2("RondG.png");
       //..
0
Nouyou Messages postés 51 Statut Membre
 
ça n'as pas marché :(
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
 at graphe.Icon2.getIconWidth(Icon2.java:30)
 at javax.swing.SwingUtilities.layoutCompoundLabelImpl(SwingUtilities.java:960)
 at javax.swing.SwingUtilities.layoutCompoundLabel(SwingUtilities.java:887)
 at javax.swing.plaf.basic.BasicGraphicsUtils.getPreferredButtonSize(BasicGraphicsUtils.java:282)
 at javax.swing.plaf.basic.BasicButtonUI.getPreferredSize(BasicButtonUI.java:376)
 at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
 at java.awt.GridLayout.preferredLayoutSize(GridLayout.java:341)
 at java.awt.Container.preferredSize(Container.java:1788)
 at java.awt.Container.getPreferredSize(Container.java:1773)
 at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
 at javax.swing.ViewportLayout.preferredLayoutSize(ViewportLayout.java:95)
 at java.awt.Container.preferredSize(Container.java:1788)
 at java.awt.Container.getPreferredSize(Container.java:1773)
 at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
 at javax.swing.ScrollPaneLayout.preferredLayoutSize(ScrollPaneLayout.java:492)
 at java.awt.Container.preferredSize(Container.java:1788)
 at java.awt.Container.getPreferredSize(Container.java:1773)
 at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
 at javax.swing.BoxLayout.checkRequests(BoxLayout.java:483)
 at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:424)
 at java.awt.Container.layout(Container.java:1503)
 at java.awt.Container.doLayout(Container.java:1492)
 at java.awt.Container.validateTree(Container.java:1688)
 at java.awt.Container.validateTree(Container.java:1697)
 at java.awt.Container.validateTree(Container.java:1697)
 at java.awt.Container.validateTree(Container.java:1697)
 at java.awt.Container.validate(Container.java:1623)
 at java.awt.Container.validateUnconditionally(Container.java:1660)
 at java.awt.Window.show(Window.java:1033)
 at java.awt.Component.show(Component.java:1655)
 at java.awt.Component.setVisible(Component.java:1607)
 at java.awt.Window.setVisible(Window.java:1014)
 at graphe.matrice2.<init>(matrice2.java:66)
 at graphe.NbreSommet.actionPerformed(NbreSommet.java:58)
 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
 at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
 at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
 at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
 at java.awt.Component.processMouseEvent(Component.java:6516)
 at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
 at java.awt.Component.processEvent(Component.java:6281)
 at java.awt.Container.processEvent(Container.java:2229)
 at java.awt.Component.dispatchEventImpl(Component.java:4872)
 at java.awt.Container.dispatchEventImpl(Container.java:2287)
 at java.awt.Component.dispatchEvent(Component.java:4698)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
 at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
 at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
 at java.awt.Container.dispatchEventImpl(Container.java:2273)
 at java.awt.Window.dispatchEventImpl(Window.java:2719)
 at java.awt.Component.dispatchEvent(Component.java:4698)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
 at java.awt.EventQueue.access$200(EventQueue.java:103)
 at java.awt.EventQueue$3.run(EventQueue.java:694)
 at java.awt.EventQueue$3.run(EventQueue.java:692)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
 at java.awt.EventQueue$4.run(EventQueue.java:708)
 at java.awt.EventQueue$4.run(EventQueue.java:706)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at javax.swing.SizeRequirements.calculateAlignedPositions(SizeRequirements.java:461)
 at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:434)
 at java.awt.Container.layout(Container.java:1503)
 at java.awt.Container.doLayout(Container.java:1492)
 at java.awt.Container.validateTree(Container.java:1688)
 at java.awt.Container.validateTree(Container.java:1697)
 at java.awt.Container.validateTree(Container.java:1697)
 at java.awt.Container.validateTree(Container.java:1697)
 at java.awt.Container.validate(Container.java:1623)
 at java.awt.Window.dispatchEventImpl(Window.java:2717)
 at java.awt.Component.dispatchEvent(Component.java:4698)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
 at java.awt.EventQueue.access$200(EventQueue.java:103)
 at java.awt.EventQueue$3.run(EventQueue.java:694)
 at java.awt.EventQueue$3.run(EventQueue.java:692)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
 at java.awt.EventQueue$4.run(EventQueue.java:708)
 at java.awt.EventQueue$4.run(EventQueue.java:706)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
"UnsupportedOperationException: Not supported yet." c'est normal vu que c'est exactement ce que tu donnes comme contenu aux méthodes de l'icone...

@Override
public int getIconWidth() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

Mais je ne vois pas trop l'intérêt de définir toi même ta propre classe Icon. Ce que tu essayes de faire existe déjà, avec un ImageIcon par exemple.

Icon icon=new ImageIcon("RondG.png");

Voir la documentation : javax.swing.ImageIcon, How to Use icons
0
Nouyou Messages postés 51 Statut Membre
 
C bon , mais est ce qu'il me faut definir une classe pour adapter la taille de l'icone à celle du JButton ?
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020 > Nouyou Messages postés 51 Statut Membre
 
Je ne sais pas trop, il faudrait essayer, mais ça risque de tronquer l'image, pas de la redimensionner.

Remarque : dans ce cas ce ne serait pas l'interface Icon qu'il faudrait implémenter, mais plutôt IconImage qu'il faudrait étendre.

class ImageIcon2 extends ImageIcon {

    private static final long serialVersionUID = 1;

    public ImageIcon2(String fileName) {
        super(fileName);
    }

    @Override
    public int getIconWidth() {
        return 10;
    }

    @Override
    public int getIconHeight() {
        return 10;
    }
}
0