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 :

paint java 5 et java 6


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2007
    Messages
    697
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 697
    Par défaut paint java 5 et java 6
    bonjour j'ai un classe qui extends JPanel dans la quelle je dessine selon des événements claviers
    (2 clique souris = droite,
    déplacement souris pressé = trait,
    alt+r = carre...
    )
    et j'ai dans ma fenêtre principale le panel et en dessous un JScrollPane
    qui contient une JTextArea dan laquelle j'affiche tous les événement capté par le JPanel.
    Voici la classe du panel:
    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
     
    package src;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
     
    import javax.swing.*;
     
    import sun.java2d.loops.DrawRect;
     
     
    class PremDessin extends JPanel
    implements MouseListener, MouseMotionListener,KeyListener{
        private int x,y;
        private JTextArea sortie;
        private ArrayList<Point> tabTrait = new ArrayList<Point>();
        private ArrayList<Point> tabCarre = new ArrayList<Point>();
        private ArrayList<Point> tabRondNoir = new ArrayList<Point>();
        private ArrayList<Point> tabRondVide= new ArrayList<Point>();
        private ArrayList<Point> tabTraitDrag = new ArrayList<Point>();
        private Fenetre fen;
        PremDessin(JTextArea listener,Fenetre fen){
            this.sortie = listener;
            this.fen = fen;
            setBackground(Color.orange);
            sortie.setText("\nInitialisation");
            addMouseListener(this);
            addMouseMotionListener(this);
            addKeyListener(this);
            this.x = 0;
            this.y = 0;        
        }
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            sortie.append("\nLancement de paint ");        
            g.drawString("",50,50);
            for(Point p : tabCarre){
                g.drawRect(p.x, p.y, 20, 20);
            }
            for(Point p : tabRondNoir){
                g.fillOval(p.x, p.y, 15, 15);
            }
            for(Point p : tabRondVide){
                g.drawOval(p.x, p.y, 15, 15);
            }
            if(tabTraitDrag.size()>1){
                for(int i = 0;i <tabTraitDrag.size()-1;i++){            
                    g.drawLine(tabTraitDrag.get(i).x, tabTraitDrag.get(i).y,
                            tabTraitDrag.get(i+1).x, tabTraitDrag.get(i+1).y);                            
                }
            }
            if(tabTrait.size()>1){
                for(int i = 0;i <tabTrait.size()-1;i++){
     
                    g.drawLine(tabTrait.get(i).x, tabTrait.get(i).y,
                            tabTrait.get(i+1).x, tabTrait.get(i+1).y);
                }                    
            }
            this.requestFocusInWindows();
            fen.updatViewPort();
        }
     
        public void mousePressed(MouseEvent e) {
            int xs = e.getX();
            int ys = e.getY();
     
            sortie.append("\nEnfoncement du bouton de la souris en "+xs+","+ys);
        }
     
        public void mouseReleased(MouseEvent e) {
            int xs = e.getX();
            int ys = e.getY();
     
            sortie.append("\nRelachement du bouton de la souris en "+xs+","+ys);
        }
     
        public void mouseClicked(MouseEvent e) {
            int xs = e.getX();
            int ys = e.getY();        
            tabTrait.add(new Point(xs,ys));
            repaint();
            sortie.append("\nClic souris en "+xs+","+ys);
        }
     
        public void mouseMoved(MouseEvent e) {
            int xs = e.getX();
            int ys = e.getY();
            this.x = xs;
            this.y = ys;        
            repaint();
            sortie.append("\nDeplacement en "+xs+","+ys);
        }
     
        public void mouseDragged(MouseEvent e) {
            int xs = e.getX();
            int ys = e.getY();
            tabTraitDrag.add(new Point(xs,ys));
            sortie.append("\nGlissement en "+xs+","+ys);   
            repaint();
        }
     
        public void mouseEntered(MouseEvent e) {
            int xs = e.getX();
            int ys = e.getY();
            sortie.append("\nEntree de la souris en "+xs+","+ys);
        }
     
        public void mouseExited(MouseEvent e) {
            int xs = e.getX();
            int ys = e.getY();
            sortie.append("\nSortie de la souris en "+xs+","+ys);
        }
     
        public void keyPressed(KeyEvent e) {
            char car  = e.getKeyChar(); 
            int i = e.getKeyCode() ;
            if (e.isControlDown() && (i == KeyEvent.VK_C) ) {
                sortie.append("\n < Ctrl + c > pressée ");
                getTabRondVide().add(new Point(getXS(),getYS()));
            }
     
            if (e.isControlDown() && e.isAltDown() && e.isShiftDown() 
                    && i == KeyEvent.VK_D) {
                sortie.append("\n< Ctrl + Alt + Shift + d > pressée ");
                getTabRondNoir().add(new Point(getXS(),getYS()));
            }
            if (e.isAltDown()  && car == 'r'){
                sortie.append("\n< Alt + r > pressée ");
               getTabCarre().add(new Point(getXS(),getYS()));
            }
            sortie.append("\nEnfoncement de la touche "+ car);
     
           repaint();
     
        }
        public void keyReleased(KeyEvent e) {
            char k = e.getKeyChar();
            sortie.append("\nTouche frappee "+ k);
        }
        public void keyTyped(KeyEvent e) {
            char k = e.getKeyChar();
            sortie.append("\nRelachement de la touche "+k);
     
        }
     
        public JTextArea getSortie() {
            return sortie;
        }
     
        public void setSortie(JTextArea sortie) {
            this.sortie = sortie;
        }
     
        public ArrayList<Point> getTabCarre() {
            return tabCarre;
        }
     
        public void setTabCarre(ArrayList<Point> tabCarre) {
            this.tabCarre = tabCarre;
        }
     
        public ArrayList<Point> getTabRondNoir() {
            return tabRondNoir;
        }
     
        public void setTabRondNoir(ArrayList<Point> tabRondNoir) {
            this.tabRondNoir = tabRondNoir;
        }
     
        public ArrayList<Point> getTabRondVide() {
            return tabRondVide;
        }
     
        public void setTabRondVide(ArrayList<Point> tabRondVide) {
            this.tabRondVide = tabRondVide;
        }
     
        public ArrayList<Point> getTabTrait() {
            return tabTrait;
        }
     
        public void setTabTrait(ArrayList<Point> tabTrait) {
            this.tabTrait = tabTrait;
        }
     
        public ArrayList<Point> getTabTraitDrag() {
            return tabTraitDrag;
        }
     
        public void setTabTraitDrag(ArrayList<Point> tabTraitDrag) {
            this.tabTraitDrag = tabTraitDrag;
        }
     
        public int getXS() {
            return x;
        }
     
        public void setXS(int x) {
            this.x = x;
        }
     
        public int getYS() {
            return y;
        }
     
        public void setYS(int y) {
            this.y = y;
        }
     
    }
    et ma JFrame:
    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
     
    package src;
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JViewport;
     
    public class Fenetre extends JFrame{
     
     
        private JTextArea listener = new JTextArea("hjjh");
        private JScrollPane scr = new JScrollPane(listener);
        private PremDessin tmp;
     
     
        public Fenetre(String title){
            super(title);
            tmp = new PremDessin(listener,this);
            listener.setFocusable(false);
            this.add(tmp,BorderLayout.CENTER);
            this.setSize(500, 600);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            scr.setPreferredSize(new Dimension(500,100));
            scr.getVerticalScrollBar().setValue(600);
            this.add(scr,BorderLayout.SOUTH);
     
            setVisible(true);
        }
        public static void main(String[] args) {
            new Fenetre("hello");
        }
     
        public void updatViewPort(){
               scr.getVerticalScrollBar().setValue(listener.getHeight());            //sert à toujours avoit le JSCrollPane en bas
        }
     
    }
    Donc ce code marche avec java 6 mais sous java 5 je dois sortir
    l'appel à updatViewPort de paintComponent et le mettre après les appel à repaint() sinon j'ai un gros bug d'affichage.Le contenu affiché du JScrollPane(le viewPortView) et ajouter au panel sous java 5

    Quelqu'un sait tous pourrais venir ce problème d'incompatibilté avec JAVA 5 de mon bout de code ?
    Images attachées Images attachées  

  2. #2
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2007
    Messages
    697
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 697
    Par défaut
    up !

  3. #3
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 900
    Billets dans le blog
    54
    Par défaut
    Pour ce que j'en vois et apres avoir commente this.requestFocusInWindows(); (methode non-trouvee), le probleme se resoud dans Java 5 (en lancant a partir JBuilder 2006) en deplacant fen.updatViewPort(); hors de la methode de dessin et en le copiant dans toutes les methodes de reception d'evenements.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  4. #4
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2007
    Messages
    697
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 697
    Par défaut
    Merci bouye d'avoir regarder mon problème,
    en effet une fois sortie du paint l'updateViewPort ne pose plus de problème sous java 5.0 mais je ne comprend toujours d'où cela vient.
    On est bien dans le même Thread non ? Est-ce que paint est lancé dans un nouveau Thread ce qui "pourrait" expliquer le problème ?

    PS:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    this.requestFocusInWindows();
    c'est le remplacant de
    (à partir de java 1.4 Il me semble ou 5)
    ca permet de donner le focus à mon JPanel pour intercepter les évenement clavier à la place de la JFrame. Il evite d'avoir des problème quand le composant ne peut pas avoir le focus je crois.

  5. #5
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 900
    Billets dans le blog
    54
    Par défaut
    Citation Envoyé par atha2 Voir le message
    On est bien dans le même Thread non ? Est-ce que paint est lancé dans un nouveau Thread ce qui "pourrait" expliquer le problème ?
    Oui mais l'EDT est composee de 2 partie : evenements et peinture. En phase de peinture, on evite de provoquer des evenements car on peut facilement boucler indefiniement (les evenement demandant la repeinte de l'interface ce qui peut ammener le code a boucler sur lui-meme) ou avoir des erreurs de ce genre. Dans ton cas, cela marche dans Java 6 car il y a eut des modifications sur le pipeline interne de rendu de Swing (nottanement pour le rendre reellement double-buffered), mais globalement ce n'etait pas la bonne methode a suivre.

    Donc pour resumer :
    - gestion du focus et des impressions de message => dans les gestionnaires d'evenements.
    - methode de peinture => ne fait que de la peinture et ne genere aucun event.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  6. #6
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2007
    Messages
    697
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Calvados (Basse Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Janvier 2007
    Messages : 697
    Par défaut
    merci pour ta réponse,
    c'est exactement ce que j'attendais.
    donc je marque résolu

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [JAVA] Quel EDI JAVA choisir pour Mac OS X ?
    Par didi dans le forum Développement OS X
    Réponses: 18
    Dernier message: 29/09/2007, 22h07
  2. Eclipse erreur : java.lang.OutOfMemoryError: Java heap space
    Par sderecourt dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 14/04/2006, 11h28
  3. [java 1.5]java.util.concurrent
    Par afrikha dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 29/03/2006, 19h12
  4. Réponses: 4
    Dernier message: 13/02/2006, 21h58
  5. [postgres+java+RPM] comment java pour postgres
    Par thief dans le forum PostgreSQL
    Réponses: 6
    Dernier message: 18/09/2004, 17h52

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