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 :

Affichage d'image compliqué


Sujet :

AWT/Swing Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 66
    Points : 41
    Points
    41
    Par défaut Affichage d'image compliqué
    Bonjour à tous,
    J'ai une interface graphique composé de plusieurs panneau, dans un de ces paneau j'affiche un trajet (ca ca fonctionne) et dans un un autre j'affiche des images (jPanelVisualisation), j'essai pour l'instant d'aficher juste un trait dans la jpanelTrajet et juste une image dans le jpanelVisualisation, si j'arrive à faire ca j'arriverais à faire la suite.
    Pour le trait dans le panneau trajet ca fonctionne, par contre pour l'image je n'y arrive pas, je ne sait pas ce que je doit mettre au dernier paramètre de g.drawImage(img, 10,10,50,100,this);, par quoi remplacer le this?

    voila mon code:

    main:
    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
     
    package essai;
     
    public class Main {
     
     
        public Main() {
        }
     
     
        public static void main(String[] args) {
            Application appli= new Application();
        }
     
    }
    InterfaceGraphique
    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
     
     
    package essai;
     
    import java.awt.Graphics;
     
     
    public class InterfaceGraphique extends javax.swing.JFrame {
        private Graphics gTraj;
        private Graphics gVis;
        private int hauteur=0;
        private int largeur=0;
        private Application app=null;
     
     
        public InterfaceGraphique() {
     
        }
     
          public InterfaceGraphique(Application app) {
            initComponents();
            this.setSize(1024,760);
            this.app=app;
            this.gTraj=jPanelTrajet.getGraphics();
            this.gVis=jPanelVisualisation.getGraphics();
            this.largeur=jPanelTrajet.getWidth();
            this.hauteur=jPanelTrajet.getHeight();
     
        }
     
          public Graphics getGTraj(){
              return this.gTraj;
          }
     
           public Graphics getGVis(){
              return this.gVis;
          }
     
          public int getLargeur(){
              return this.largeur;
          }
     
          public int getHauteur(){
          return this.hauteur;
          }
     
         public void paint(Graphics g){
             super.paint(g);
         }
     
     
        private void initComponents() {
    //ici se trouve le code généré par netbeans pour l'interface que j'ai supprimer pour le forum
        }                    
     
        private void jButtonArreterActionPerformed(java.awt.event.ActionEvent evt) {                                               
    app.arreterApplication();
    // TODO add your handling code here:
        }                                              
     
        private void jButtonDemarrerActionPerformed(java.awt.event.ActionEvent evt) {                                                
            repaint();
            app.demarrerApplication();
    // TODO add your handling code here:
        }                                               
     
      /*
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new InterfaceGraphique().setVisible(true);
                }
            });
        }*/
     
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButtonArreter;
        private javax.swing.JButton jButtonAutomatique;
        private javax.swing.JButton jButtonDemarrer;
        private javax.swing.JButton jButtonManuel;
        private javax.swing.JButton jButtonOuvrir;
        private javax.swing.JButton jButtonSauvegarder;
        private javax.swing.JPanel jPanelApplication;
        private javax.swing.JPanel jPanelCommande;
        private javax.swing.JPanel jPanelFichier;
        private javax.swing.JPanel jPanelFonctionnement;
        private javax.swing.JPanel jPanelHistorique;
        private javax.swing.JPanel jPanelTrajet;
        private javax.swing.JPanel jPanelVisualisation;
        // End of variables declaration                   
     
    }

    Application:
    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
     
     
    package essai;
     
    import java.awt.Graphics;
     
     
     
     
    public class Application {
        private InterfaceGraphique interGraph=null;
        private Trajet t=null;
        private Visualisation v=null;
        private Graphics g;
     
     
     
        public Application() {
            this.interGraph=new InterfaceGraphique(this);
            this.interGraph.setVisible(true);
            init();
     
        }
     
        public void init(){
            initObjects();
        }
     
        public void initObjects(){
            this.t=new Trajet(this.interGraph.getGTraj(),this.interGraph.getLargeur(),this.interGraph.getHauteur());
            this.v=new Visualisation(this.interGraph.getGVis());
        }
     
        public void demarrerApplication(){
            this.interGraph.repaint();
     
            t.start();
            v.start();
     
     
     
        }
     
     
     
        public void arreterApplication(){
            t.stopTrajet();
        }
     
    }
    Trajet:
    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
     
     
    package essai;
     
    import java.awt.*;
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
    public class Trajet extends Thread{
        private Graphics g;
        private boolean enCours=true;
     
        public Trajet() {
        }
     
        public Trajet(Graphics g,int largeur,int hauteur){
            this.g=g;
     
        }
     
        public void manuel(){
            traceTraj(this.g);
        }
     
        public void run(){
            enCours=true;
            do{
                manuel();
     
            }
            while(enCours);
        }
     
        public void stopTrajet(){
            this.enCours=false;
     
        }
     
        private void traceTraj(Graphics g) {
            Graphics2D g2D=(Graphics2D)g;
            g2D.setPaint(Color.GREEN);
     
            this.g.drawLine(0,0,200,100);
        }
     
     
    }

    Visualisation:

    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
     
    package essai;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.io.File;
     
     
    public class Visualisation extends Thread {
        private Graphics g;
        public boolean enCours=true;
        InterfaceGraphique intergraph;
        File file;
        Image img;
        int a;
     
        public Visualisation() {
        }
     
        public Visualisation(Graphics g) {
            this.g=g;
        }
     
        public void run(){
     
            enCours=true;
            do{
                afficher();
            }
            while(enCours);
        }
     
        public void afficher(){
     
            afficherImage(this.g);
        }
     
        private void afficherImage(Graphics g) {
     
            file=new File("Q:\\photos\\Canard1.jpg");
            String name=file.getName();
            img=Toolkit.getDefaultToolkit().getImage(name) ;
            g.drawImage(img, 10,10,50,100,this);
            System.out.println("image");
     
     
        }
     
    }

  2. #2
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Le dernier paramètre ,d'après la javadoc, est l'ImageObserver que tu veux appliquer à ton image. Si tu veux en utiliser un crée le avant et passe le en paramètre, si tu n'en veux pas passe lui la valeur null.
    autant l'hiver éclate que l'hétéroclite
    le vrai geek c'est celui qui croit qu'il y a 1024 mètres dans un kilomètre

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 66
    Points : 41
    Points
    41
    Par défaut
    et c'est quoi l'imageObserver? ca sert à quoi?
    Pqarce que là je n'ai aucune image qui s'affiche je pensais que le problème venait de là mais enfait peut être pas.

  4. #4
    Membre émérite
    Avatar de xavlours
    Inscrit en
    Février 2004
    Messages
    1 832
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 1 832
    Points : 2 410
    Points
    2 410
    Par défaut
    Bonjour,

    je pense qu'un petit tour sur la FAQ répondra à ta question. En fait, il suffit de surcharger la méthode paintComponent, et ça marche tout seul. Tu n'as donc pas besoin de faire circuler les objets Graphics dans tous les constructeurs, Swing s'en occupe pour toi.
    [EDIT] Au fait, pour l'ImageObserver, tu peux mettre null.
    "Le bon ni le mauvais ne me feraient de peine si si si je savais que j'en aurais l'étrenne." B.V.
    Non au langage SMS ! Je ne répondrai pas aux questions techniques par MP.
    Eclipse : News, FAQ, Cours, Livres, Blogs.Et moi.

  5. #5
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    A priori, d'après la javadoc toujours, c'est un observer qui te prévient si les paramètres de ton image change.
    autant l'hiver éclate que l'hétéroclite
    le vrai geek c'est celui qui croit qu'il y a 1024 mètres dans un kilomètre

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

Discussions similaires

  1. [FLASH MX] Affichage d'images par loadMovie
    Par n_tony dans le forum Flash
    Réponses: 7
    Dernier message: 23/09/2004, 15h34
  2. XSL: pb d'affichage d'images
    Par enez dans le forum XSL/XSLT/XPATH
    Réponses: 10
    Dernier message: 12/09/2004, 14h17
  3. PB affichage d'image avec IE
    Par arturo dans le forum Modules
    Réponses: 6
    Dernier message: 25/09/2003, 17h28
  4. [VB6] Affichage d'image avec qlq contraintes
    Par youri dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 21/11/2002, 14h44

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