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 :

[Débutant]Jpanel et dessin de composants


Sujet :

2D Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 4
    Par défaut [Débutant]Jpanel et dessin de composants
    Bonjour,

    après avoir lu pas mal de tutoriaux et testé des tonnes de solutions, je viens chercher de l'aide car il y a un truc bête que je n'ai pas dû comprendre dans toutes ces docs...

    Mon problème : je souhaite dessiner des objets graphiques (des points par exemple) sur un Jpanel (qui à terme comportera une image en arrière plan, mais chaque chose en son temps).

    Pour le moment j'en suis réduit à tester des bouts de code pour comprendre ce qu'il se passe...

    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
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.geom.Ellipse2D;
    import java.util.ArrayList;
     
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class testVisu {
     
    	private ArrayList<GraphPoint> graphPoints = new ArrayList<GraphPoint>();  //  @jve:decl-index=0:
     
    	private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="288,144"
     
    	private JPanel jContentPane = null;
     
    	private JPanel jPanel = null; // @jve:decl-index=0:visual-constraint="203,157"
     
    	/**
             * This method initializes jFrame
             * 
             * @return javax.swing.JFrame
             */
    	private JFrame getJFrame() {
    		if (jFrame == null) {
    			jFrame = new JFrame();
    			jFrame.setSize(new Dimension(400, 400));
    			jFrame.setContentPane(getJContentPane());
     
    			jFrame.setVisible(true);
     
    			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
    				public void windowClosing(java.awt.event.WindowEvent e) {
    					jFrame.dispose();
    				}
    			});
    		}
    		return jFrame;
    	}
     
    	/**
             * This method initializes jContentPane
             * 
             * @return javax.swing.JPanel
             */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
     
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new FlowLayout());
    			jContentPane.setSize(new Dimension(350, 350));
    			jContentPane.add(getJPanel());
     
    		}
    		return jContentPane;
    	}
     
    	/**
             * This method initializes jPanel
             * 
             * @return javax.swing.JPanel
             */
    	private JPanel getJPanel() {
    		if (jPanel == null) {
    			jPanel = new JPanel();
    			 jPanel.setBackground(Color.yellow);
    			 jPanel.setLayout(new FlowLayout());
    			jPanel.setPreferredSize(new Dimension(300, 300));
     
    		}
    		return jPanel;
    	}
     
     
     
    	private void addPoint(int _x, int _y, Color color) {
    		GraphPoint pt1 = new GraphPoint(_x, _y, color);
    		graphPoints.add(pt1);
    		getJPanel().add(pt1);
    	}
     
    	private class GraphPoint extends JComponent {
     
    		Ellipse2D.Double point = null;
     
    		Color color = null;
     
    		public GraphPoint(int _x, int _y, Color _color) {
     
    			color = _color;
    			point = new Ellipse2D.Double(_x, _y, 20, 20);
     
    		}
     
    		public void move(int _x, int _y) {
     
    			point.x = _x;
    			point.y = _y;
     
    		}
     
     
     
    		protected void paintComponent(Graphics g) {
    			Graphics2D g2d = (Graphics2D) g;
     
     
    			g2d.setColor(color);
    			g2d.fill(point);
    		}
     
    	}
    	public static void main(String[] args) {
    		testVisu app = new testVisu();
    		app.getJFrame();
    //fonctionne
    //		 Ellipse2D.Double point = new Ellipse2D.Double(150, 150, 20, 20);
    //		 Graphics2D g2d = (Graphics2D)app.getJPanel().getGraphics().create();
    //		 g2d.setColor(Color.BLUE);
    //		 g2d.fill(point);
     
    		//ajouter un GraphPoint
    		app.addPoint(100, 100, Color.RED);
     
    		JButton button =new JButton();
    		button.setPreferredSize(new Dimension(100,20));
    		button.setText("toto");
    		app.jPanel.add(button);
     
    //		app.jPanel.repaint();
    //		DriverPoint pt2 = app.new DriverPoint(200, 100, Color.blue);
    //		app.getJPanel().add(pt2);
    		// app.getJPanel().repaint();
    	}
    }
    ce code affiche un panel, ajoute un cercle rouge rempli et un bouton.

    J'ai 2 questions :
    - pourquoi le cercle rouge n'apparait pas (en utilisant l'objet Graphpoint) ?
    - pourquoi le bouton n'apparait que quand la fenêtre est redimensionnée (ou réduite) ? (le composant est dessiné correctement dans ces 2 cas, mais pourquoi pas dès le départ)

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 4
    Par défaut
    j'ai refait des essais de code... j'arrive à afficher un point dans la frame, mais pas dans un panel qui se trouve dans la frame...

    le code de mon composant point :

    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
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
     
    import javax.swing.JComponent;
     
    public class GraphPoint extends JComponent {
     
    		Ellipse2D.Double point = null;
     
    		Color color = null;
     
    		public GraphPoint(int _x, int _y, Color _color) {
     
    			color = _color;
    			point = new Ellipse2D.Double(_x, _y, 20, 20);
     
    		}
     
    		public void move(int _x, int _y) {
     
    			point.x = _x;
    			point.y = _y;
     
    		}	
     
    		protected void paintComponent(Graphics g) {
    			Graphics2D g2d = (Graphics2D) g;
     
    			g2d.setColor(color);
    			g2d.fill(point);
    		}
     
    	}
    et mon code pour mettre un point dans le panel :



    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
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.util.ArrayList;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class CopyOftestVisu {
     
    	private ArrayList<GraphPoint> graphPoints = new ArrayList<GraphPoint>(); // @jve:decl-index=0:
     
    	private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="288,144"
     
    	private JPanel jContentPane = null;
     
    	private JPanel jPanel = null; // @jve:decl-index=0:visual-constraint="203,157"
     
    	/**
             * This method initializes jFrame
             * 
             * @return javax.swing.JFrame
             */
    	private JFrame getJFrame() {
    		if (jFrame == null) {
    			jFrame = new JFrame();
    			jFrame.setSize(new Dimension(400, 400));
     
    			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
    				public void windowClosing(java.awt.event.WindowEvent e) {
    					jFrame.dispose();
    				}
    			});
    		}
    		return jFrame;
    	}
     
    	private void addPoint(int _x, int _y, Color color) {
    		GraphPoint pt1 = new GraphPoint(_x, _y, color);
    		graphPoints.add(pt1);
    		jFrame.getContentPane().add(pt1, BorderLayout.CENTER);
     
    	}
     
    	public static void main(String[] args) {
    		CopyOftestVisu app = new CopyOftestVisu();
    		//frame
    		app.getJFrame();
     
    		//panel
    		JPanel panel = new JPanel();
    		panel.setLayout(new FlowLayout());
    		panel.setBackground(Color.BLUE);
    		app.getJFrame().add(panel);
     
    		//ajout d'un point au panel : ne fonctionne pas
    		GraphPoint pt1 = new GraphPoint(100, 100, Color.RED);
    		app.graphPoints.add(pt1);
    		panel.add(pt1);
     
    		//ajout d'un point à la frame : fonctionne
    //		app.addPoint(150, 150, Color.RED);
     
    		app.getJFrame().setVisible(true);
     
     
     
    	}
    }
    quel est le problème avec le panel ? Problème de layout ?

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 4
    Par défaut
    j'ai vraiment l'impression de poser des questions débiles...

    Pour afficher un objet qui hérite de Jcomponent, il suffit bien de définir sa méthode paintcomponent() et ensuite de juste faire un add(component) sur son containeur ????

    pourquoi ça passe dans la jframe, et pas dans le jpanel ?

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 4
    Par défaut
    apparemment j'ai résolu mon problème :

    il faut que le composant définisse une méthode paint, la paintcomponent ne semble pas suffire

Discussions similaires

  1. [Swing]Comment dessiner ce composant complexe?
    Par jlassiramzy dans le forum AWT/Swing
    Réponses: 18
    Dernier message: 25/04/2007, 00h56
  2. Réponses: 7
    Dernier message: 04/06/2006, 17h00
  3. [Delphi 6] dessiner sur composant QuickRep
    Par KrusK dans le forum Composants VCL
    Réponses: 2
    Dernier message: 18/06/2005, 00h48
  4. [Débutant] JPanel dans JScrollPane
    Par Nicolas_75 dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 06/04/2005, 09h39
  5. [Dessin JPanel] Generer dessin pas a pas
    Par -=Spoon=- dans le forum 2D
    Réponses: 2
    Dernier message: 03/12/2004, 22h41

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