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

SWT/JFace Java Discussion :

SWT - GC et Thread


Sujet :

SWT/JFace Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2012
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2012
    Messages : 4
    Points : 4
    Points
    4
    Par défaut SWT - GC et Thread
    Bonjour,

    je n'arrive pas à créer un GC dans un Thread.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    static class monThread extends Thread {
    ....
     
        public run void() {
       GC monGC=new GC(canvas);
    ...
    }
    ...
    }
    ne donnera rien même si canvas est static.
    Y a-t-il une astuce ?

    Merci.

  2. #2
    Membre expert
    Avatar de Gueritarish
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2007
    Messages
    1 800
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2007
    Messages : 1 800
    Points : 3 919
    Points
    3 919
    Par défaut
    Salut,

    Comme il s'agit d'un élément graphique, il te faut le créer et t'en servir dans le thread IHM. Un bout de code qui fait la magie:
    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
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
     
    public class Snippet245 {
     
      /**
       * Default constructor.
       */
      public Snippet245() {
        final Display display = new Display();
        final Shell createdShell = createContent(display);
        createdShell.open();
        while (!createdShell.isDisposed()) {
          if (!display.readAndDispatch())
            display.sleep();
        }
        display.dispose();
      }
     
      public static void main(final String[] args) {
        new Snippet245();
      }
     
      /**
       * Draw an oval using a GC instance.
       */
      public void launchTreatment(final SelectionEvent anEvent) {
        final Runnable run = new Runnable() {
     
          /**
           * {@inheritDoc}
           */
          @Override
          public void run() {
            // Utilisation du Thread IHM:
            Display.getDefault().asyncExec(new Runnable() {
     
              /**
               * {@inheritDoc}
               */
              @Override
              public void run() {
                final Display display = anEvent.widget.getDisplay();
                final GC gc = new GC(display.getActiveShell());
                gc.setAntialias(SWT.ON);
                gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
                gc.fillOval(50, 50, 100, 60);
                gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
                gc.setLineWidth(2);
                gc.drawOval(50, 50, 100, 60);
                gc.dispose();
              }
            });
          }
        };
        new Thread(run).start();
      }
     
      /**
       * <p>
       * Create the UI content.
       * </p>
       * 
       * @param aDisplay
       *          the display to use as a parent.
       * @return the Shell created.
       */
      private Shell createContent(final Display aDisplay) {
        final Shell shell = new Shell(aDisplay);
        shell.setLayout(new GridLayout());
        final Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
        button.setText("Launch");
        button.addSelectionListener(new SelectionAdapter() {
          /**
           * {@inheritDoc}
           */
          @Override
          public void widgetSelected(final SelectionEvent anEvent) {
            launchTreatment(anEvent);
          }
        });
     
        final Rectangle clientArea = shell.getClientArea();
        shell.setBounds(clientArea.x + 10, clientArea.y + 10, 200, 200);
     
        return shell;
      }
    }
    Voilà, à+
    Gueritarish
    Pas de questions technique par MP, les forums sont là pour ça.

Discussions similaires

  1. [SWT] Gestion des Thread ?
    Par MrGlobox dans le forum SWT/JFace
    Réponses: 5
    Dernier message: 08/01/2013, 16h39
  2. org.eclipse.swt.SWTException: Invalid thread access
    Par LordDaedalus dans le forum SWT/JFace
    Réponses: 3
    Dernier message: 12/02/2010, 11h23
  3. [SWT] Thread et SWT
    Par mehdi.kiwi dans le forum SWT/JFace
    Réponses: 4
    Dernier message: 11/03/2009, 17h42
  4. [SWT] Redraw de composant SWT depuis un thread non SWT
    Par Slayne dans le forum SWT/JFace
    Réponses: 3
    Dernier message: 07/02/2008, 09h44
  5. [SWT]mise a jour ihm SWT par un thread
    Par will82 dans le forum SWT/JFace
    Réponses: 1
    Dernier message: 06/08/2004, 11h37

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