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 :

Comment dessiner une petite interface de radar?


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 418
    Par défaut Comment dessiner une petite interface de radar?
    salut,

    je desire mettre en place une petite fenetre contenanat une interface graphique d'un radar qui se presente comme suit;

    - un rectangle noir de dimension 1010*1010 par exemple
    - 10 cercles circonscris contenus dans cet espace forme par le rectangle noir et ayant une couleur verte
    - deux axes definissaant le repere en partant diu centre du rectangle
    - une ligne balayant le cercle pour simuler la sonde ou l'antenen d'un radar , donc elel fait le tour du cercle indefiniement sauf si on clique sur un bouton "stop" en bas du rectangle


    je suis nouveau parmi vous et j'avoue que je suis perdu pour realisr ceci...si quelqu'un pourrait bien m'aider ca sera genial ..et encore merci

  2. #2
    Expert confirmé
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Par défaut
    Quelques pistes :

    • Pour ce qui est du trait qui tourne, il va falloir t'orienter vers la Classe Timer
    • Pour le dessin en général, regarde la classe Graphics et Graphics2D
    • Ensuite, fais une recherche sur le forum des sujet traitant du dessin pour te baser sur des exemples

  3. #3
    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
    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
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.EventQueue;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
     
    /**
     * Created by IntelliJ IDEA.
     * User: bebe
     * Date: 21-Jun-2006
     * Time: 20:42:44
     * To change this template use File | Settings | File Templates.
     */
    public class MonRadar extends JComponent {
     
        public MonRadar() {
            setPreferredSize(new Dimension(800, 800));
        }
     
        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
     
    //        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    //        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
     
            g2d.setColor(Color.BLACK);
            g2d.fillOval(0, 0, 800, 800);
     
            g2d.setColor(Color.GREEN);
            g2d.drawOval(0, 0, 800, 800);
     
            g2d.drawOval(100, 100, 600, 600);
            g2d.drawOval(200, 200, 400, 400);
            g2d.drawOval(300, 300, 200, 200);
            g2d.drawOval(400, 400, 1, 1);
     
            g2d.drawLine(0, 400, 800, 400);
            g2d.drawLine(400, 0, 400, 800);
     
        }
     
        public static void main(String[] args) {
            Runnable runnable = new Runnable() {
                public void run() {
                    JFrame myFrame = new JFrame("Mon radar...");
                    myFrame.add(new MonRadar());
                    myFrame.pack();
                    myFrame.setResizable(false);
                    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    myFrame.setVisible(true);
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    voila un code rapide qui te montre le principe du dessins.
    pour la barre qui circule tu vas devoir utiliser des cosinus et sinus.

  4. #4
    Rédacteur/Modérateur

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

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

    Informations forums :
    Inscription : Août 2005
    Messages : 6 913
    Billets dans le blog
    54
    Par défaut
    Je me suis un peu amuse a tripatouiller le code de bbclone ; enjoy !
    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
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
     
    package test;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
     
    /** Displays a radar.
     */
    public class RadarComponent extends JComponent {
     
      /** Default width.
       */
      public static int DEFAULT_WIDTH = 800;
     
      /** Default height.
       */
      public static int DEFAULT_HEIGHT = 800;
     
      /** Back base paint.
       * @todo Make this a configurable property.
       */
      private static final Color BACK_RADAR_PAINT = Color.BLACK;
     
      /** Front base paint.
       * @todo Make this a configurable property.
       */
      private static final Color FRONT_RADAR_PAINT = Color.GREEN;
     
      /** Paint of the passive area.
       * @todo Make this a configurable property.
       */
      private static final Color FRONT_PASSIVE_RADAR_PAINT = FRONT_RADAR_PAINT.darker().darker();
     
      /** Paint of the active line.
       * @todo Make this a configurable property.
       */
      private static final Color FRONT_ACTIVE_RADAR_PAINT = FRONT_RADAR_PAINT;
     
      /** Paint of the echo area.
       * @todo Make this a configurable property.
       */
      private static final Color FRONT_ACTIVE_RADAR_TRANSPARENT_PAINT = new Color(FRONT_RADAR_PAINT.getRed(), FRONT_RADAR_PAINT.getGreen(), FRONT_RADAR_PAINT.getBlue(), 0);
     
      /** Stroke of the passive line.
       * @todo Make this a configurable property.
       */
      private static final Stroke PASSIVE_STROKE = new BasicStroke(0.8f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
     
      /** Stroke of the active line.
       * @todo Make this a configurable property.
       */
      private static final Stroke ACTIVE_STROKE = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
     
      /** Default sleep time.
       * @todo Make this a configurable property.
       */
      private int SLEEP_TIME = 16;
     
      /** Listener for timer events and component resizing events.
       */
      private InnerListener innerListener = new InnerListener();
     
      /** Timer for display refresh.
       */
      private Timer timer = new Timer(SLEEP_TIME, innerListener);
     
      /** Re-usable line.
       */
      private Line2D.Float line = new Line2D.Float();
     
      /** Re-usable pie slice shape.
       */
      private Arc2D.Float arc = new Arc2D.Float();
      private Rectangle2D arcRect = new Rectangle2D.Float();
     
      /** Re-usable circular frame.
       */
      private Ellipse2D.Float ellipse = new Ellipse2D.Float();
     
      /** Current paint for the echo area.
       */
      private GradientPaint gradient;
     
      /** Current radius in pixels.
       */
      private float radius;
     
      /** Current diameter in pixels.
       */
      private float diameter;
     
      /** Current X coordinate of the center in pixels.
       */
      private float centerX;
     
      /** Current Y coordinate of the center in pixels.
       */
      private float centerY;
     
      /** Current X coordinate of the top left corner of the radar area in pixels.
       */
      private float radarX;
     
      /** Current Y coordinate of the top left corner of the radar area in pixels.
       */
      private float radarY;
     
      /** Current angle in radians.
       */
      double angle = 0;
     
      /** Angle offset in radians.
       * @todo Make this a configurable property.
       */
      double da = 2 * Math.PI / 1000;
     
      /** Creates a new instance.
       */
      public RadarComponent() {
        super();
        setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_WIDTH));
        precalculate();
        addComponentListener(innerListener);
        timer.start();
      }
     
      /** Precalculates and sets all necessary measures and objects.
       */
      private void precalculate() {
        int width = getWidth();
        int height = getHeight();
        radius = Math.min(width, height) / 2f;
        if (radius != 0) {
          diameter = 2 * radius;
          centerX = width / 2f;
          centerY = height / 2f;
          radarX = centerX - radius;
          radarY = centerY - radius;
          // Note arc intialization angles are in degrees.
          arc.setArc(new Rectangle2D.Double(radarX, radarY, diameter, diameter), 0f, 20f, Arc2D.PIE);
          arcRect.setRect(centerX, centerY - radius / 8f, radius, radius / 8f);
          // Rescale gradient accordingly.
          // Note : this is a "should look OK" approximation. Could be better.
          gradient = new GradientPaint(centerX, centerY - radius / 8f, FRONT_ACTIVE_RADAR_TRANSPARENT_PAINT, centerX, centerY, FRONT_RADAR_PAINT, false);
        }
      }
     
      /**  {@inheritDoc}
       */
      @Override protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if ((radius > 0) && isVisible() && isShowing()) {
          Graphics2D g2d = (Graphics2D) g;
     
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
          // Paint back circle.
          g2d.setPaint(BACK_RADAR_PAINT);
          ellipse.setFrame(radarX, radarY, diameter, diameter);
          g2d.fill(ellipse);
          // Paint axes.
          g2d.setStroke(PASSIVE_STROKE);
          g2d.setPaint(FRONT_PASSIVE_RADAR_PAINT);
          float dx = radius / 4f;
          for (int i = 0; i < 5; i++) {
            ellipse.setFrame(radarX + i * dx, radarY + i * dx, diameter - 2 * i * dx, diameter - 2 * i * dx);
            g2d.draw(ellipse);
          }
          line.setLine(radarX, centerY, radarX + diameter, centerY);
          g2d.draw(line);
          line.setLine(centerX, radarY, centerX, radarY + diameter);
          g2d.draw(line);
          // Backup current transformation.
          AffineTransform backTransform = g2d.getTransform();
     
          // Rotate to proper angle.
          g2d.rotate(angle, centerX, centerY);
     
          //Paint echo zone.
          g2d.setPaint(gradient);
          g2d.fill(arc);
          //g2d.fill(arcRect);
     
          // Paint active line.
          g2d.setStroke(ACTIVE_STROKE);
          g2d.setPaint(FRONT_ACTIVE_RADAR_PAINT);
          line.setLine(centerX, centerY, centerX + radius, centerY);
          g2d.draw(line);
     
          //g2d.rotate(-angle, centerX, centerY);
          // Restore transformation, avoid possible rounding error.
          g2d.setTransform(backTransform);
        }
      }
     
      /** Program entry point.
       * @param args Arguments from the command line.
       */
      public static void main(String ...args) {
        Runnable runnable = new Runnable() {
          /**  {@inheritDoc}
           */
          public void run() {
            JFrame myFrame = new JFrame("A radar");
            myFrame.add(new RadarComponent());
            myFrame.pack();
            //myFrame.setResizable(false);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setVisible(true);
          }
        };
        SwingUtilities.invokeLater(runnable);
      }
     
      /** Inner listener for this component
       */
      private class InnerListener implements ActionListener, ComponentListener {
        /** Creates a new instanc.
         */
        public InnerListener() {
        }
     
        /** {@inheritDoc}
         * <br>Reacts to timer events.
         */
        public void actionPerformed(ActionEvent event) {
          angle += da;
          if (isVisible() && (isShowing())) {
            repaint();
          }
        }
     
        /** {@inheritDoc}
         *  <br>Rescale graphical objects according to new size.
         */
        public void componentResized(ComponentEvent event) {
          precalculate();
          if (isVisible() && (isShowing())) {
            repaint();
          }
        }
     
        /**  {@inheritDoc}
         */
        public void componentMoved(ComponentEvent event) {
        }
     
        /**  {@inheritDoc}
         */
        public void componentShown(ComponentEvent event) {
        }
     
        /**  {@inheritDoc}
         */
        public void componentHidden(ComponentEvent e) {
        }
      }
    }
    AffineTransforme est ton amie ; pas besoin de sinus ou de cosinus (en general).
    Regle de base : si tu sais dessiner a l'horizontale ; alors tu sais dessiner avec n'importe quel angle (ou autre transformation affine). Faut savoir etre faineant quand il le faut !

    EDIT : je pensais jouer un peu avec Mustang pour faire un effet un peu plus realiste en utilisant un autre type de gradiant ; mais d'une part, l'affichage 2D dec@#$ toujours sur mon ordi avec Java 6:


    Voir egalement ca : http://bugs.sun.com/bugdatabase/view...bug_id=6440758

    Et d'autre part, le gradiant radial de Mustang ne fait pas trop ce que j'attendais. Le probleme vient probalement d'une confusion due a Paint Shop Pro auquel je suis habite et qui le nomme sunburst alors que j'essaie de faire quelques chose proche de son gradient radial a lui (Angle Gradient dans Photoshop) qui est en fait plutot un type de gradient circulaire (ce qui n'est pas du tout pas la meme chose). J'aurai donc voulu plutot utilise un truc du genre :



    Ce qui permettait de simuler l'effacement progressif de la surface du radar. J'espere qu'il rajouteront egalement ce type de gradiant avant la sortie de Mustang.
    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

  5. #5
    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
    > Je me suis un peu amuse a tripatouiller le code de bbclone ; enjoy !
    LOL
    le code que j'ai fait m'a pris 3 minute maximum
    c'était pour le mettre sur la voix!

    le code tu a fait ca ressemble plut du tout au code que j'ai fait c'est plus long a faire

  6. #6
    Membre éclairé Avatar de soad
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    520
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Février 2004
    Messages : 520
    Par défaut
    C'est quand meme magnifique, sur développez on vous pond le code à votre place lol

    Mais sinon ca le fais pas mal ton radar bouye... (j'ai pas exécuter le code mais d'après les images)

Discussions similaires

  1. Comment dessiner une flèche ?
    Par Amine_sas dans le forum 2D
    Réponses: 4
    Dernier message: 20/12/2006, 22h58
  2. Réponses: 2
    Dernier message: 08/05/2006, 21h23
  3. Comment positionner une petite boite dans une boite ?
    Par hackrobat dans le forum Balisage (X)HTML et validation W3C
    Réponses: 6
    Dernier message: 25/10/2005, 10h37
  4. Réponses: 3
    Dernier message: 29/06/2005, 15h29
  5. Comment dessiner une ligne dans un Chart ?
    Par libititi dans le forum Composants VCL
    Réponses: 3
    Dernier message: 16/06/2005, 15h56

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