IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Synth L&F vs Me - Round 1 - Fight


Sujet :

AWT/Swing Java

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre émérite
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Par défaut Synth L&F vs Me - Round 1 - Fight
    il y a quelque jour, gfx parlait de Synth la http://www.developpez.net/forums/sho...d.php?t=168108

    j'avait deja essayer une fois Synth ca prenait trop de temps pour avoir exactement qu'est ce que je voulait alors j'ai laisser tomber.

    aujourd'hui j'avais un peu de temp et j'ai decider de retester synth. j'ai decider de commencer par des choses simple et pas de TextField.

    je vais essayer de faire les chose par etape :-)))

    la premiere etape se resume a ca: un label deux bouton et un separator

    pourquoi ca? juste pour assez pour faire cette interface (copie d'IntelliJ)

    http://www.developpez.net/forums/att...1&d=1151079579

    j'ai voulu eviter les painter mais j'ai ete obliger d'utiliser un.
    J'ai beaucoup beaucoup de question avant de les poser, je vais poster le code du truc complet si quelqu'un veut tester

    MySynthTestRound1.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
     
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.HeadlessException;
    import java.awt.Insets;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.text.ParseException;
    import java.lang.reflect.InvocationTargetException;
     
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JSeparator;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.plaf.synth.SynthLookAndFeel;
     
    /**
     * Synth L&F : Test 1 - Defining basic styles for label, button and separators.
     * @author me :-)
     */
    public class MySynthTestRound1 extends JFrame implements ActionListener {
     
     
        public MySynthTestRound1() throws HeadlessException {
            super("Test Synth n°1 (Setting IDEA frame)");
            initGui();
        }
     
        /**
         * Helper method for creating buttons.
         * @param buttonText the text to display on the button
         * @param iconImage the Icon image to display on the button
         * @return a button
         */
        private JButton createButton(String buttonText, String iconImage) {
            BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(getClass().getClassLoader().getResource(iconImage));
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
     
            JButton button = new JButton(buttonText);
            if (bufferedImage != null) {
                button.setIcon(new ImageIcon(bufferedImage));
            }
     
            button.setVerticalTextPosition(SwingConstants.BOTTOM);
            button.setHorizontalTextPosition(SwingConstants.CENTER);
     
            button.setPreferredSize(new Dimension(130, 80));
            button.setMinimumSize(new Dimension(130, 80));
     
            button.setName("configurableButtonStyled");
     
            return button;
        }
     
        /**
         *
         * @param buttonText
         * @return a button
         */
        private JButton createSimpleButton(String buttonText) {
            JButton button = new JButton(buttonText);
            button.setPreferredSize(new Dimension(125, 30));
            button.setMinimumSize(new Dimension(125, 30));
            button.setName("actionButtonStyled");
            return button;
        }
     
        /**
         * Nothing interesting in this method. Used only to create and layout
         * components on the panel.
         */
        private void initGui() {
            this.setLayout(new GridBagLayout());
            this.setSize(new Dimension(620, 540));
     
            JLabel  projectSettingsLabel = new JLabel("Project Settings [SynthTestRound1]");
            JLabel ideSettingLabel = new JLabel("IDE Settings");
     
            JButton modulesButton = createButton("Modules", "pictures/modules.png");
            JButton compilerButton = createButton("Compiler", "pictures/compiler.png");
            JButton projectCodeStyleButton = createButton("<html><p align=\"center\">Project<br>code sytles</p></html>", "pictures/codeStyle.png");
            JButton versionControlButton = createButton("Version Control", "pictures/versionControl.png");
            JButton guiDesignerButton = createButton("GUI Designer", "pictures/uiDesigner.png");
     
            JSeparator projectSettingSeparator = new JSeparator();
     
            JSeparator ideSettingSeparator = new JSeparator();
     
            JButton generalButton = createButton("General", "pictures/general.png");
            JButton appearanceButton = createButton("Appearance", "pictures/appearance.png");
            JButton editorButton = createButton("Editor", "pictures/editor.png");
            JButton codeCompletionButton = createButton("Code Completion", "pictures/codeCompletion.png");
            JButton fileTypesButton = createButton("File Types", "pictures/fileTypes.png");
            JButton localHistoryButton = createButton("Local History", "pictures/localHistory.png");
     
            JButton liveTemplatesButton = createButton("Live Templates", "pictures/liveTemplates.png");
            JButton fileTemplatesButton = createButton("File Templates", "pictures/fileTemplates.png");
     
            JButton classicViewButton = createSimpleButton("Classic View");
     
            JButton closeButton = createSimpleButton("Close");
            closeButton.addActionListener(this);
     
            /* lay-out components on the frame.*/
            this.add(projectSettingsLabel,
                    new GridBagConstraints(0, 0, GridBagConstraints.REMAINDER,
                            1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(25, 20, 0, 0), 0, 0));
            this.add(projectSettingSeparator,
                    new GridBagConstraints(0, 1, GridBagConstraints.REMAINDER,
                            1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                            new Insets(10, 20, 0, 20), 0, 0));
            this.add(modulesButton,
                    new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 20, 0, 0), 0, 0));
            this.add(compilerButton,
                    new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 0, 0, 0), 0, 0));
            this.add(projectCodeStyleButton,
                    new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 0, 0, 0), 0, 0));
            this.add(versionControlButton,
                    new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 0, 0, 0), 0, 0));
            this.add(guiDesignerButton,
                    new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 0, 0, 20), 0, 0));
            this.add(ideSettingLabel,
                    new GridBagConstraints(0, 3, GridBagConstraints.REMAINDER,
                            1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(30, 20, 0, 0), 0, 0));
            this.add(ideSettingSeparator,
                    new GridBagConstraints(0, 4, GridBagConstraints.REMAINDER,
                            1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                            new Insets(10, 20, 0, 20), 0, 0));
            this.add(generalButton,
                    new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 20, 0, 0), 0, 0));
            this.add(appearanceButton,
                    new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 0, 0, 0), 0, 0));
            this.add(editorButton,
                    new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 0, 0, 0), 0, 0));
            this.add(codeCompletionButton,
                    new GridBagConstraints(3, 5, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(10, 0, 0, 0), 0, 0));
            this.add(fileTypesButton,
                    new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(0, 20, 0, 0), 0, 0));
            this.add(localHistoryButton,
                    new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            this.add(liveTemplatesButton,
                    new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            this.add(fileTemplatesButton,
                    new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            this.add(classicViewButton,
                    new GridBagConstraints(0, 7, 1, 1, 0.0, 1.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE,
                            new Insets(50, 20, 10, 0), 0, 0));
            this.add(closeButton,
                    new GridBagConstraints(4, 7, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE,
                            new Insets(50, 0, 10, 20), 0, 0));
        }
     
        /**
         * Invoked when an action occurs.
         */
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
     
        public static void main(String[] args) {
     
            Runnable runnable = new Runnable() {
                public void run() {
                    SynthLookAndFeel synth = new SynthLookAndFeel();
                    try {
                        synth.load(MySynthTestRound1.class.getResource("synthround1.xml"));
                        UIManager.setLookAndFeel(synth);
                    } catch (ParseException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    } catch (IOException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    } catch (UnsupportedLookAndFeelException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            };
     
            try {
                EventQueue.invokeAndWait(runnable);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (InvocationTargetException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
     
            runnable = new Runnable() {
                public void run() {
                    MySynthTestRound1 synthTest1 = new MySynthTestRound1();
                    synthTest1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    synthTest1.pack();
                    synthTest1.setLocationRelativeTo(null);
                    synthTest1.setVisible(true);
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    Round1Painter.java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
    import javax.swing.UIManager;
    import javax.swing.plaf.synth.SynthContext;
    import javax.swing.plaf.synth.SynthPainter;
     
    /**
     * Created by IntelliJ IDEA.
     * User: bebe
     * Date: 23-Jun-2006
     * Time: 17:47:28
     * To change this template use File | Settings | File Templates.
     */
    public class Round1Painter extends SynthPainter {
     
     
        @Override
        public void paintPanelBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
            /* How to do that directly in the xml? I guess it's possible but how? */
            Graphics2D g2d = (Graphics2D)g;
            g2d.setColor((Color)UIManager.get("Panel.BackgroundColor"));
            g2d.fillRect(x, y, w, h);
        }
     
        @Override
        public void paintSeparatorBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
            Graphics2D g2d = (Graphics2D)g;
            g2d.setColor(new Color(175, 173, 160));
            g2d.fillRect(x, y, w, h);
        }
     
        @Override
        public void paintButtonBorder(SynthContext context, Graphics g, int x, int y, int w, int h) {
            Graphics2D g2d = (Graphics2D)g;
            g2d.setColor((Color)UIManager.get("Button.BorderColor"));
            g2d.drawRect(x, y, w-1, h-1);
        }
     
        @Override
        public void paintButtonBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
            Graphics2D g2d = (Graphics2D)g;
            g2d.setColor(new Color(181, 190, 214));
            g2d.fillRect(x, y, w-1, h-1);
            super.paintButtonBackground(context, g, x, y, w, h);
        }
    }

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
     
    <synth>
     
        <!-- some properties I'd like to reuse -->
        <object class="javax.swing.plaf.ColorUIResource" id="backgroundPanelColor">
            <int>236</int>
            <int>233</int>
            <int>216</int>
        </object>
     
        <object class="javax.swing.plaf.ColorUIResource" id="buttonBorderColor">
            <int>8</int>
            <int>36</int>
            <int>107</int>
        </object>
     
        <!--
        <object class="javax.swing.plaf.BorderUIResource$LineBorderUIResource" id="blackLineBorder">
                <object class="java.awt.Color">
                    <int>0</int>
                    <int>0</int>
                    <int>0</int>
                </object>
        </object>
     
     
        <object class="javax.swing.plaf.FontUIResource" id="defaultLabelFont">
            <object class="java.awt.Font">
                <string>Verdana</string>
                <int>Font.PLAIN</int>
                <int>20</int>
            </object>
        </object>
     
     
        <object class="javax.swing.plaf.BorderUIResource$EtchedBorderUIResource" id="defaultButtonBorder">
               <int>0</int> // EtchedBorder.LOWERED Lowered
        </object>
     
        -->
     
     
     
        <!--  -->
        <defaultsProperty key="Panel.BackgroundColor" value="backgroundPanelColor"/>
        <defaultsProperty key="Button.BorderColor" value="buttonBorderColor"/>
     
        <!-- some styles -->
     
        <!-- .......................................................................... -->
        <style id="panelStyle">
            <object id="backgroundPanel" class="Round1Painter"/>
            <painter method="panelBackground" idref="backgroundPanel"/>
        </style>
        <bind style="panelStyle" type="region" key="Panel"/>
     
     
        <style id="separatorStyle">
            <property key="Separator.thickness" type="integer" value="1"/>
            <object id="backgroundSeparator" class="Round1Painter"/>
            <painter method="separatorBackground" idref="backgroundSeparator"/>
        </style>
        <bind style="separatorStyle" type="region" key="Separator"/>
     
     
     
        <!-- multiples styles for buttons -->
        <style id="configurableButtonStyle">
            <!-- some propeties set for all states of this button -->
            <font name="Verdana" size="12" style="PLAIN"/>
            <property key="Button.margin" type="insets" value="5 5 5 5"/>
     
            <!--
            <property key="Button.border" type="idref" value="BlackLineBorder"/>
     
                This doesn't work cuz it seems property Button.border doesn't exist either.
                 >> http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/plaf/synth/doc-files/componentProperties.html.
             -->
     
            <state value="MOUSE_OVER">
                <!--
                    as my BlackLineBorder doesnt work, I'm forced to use a SynthPainter do draw it. I'll see ImagePainter
                    later ;-).
                -->
                <object id="borderButton" class="Round1Painter"/>
                <painter method="buttonBorder" idref="borderButton"/>
     
                <object id="backgroundButton" class="Round1Painter"/>
                <painter method="buttonBackground" idref="backgroundButton"/>
            </state>
        </style>
        <bind style="configurableButtonStyle" type="name" key="configurableButtonStyled"/>
     
        <style id="defaultButton">
            <font name="Verdana" size="12" style="BOLD"/>
            <state>
                <object id="borderButton" class="Round1Painter"/>
                <painter method="buttonBorder" idref="borderButton"/>
            </state>
        </style>
        <bind style="defaultButton" type="name" key="actionButtonStyled"/>
     
        <style id="labelStyle">
            <font name="Verdana" size="20" style="PLAIN"/>
        </style>
        <bind style="labelStyle" type="region" key="Label"/>
     
    </synth>

    et maintenant mes question

    1) on peut pas definir une couleur de background sur un panel sans passer par un painter?
    si je fais dans un style pour un panel
    <color value="BLACK" type="BACKGROUND"/>
    ca fait rien. c'est pas normal quand meme?

    2) pour le truc que gfx a expliquer ici http://www.developpez.net/forums/sho...2&postcount=11 ca marche pas tel quel, mais j'ai trouver.
    au lieu de
    <bind style="textfield" type="region" name="SearchField"/>

    <bind style="aStyleName" type="name" key="component's name"/>

    c'est le setText("") qui m'a mit sur la piste :-))

    maintenant ce que je trouverait bien c'est peut definir un comportement sur tout les boutons sauf ceux qui ont un certain nom. c'est possible?

    3) si je defini un objet(ici c'est un border) comme ca
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    <object class="javax.swing.plaf.BorderUIResource$EtchedBorderUIResource" id="defaultButtonBorder">
          <int>0</int> // EtchedBorder.LOWERED Lowered
    </object>
    comment je peut l'utiliser pour un Button?
    j'ai vu qu'on peut l'utiliser sur des table par exemple mais comment je fais sur un bouton?


    4) quand on extend SytnhPainter, on ne peut definir qu'une seule methode par exemple pour dessiner les bords d'un bouton.
    public void paintButtonBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { ... }

    c'est normal,

    si je veux un bord différent par state(focused, mouse_over...) ou bien par type de composant j'aurai besoin de plusieurs painter. ca peut tres vite faire beaucoup de painter a gerer. y'a pas un moyen de faire ca autrement?

    5) quel est la difference entre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    <style id="anId">
       <state>
           <font name="......".../>
           ....
       </state>
    </style>
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    <style id="anId">
       <state ="DEFAULT">
           <font name="......".../>
           ....
       </state>
    </style>
    j'ai encore une liste de question en stock mais je comence petit :-))))
    toute les conseil et remarque sont bienvenue :-)

    merci a tous

    ps: comme je sais que gfx passe ici je me suis dis que c'etait une bonne ocasion de poser des question direct au principal developeur de Synth
    j'espere aussi qu'il repondra haha
    Images attachées Images attachées  

Discussions similaires

  1. OpenDialog(round 2)
    Par shogoune dans le forum Composants VCL
    Réponses: 4
    Dernier message: 10/06/2003, 14h10
  2. qu'est ce que l'instruction "round"?
    Par isa_21 dans le forum Langage SQL
    Réponses: 2
    Dernier message: 10/03/2003, 10h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo