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

2D Java Discussion :

Problème de Graphics


Sujet :

2D Java

  1. #1
    Candidat au Club
    Inscrit en
    Février 2012
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Février 2012
    Messages : 3
    Par défaut Problème de Graphics
    Bonjour, j’ai deux classes qui hérite de la classe JPanel et qui ont tous les deux des graphismes.

    Par exemple :
    1. Sur le premier JPanel j’ai un rectangle blanc qui occupe toute la surface.
    2. Sur le deuxième j’ai un petit rectangle bleu.


    Comment pourrais-je fusionner les deux graphiques pour pouvoir afficher la surface blanche et le rectangle bleu sur un JFrame ?

    Voici le résultat voulu : http://img713.imageshack.us/img713/462/sanstitrewdq.png

  2. #2
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Le faire dans un seul panel dans lequel on dessine les deux rectangles dans la méthode paintComponent.

  3. #3
    Candidat au Club
    Inscrit en
    Février 2012
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Février 2012
    Messages : 3
    Par défaut
    Oui, je sais faire cela, je voudrais savoir s’il y a une méthode pour récupérer les « Graphics » d’un JPanel et les afficher sur un autre ?

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Février 2011
    Messages
    56
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 56
    Par défaut
    Bonjour,

    Je vais peut-être dire une connerie, mais au cas ou :

    il faut tracer avec des shapes,

    - tu créer un Arraylist de Shapes pour ton jFrame
    - tu créer un Arraylist de Shapes pour ton jPanel1
    - tu créer un Arraylist de Shapes pour ton jPanel2

    Quand tu traces dans ton jPanel1 ou jPanel2 tu met aussi tes shapes dans l'ArrayList de ton jFrame

    Enfin, c'est ce que je ferai

    Cdt,
    Dmf

  5. #5
    Candidat au Club
    Inscrit en
    Février 2012
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Février 2012
    Messages : 3
    Par défaut
    Bonjour,

    Ça pourra résoudre le problème, mais dans le cas où j’ai 20 classes de JPanel avec 20 Graphics differents et je voudrais juste envoyer comme paramètre mon JFrame à un JPanel et c’est à lui de récupérer le Graphic d’un JPanel X dans le JFrame et de lui ajouter ses propres Graphics et d’afficher le résultat sur ce JPanel X du JFrame.

    Pour faire simple :

    J’ai un JFrame là où il y a un JPanel X (qui contient des Graphics).
    J’ai 20 JPanel avec des Graphics.
    Quand j’instancie l’un des 20 JPanel dans mon JFrame, j’envoie mon JFrame comme paramètre, (dans le corps du constructeur je le récupère), je voudrais pouvoir ajouter au Graphics courant du JPanel X (du JFrame) le Graphic du JPanel instancié !

    J’espère que j’étais clair…

    PS: Je n'ais pas encore créer 20 JPanel...

  6. #6
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Tu sembles avoir mal saisi les concepts liés au Graphics. En effet un objet Graphics est un objet représentant le contexte graphique d'une application à un instant T. En dehors de cet instant, il n'a plus aucune existence, et un autre Graphics prendra le relais.

    Au lieu d'utiliser n JPanel pour ça, je te propose une autre approche bien plus propre. En effet, tu n'as guère besoin d'avoir à disposition tous ces panels, juste d'objet sachant se dessiner sur des Graphics.

    Crée une interface définissant un objet dessinable.

    Cet objet proposerait une méthode draw qui prendrait en paramètre un objet de type Graphics.

    Puis tu crées différentes implémentations de ton interface, une pour dessiner des carré, une pour dessiner des cercles, une autre pour dessiner des images et ainsi de suite.

    Par exemple:

    L'interface:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    package draw;
     
    import java.awt.Graphics;
     
    public interface Drawable {
     
        public void draw(Graphics g);
    }
    Une classe abstraite ajoutant la notion d'emplacement
    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
    package draw;
     
    import java.awt.Point;
     
    public abstract class LocationedShape implements Drawable {
        private Point location;
     
        public LocationedShape(Point point) {
            this.location = point;
        }
     
        public void setLocation(Point location) {
            this.location = location;
        }
     
        public Point getLocation() {
            return location;
        }
     
    }
    Et classe abstraite héritant de la précédente ajoutant la notion de couleur.

    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
    package draw;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.geom.Rectangle2D;
     
    public abstract class ColoredShape extends LocationedShape {
     
     
        private Color backgroundColor=Color.white;
        private Color borderColor=Color.black;
        private Color foregroundColor=Color.black;
     
     
     
        /**
         * @param point
         * @param backgroundColor
         * @param borderColor
         * @param foregroundColor
         */
        public ColoredShape(Point point, Color backgroundColor, Color borderColor,
                Color foregroundColor) {
            super(point);
            this.backgroundColor = backgroundColor;
            this.borderColor = borderColor;
            this.foregroundColor = foregroundColor;
        }
     
        public void setBorderColor(Color borderColor) {
            this.borderColor = borderColor;
        }
     
        public void setBackgroundColor(Color backgroundColor) {
            this.backgroundColor = backgroundColor;
        }
     
        public void setForegroundColor(Color foregroundColor) {
            this.foregroundColor = foregroundColor;
        }
     
        /**
         * @return the backgroundColor
         */
        public Color getBackgroundColor() {
            return backgroundColor;
        }
     
        /**
         * @return the borderColor
         */
        public Color getBorderColor() {
            return borderColor;
        }
     
        /**
         * @return the foregroundColor
         */
        public Color getForegroundColor() {
            return foregroundColor;
        }
     
     
     
    }
    Deux implémentations (une pour un rectangle, une pour un cercle)

    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
    package draw;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.geom.Ellipse2D;
     
    public class Circle extends ColoredShape {
     
     
        private double radius;
     
     
     
        /**
         * @param point
         * @param backgroundColor
         * @param borderColor
         * @param foregroundColor
         * @param radius
         */
        public Circle(Point point, Color backgroundColor, Color borderColor,
                Color foregroundColor, double radius) {
            super(point, backgroundColor, borderColor, foregroundColor);
            this.radius = radius;
        }
     
     
     
        /**
         * @return the radius
         */
        public double getRadius() {
            return radius;
        }
     
     
     
     
     
        /**
         * @param radius the radius to set
         */
        public void setRadius(double radius) {
            this.radius = radius;
        }
     
     
     
     
     
        @Override
        public void draw(Graphics g) {
            Ellipse2D circle = new Ellipse2D.Double(getLocation().getX(), getLocation().getY(), radius, radius);
            //On crée un contexte graphique temporaire  afin de ne pas imacter le contexte initial
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setPaint(getBackgroundColor());
            g2d.fill(circle);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setPaint(getBorderColor());
            g2d.draw(circle);
            //On finalise le contexte graphique temporaire
            g2d.dispose();
        }
     
    }
    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
    package draw;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.geom.Rectangle2D;
     
    public class Rectangle extends ColoredShape {
     
     
        private double width;
        private double height;
     
     
     
        /**
         * @param point
         * @param backgroundColor
         * @param borderColor
         * @param foregroundColor
         * @param width
         * @param height
         */
        public Rectangle(Point point, Color backgroundColor, Color borderColor,
                Color foregroundColor, int width, int height) {
            super(point, backgroundColor, borderColor, foregroundColor);
            this.width = width;
            this.height = height;
        }
     
     
     
     
        /**
         * @return the width
         */
        public double getWidth() {
            return width;
        }
     
     
     
     
        /**
         * @param width the width to set
         */
        public void setWidth(double width) {
            this.width = width;
        }
     
     
     
     
        /**
         * @return the height
         */
        public double getHeight() {
            return height;
        }
     
     
     
     
        /**
         * @param height the height to set
         */
        public void setHeight(double height) {
            this.height = height;
        }
     
     
     
     
        @Override
        public void draw(Graphics g) {
            Rectangle2D rec = new Rectangle2D.Double(getLocation().getX(), getLocation().getY(), width, height);
            //On crée un contexte graphique temporaire  afin de ne pas imacter le contexte initial
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setPaint(getBackgroundColor());
            g2d.fill(rec);
            g2d.setPaint(getBorderColor());
            g2d.draw(rec);
            //On finalise le contexte graphique temporaire
            g2d.dispose();
     
        }
     
    }
    Et enfin le JPanel responsable de toute la partie dessin:

    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
    package draw;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.util.ArrayList;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class ShapesPanel extends JPanel{
     
        private ArrayList<Drawable> shapes = new ArrayList<Drawable>();
     
        /**
         * @return the shapes
         */
        public ArrayList<Drawable> getShapes() {
            return shapes;
        }
     
     
     
        /**
         * @param shapes the shapes to set
         */
        public void setShapes(ArrayList<Drawable> shapes) {
            this.shapes = shapes;
        }
     
        /**
         * 
         * @param d The shape to add
         */
        public void addShape(Drawable d) {
            this.shapes.add(d);
        }
     
     
        @Override
        protected void paintComponent(Graphics g) {
            // TODO Auto-generated method stub
            super.paintComponent(g);
            for(Drawable d : shapes) {
                d.draw(g);
            }
        }
     
     
        public static void main(String[] args) {
            Circle c = new Circle(new Point(100, 200), Color.yellow, Color.blue, Color.blue, 150);
            Rectangle rec = new Rectangle(new Point(400, 300), Color.blue.brighter().brighter().brighter(), Color.blue.darker().darker(), Color.blue.darker().darker(), 50, 100);
            ShapesPanel panel = new ShapesPanel();
            panel.addShape(c);
            panel.addShape(rec);
     
            JFrame f = new JFrame();
            f.add(panel);
            f.setSize(800, 600);
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
     
    }
    Bon, ensuite je t'enjoins à étudier l'interface Shape fournie dans java et toutes ses implémentations, mon exemple ci dessusen étant très fortement inspiré...

Discussions similaires

  1. Problème avec Graphics
    Par KeroroLulu dans le forum Interfaces Graphiques en Java
    Réponses: 1
    Dernier message: 10/05/2015, 13h43
  2. Ouverture d'un projet existant : problème XML Graphical Layout
    Par Rappunzell dans le forum Composants graphiques
    Réponses: 2
    Dernier message: 09/11/2011, 15h48
  3. Problème fonction graphics.beginFill
    Par YoshK dans le forum ActionScript 3
    Réponses: 1
    Dernier message: 17/03/2011, 02h46
  4. problème include graphics
    Par dijonette dans le forum Tableaux - Graphiques - Images - Flottants
    Réponses: 12
    Dernier message: 12/03/2008, 14h25

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