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

JavaFX Discussion :

Lier une classe graphique au sample.fxml


Sujet :

JavaFX

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 2
    Points : 1
    Points
    1
    Par défaut Lier une classe graphique au sample.fxml
    Bonsoir à tous,

    Je recherche un peu d'aide pour pouvoir poursuivre le développement d'une petite application javafx.
    voici ma question:

    Novice en la matière, je développe actuellement une petite application capable d'afficher des formes géométriques. J'utilise le RTE Intellij IDEA dans lequel j'ai créer un nouveau projet javaFX.

    Le but ici est d'afficher une croix.

    Main.java: je n'ai apporté aucune modification à la classe 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
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    package sample;
     
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Group;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    import sample.model.Segment;
     
    /* dans le main je n'ai touche a rien*/
     
    public class Main extends Application {
     
        @Override
        public void start(Stage primaryStage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            primaryStage.setTitle("Jeux");
            primaryStage.setScene(new Scene(root, 1000, 500));
            primaryStage.show();
        }
     
        public static void main(String[] args) {
            launch(args);
        }
    }
    sample.fxml: j'utilise scene buider pour construire l interface graphique. Celle-ci contient actuellement une BorderPane dans laquelle on trouve un bouton jouer et un noeud graphique Pane au centre. Je cherche à lier d'autre classe que le Controller au pane:

    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
     
    <?xml version="1.0" encoding="UTF-8"?>
     
    <?import javafx.scene.canvas.*?>
    <?import javafx.scene.paint.*?>
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.layout.GridPane?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
     
    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller" >
     
       <top>
          <Accordion BorderPane.alignment="CENTER">
            <panes>
              <TitledPane animated="false" textOverrun="CENTER_ELLIPSIS">
                <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                </content>
                   <graphic>
                      <Button fx:id="Jouer" mnemonicParsing="false" onAction="#jouer" prefHeight="54.0" prefWidth="110.0" text="Jouer" textAlignment="CENTER" textFill="#3219b0" />
                   </graphic>
              </TitledPane>
              <TitledPane animated="false" text="untitled 2">
                <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                </content>
              </TitledPane>
            </panes>
          </Accordion>
       </top>
        <center>
            <Pane fx:id="pane" onMousePressed="#onMouseClicked" prefHeight="313.0" prefWidth="601.0">
             <children>
                <Canvas height="260.0" width="267.0" styleClass="sample.model.Croix" />
     
             </children></Pane>
                </center>
    </BorderPane>
    Dans mon Controller se trouve la méthode qui me permet d'interagir avec le bouton:
    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
     
    package sample;
     
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.Pane;
    import javafx.scene.shape.Line;
    import sample.model.Croix;
    import sample.model.Point;
    import sample.model.Segment;
     
     
    public class Controller {
     
        @FXML
        private Pane pane;
     
     
        public void jouer(ActionEvent actionEvent) {
            System.out.println("clic!");
            Point a = new Point(50, 50);
            Croix croixDepart = new Croix(a);
            //croixDepart.afficherCroix(a); // c est cette ligne que je n arrive pas a faire fonctionner
     
    /* //affichage croix
    //ici l'affichage fonctionne bien mais avec les même instructions dans ma méthode afficherCroix() de ma classe Croix ça ne fonctionne pas.
    //Je souhaiterais pouvoir supprimer ces lignes et utiliser uniquement: croixDepart.afficherCroix(a).
     
            Line line2 = new Line(croixDepart.getGauche().getX(), croixDepart.getGauche().getY(), croixDepart.getDroite().getX(),croixDepart.getDroite().getY());
            line2.setStroke(javafx.scene.paint.Color.BLACK);
            line2.setStrokeWidth(1);
            Line line3 = new Line(croixDepart.getHaut().getX(), croixDepart.getHaut().getY(), croixDepart.getBas().getX(),croixDepart.getBas().getY());
            this.pane.getChildren().addAll(line2, line3);
            //fin affichage croix*/
    En ce qui concerne ma classe Croix, c est la méthode afficherCroix() que je n'arrive pas à faire fonctionner:
    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
     
    package sample.model;
     
    import javafx.fxml.FXML;
    import javafx.scene.Parent;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.layout.Pane;
    import javafx.scene.shape.Line;
    import javafx.scene.shape.LineTo;
    import javafx.scene.shape.MoveTo;
     
     
    public class Croix extends Parent {
     
        @FXML
        private Pane pane;
     
     
        private Point centre = new Point();
        private Point gauche = new Point();
        private Point droite = new Point();
        private Point haut = new Point();
        private Point bas = new Point();
        private Segment seg1 = new Segment();
        private Segment seg2 = new Segment();
     
     
        public Croix(Point centre, Segment seg1, Segment seg2) {
     
            this.centre = centre;
            this.seg1 = seg1;
            this.seg2 = seg2;
        }
     
        public Croix(Point centre) {
            this.centre = centre;
     
            this.gauche.setX(this.centre.getX() -10);
            this.gauche.setY(this.centre.getY());
            this.droite.setX(this.centre.getX() + 10);
            this.droite.setY(this.centre.getY());
            this.haut.setX(this.centre.getX());
            this.haut.setY(this.centre.getY()+10);
            this.bas.setX(this.centre.getX());
            this.bas.setY(this.centre.getY()-10);
     
            System.out.println("creation d une croix");
        }
     
        //getter
        public Point getGauche() {return gauche;}
        public Point getDroite() {return droite;}
        public Point getHaut() {return haut;}
        public Point getBas() {return bas;}
        public Point getCentre() {return centre;}
        public Segment getSeg1() {return seg1;}
        public Segment getSeg2() {return seg2;}
     
     
        //setter
        public void setGauche(Point gauche) {this.gauche = gauche;}
        public void setDroite(Point droite) {this.droite = droite;}
        public void setHaut(Point haut) {this.haut = haut;}
        public void setBas(Point bas) {this.bas = bas;}
        public void setCentre(Point centre) {this.centre = centre;}
        public void setSeg1(Segment seg1) {this.seg1 = seg1;}
        public void setSeg2(Segment seg2) {this.seg2 = seg2;}
     
        public Point positionCroix() {
     
            Point position = new Point(this.centre.getX(),this.centre.getY());
            return position;
        }
     
        public void afficherCroix(Point a) {
     
            Croix croixAffichage = new Croix(a);
     
            Line line2 = new Line(croixAffichage.getGauche().getX(), croixAffichage.getGauche().getY(), croixAffichage.getDroite().getX(),croixAffichage.getDroite().getY());
            line2.setStroke(javafx.scene.paint.Color.BLACK);
            line2.setStrokeWidth(1);
            Line line3 = new Line(croixAffichage.getHaut().getX(), croixAffichage.getHaut().getY(), croixAffichage.getBas().getX(),croixAffichage.getBas().getY());
            this.pane.getChildren().addAll(line2, line3);// ici la croix ne s'affiche pas
    Voila, pour résumé je souhaites faire afficher ma croix dans le Pane en passant par une methode afficherCroix qui appartient à la classe croix mais ça ne fonctionne pas. En revanche, les mêmes lignes de code fonctionne dans le Controller. Mon idée est de ne pas surcharger le Controller et ne pas avoir à taper toutes les lignes à chaque fois que je souhaites tracer une nouvelle croix, d'ou le besoin d'utiliser des méthode d'affichage dans mes classes graphiques.

    Merci par avance pour votre aide, j’espère avoir été assez clair. Dans le cas contraire n’hésitez pas à me demander d'avantage d'informations.

  2. #2
    Rédacteur/Modérateur

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

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

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Ben avec le code partiel que tu as envoyé, le pane dans la Croix est jamais positionnée à quoi que ce soit et donc est null.

    Cependant la structure est bizarroïde... ta Croix étend Parent donc elle pourrait directement contenir des segments mais tu insères tes segments dans un Pane ??? Bref, j'ai un peu de mal à comprendre ce que tu veux faire.
    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

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 2
    Points : 1
    Points
    1
    Par défaut
    Bonjour,

    merci pour ta réponse, j'ai bien pris en compte tes remarques mais je n'arrive toujours pas à afficher ma croix.
    aurais tu une modification à me proposer pour voir si ça fonctionne.

    merci

  4. #4
    Rédacteur/Modérateur

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

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

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    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
    package cross;
     
    import javafx.beans.property.DoubleProperty;
    import javafx.beans.property.ObjectProperty;
    import javafx.beans.property.ReadOnlyDoubleProperty;
    import javafx.beans.property.ReadOnlyDoubleWrapper;
    import javafx.beans.property.SimpleDoubleProperty;
    import javafx.beans.property.SimpleObjectProperty;
    import javafx.scene.layout.Region;
    import javafx.scene.paint.Color;
    import javafx.scene.paint.Paint;
    import javafx.scene.shape.Line;
     
    public final class Cross extends Region {
     
        private final Line segment1 = new Line();
        private final Line segment2 = new Line();
     
        public Cross() {
            super();
            segment1.setFill(Color.BLACK);
            segment1.strokeProperty().bind(strokeProperty());
            segment2.setFill(Color.BLACK);
            segment2.strokeProperty().bind(strokeProperty());
            getChildren().addAll(segment1, segment2);
            radiusProperty().addListener(observable -> {
                updateGeometry();
                updateLocation();
            });
            centerXProperty().addListener(observable -> updateLocation());
            centerYProperty().addListener(observable -> updateLocation());
            updateGeometry();
            updateLocation();
        }
     
        private void updateGeometry() {
            final double diameter = 2 * getRadius();
            segment1.setEndX(diameter);
            segment1.setEndY(diameter);
            segment2.setStartX(diameter);
            segment2.setEndY(diameter);
            setMinSize(diameter, diameter);
            setMaxSize(diameter, diameter);
            setPrefSize(diameter, diameter);
        }
     
        private void updateLocation() {
            final double radius = getRadius();
            final double centerX = getCenterX();
            final double centerY = getCenterY();
            setTranslateX(centerX - radius);
            setTranslateY(centerY - radius);
        }
     
        @Override
        public boolean isResizable() {
            return false;
        }
     
        private final ReadOnlyDoubleWrapper radius = new ReadOnlyDoubleWrapper(this, "radius", 5);
     
        public final double getRadius() {
            return radius.get();
        }
     
        public final void setRadius(final double value) {
            final double v = Math.max(value, 0);
            radius.set(v);
        }
     
        public final ReadOnlyDoubleProperty radiusProperty() {
            return radius.getReadOnlyProperty();
        }
     
        private final DoubleProperty centerX = new SimpleDoubleProperty(this, "centerX", 0);
     
        public final double getCenterX() {
            return centerX.get();
        }
     
        public final void setCenterX(final double value) {
            centerX.set(value);
        }
     
        public final DoubleProperty centerXProperty() {
            return centerX;
        }
     
        private final DoubleProperty centerY = new SimpleDoubleProperty(this, "centerY", 0);
     
        public final double getCenterY() {
            return centerY.get();
        }
     
        public final void setCenterY(final double value) {
            centerY.set(value);
        }
     
        public final DoubleProperty centerYProperty() {
            return centerY;
        }
     
        private final ObjectProperty<Paint> stroke = new SimpleObjectProperty<>(this, "stroke", Color.BLACK);
     
        public final Paint getStroke() {
            return stroke.get();
        }
     
        public final void setStroke(final Paint value) {
            stroke.set(value);
        }
     
        public final ObjectProperty<Paint> strokeProperty() {
            return stroke;
        }
    }
    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
    package cross;
     
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ToolBar;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    import org.scenicview.utils.ScenicViewBooter;
     
    public class Main extends Application {
     
        @Override
        public void start(Stage primaryStage) throws Exception {
            final AnchorPane drawArea = new AnchorPane();        
            final Button addButton = new Button("Add");
            addButton.setOnAction(actionEvent -> {
                final Cross cross = new Cross();
                cross.setStroke(Color.BLUE);
                cross.setRadius(25);
                final double x = drawArea.getWidth() * Math.random();
                final double y = drawArea.getWidth() * Math.random();
                cross.setCenterX(x);
                cross.setCenterY(y);
                Circle circle = new Circle();
                circle.setFill(Color.RED);
                circle.setRadius(5);
                circle.setCenterX(x);
                circle.setCenterY(y);
                drawArea.getChildren().addAll(circle, cross);
            });
            final ToolBar toolBar = new ToolBar();
            toolBar.getItems().add(addButton);
            final BorderPane root = new BorderPane();        
            root.setCenter(drawArea);
            root.setTop(toolBar);
            final Scene scene = new Scene(root, 500, 500);
            primaryStage.setTitle("Test");
            primaryStage.setScene(scene);
            primaryStage.show();
            //
            //ScenicViewBooter.show(scene);
        }
     
        public static void main(String... args) {
            Application.launch(args);
        }
    }
    PS : le cercle rouge c'est juste pour vérifier que les croix sont bien placées.
    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

Discussions similaires

  1. lier une classe JAVA dans un fichier JSP
    Par zezitinho dans le forum NetBeans
    Réponses: 6
    Dernier message: 21/11/2008, 10h38
  2. lier une classe a un clip
    Par donnie62 dans le forum ActionScript 3
    Réponses: 2
    Dernier message: 23/05/2008, 09h49
  3. Lier une interface graphique à un analyseur
    Par dreanlin dans le forum Prolog
    Réponses: 11
    Dernier message: 01/03/2008, 12h06
  4. [VB.net] Lier une Class à une BD
    Par badrel dans le forum Windows Forms
    Réponses: 1
    Dernier message: 14/12/2007, 09h30
  5. Réponses: 5
    Dernier message: 26/05/2005, 15h40

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