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 :

Translation d'un rectangle


Sujet :

AWT/Swing Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2013
    Messages : 4
    Par défaut Translation d'un rectangle
    Bonjour,

    Je dois créer un rectangle qui se déplace horizontalement dans une fenetre puis reviens au point de départ. Le déplacement doit se faire periodiquement toutes les 200 millisecondes.

    Je suis débutante, et je ne sais pas du tout comment m'y prendre.

    Merci de votre aide

  2. #2
    Expert confirmé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2012
    Messages
    3 020
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Septembre 2012
    Messages : 3 020
    Par défaut
    Procède par étapes :

    1 : Commence par afficher un rectangle immobile à sa position de départ

    2 : Fais le bouger.

    Et pour t'aider, il y a des tutoriels un peu partout. Et puisque tu es étudiante, tu dois en plus avoir un cours là dessus

  3. #3
    Membre éprouvé
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Août 2005
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Août 2005
    Messages : 86
    Par défaut
    Hello,

    Comme Carhiboux l'a dis, il faut commencer par fabriquer ton rectangle (Classe MonRectangle) et le positionner dans une JFrame.

    Pour t'aider, je te met ces quelques lignes de code :

    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
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.geom.Rectangle2D;
     
    import javax.swing.JComponent;
     
    class MonRectangle extends JComponent implements Runnable {
     
        private static final long serialVersionUID = 1L;
     
        public MonRectangle() {
             //TODO :
        }
     
        @Override
        public void run() {
            //TODO : ici tu dois réflichir à comment faire bouger ce composant
        }
     
        @Override
        public void paint(Graphics g) {
            Shape shape = new Rectangle2D.Float(0,0, 50, 50);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.blue);
            g2.fill(shape);
        }
     
    }
    Awane

  4. #4
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2013
    Messages : 4
    Par défaut
    Merci pour vos réponses.
    Je vais essayer d'avancer avec cela, j'espère que je vais le réussir.

  5. #5
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2013
    Messages : 4
    Par défaut
    Bonjour à tous,

    J'ai pu avancer dans ma translation de rectangle. ça marche très bien, sauf que des fois le deplacement se bloque et j'ai un afffichage très bizare dans la fenêtre.

    Voici mon code :

    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
     
     
    public class RecTranslation {
     
        public static void main(String[] args) {
     
            MonRectangle monRectangle = new MonRectangle(0f,500f);
            final JFrame fenetre = new JFrame("Translation Rectangle");
     
            JPanel panel = new JPanel(new BorderLayout());
     
            panel.add(monRectangle, BorderLayout.CENTER);
            fenetre.add(monRectangle, BorderLayout.CENTER);
     
            fenetre.setSize(500, 300);
            fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fenetre.setVisible(true);
     
            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
            executor.scheduleAtFixedRate(monRectangle, 0L, 200, TimeUnit.MILLISECONDS);
        }
    }
     
     
    class MonRectangle extends JComponent implements Runnable {
     
        private static final long serialVersionUID = 1L;
        private float x1;
        private final float x2;
     
        public MonRectangle(float x1, float x2) {
          this.x1 = x1;
          this.x2 = x2;
        }
     
        @Override
        public void run() {
            if (x1 >= x2) {
                x1=10;
            }
            x1= x1 + 10;
     
            repaint();
        }
     
        @Override
        public void paint(Graphics g) {
            Shape shape =  new RoundRectangle2D.Float(x1,100, 50, 50, 10, 10);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
            int startX = 0, startY = 30, endX = 70, endY = 40;
            GradientPaint gradient = new GradientPaint(startX, startY, Color.white, endX, endY, Color.gray);
     
            g2.setPaint(gradient);
            g2.fill(shape);
            g2.setColor(Color.black);
            g2.setStroke(new BasicStroke(2));
            g2.draw(shape);
        }
    }
    Merci d'avance

  6. #6
    Membre éprouvé
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Août 2005
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Finance

    Informations forums :
    Inscription : Août 2005
    Messages : 86
    Par défaut
    Hello imane,

    Je pense qu'il faut executer ton repaint dans l'EDT (Event Dipatsh Thread)
    C'est le thread graphique de Swing (un peux plus d'explication ici :
    http://java.developpez.com/faq/gui/?...#GRAPHIQUE_EDT

    http://gfx.developpez.com/tutoriel/j...ing-threading/

    Si tu le dessin se fait en dehors de l'EDT, tu risques d'avoir des problèmes de mise à jour de ta vue.

    Tu dois le faire comme ceci :

    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
     
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Shape;
    import java.awt.geom.RoundRectangle2D;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
     
    public class RecTranslation {
     
        public static void main(String[] args) {
     
            MonRectangle monRectangle = new MonRectangle(0f,500f);
            final JFrame fenetre = new JFrame("Translation Rectangle");
     
            JPanel panel = new JPanel(new BorderLayout());
     
            panel.add(monRectangle, BorderLayout.CENTER);
            fenetre.add(monRectangle, BorderLayout.CENTER);
     
            fenetre.setSize(500, 300);
            fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    fenetre.setVisible(true);
                }
            });
     
     
            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
            executor.scheduleAtFixedRate(monRectangle, 0L, 200, TimeUnit.MILLISECONDS);
        }
    }
     
     
    class MonRectangle extends JComponent implements Runnable {
     
        private static final long serialVersionUID = 1L;
        private float x1;
        private final float x2;
     
        public MonRectangle(float x1, float x2) {
            this.x1 = x1;
            this.x2 = x2;
        }
     
        @Override
        public void run() {
            if (x1 >= x2) {
                x1=10;
            }
            x1= x1 + 10;
     
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    repaint();
                }
            });
        }
     
        @Override
        public void paint(Graphics g) {
            Shape shape =  new RoundRectangle2D.Float(x1,100, 50, 50, 10, 10);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
            int startX = 0, startY = 30, endX = 70, endY = 40;
            GradientPaint gradient = new GradientPaint(startX, startY, Color.white, endX, endY, Color.gray);
     
            g2.setPaint(gradient);
            g2.fill(shape);
            g2.setColor(Color.black);
            g2.setStroke(new BasicStroke(2));
            g2.draw(shape);
        }
    }
    Awane

  7. #7
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2013
    Messages : 4
    Par défaut
    Merci beaucoup awane.
    Je vais tester.

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

Discussions similaires

  1. translation de rectangle avec deux sliders
    Par nanouchg dans le forum Images
    Réponses: 3
    Dernier message: 11/10/2011, 20h21
  2. [MFC]Ecrire du texte dans un rectangle
    Par zaz16 dans le forum MFC
    Réponses: 8
    Dernier message: 29/07/2003, 10h31
  3. Comment centrer un Texte dans un rectangle ...
    Par Djedjeridoo dans le forum Composants VCL
    Réponses: 3
    Dernier message: 16/06/2003, 21h56
  4. Dessiner un rectangle avec bords et texte ...
    Par Djedjeridoo dans le forum Composants VCL
    Réponses: 3
    Dernier message: 16/06/2003, 17h17
  5. Réponses: 9
    Dernier message: 11/03/2003, 12h22

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