Bonjour,
J'ai suivi ce très bon tuto pour m'initier au dessin avec java : http://duj.developpez.com/tutoriels/java/dessin/intro/
J'ai réussi à tracer mes rectangles comme vous pouvez le voir sur la photo : Voir la photo ci-joint.
Ces rectangles sont de dimension fixe. Je cherche à ce q'ils suivent dynamiquement la résolution de ma fenêtre (modification de leur taille au redimensionnement de la fenêtre).
Voici tout le code de mon projet :
MonMain1.java
FormDrawable.java
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 package b_deuxiemeRectangleJava; import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class MonMain1 { /** * @param args */ public static void main(String[] args) { JCanvas jc = new JCanvas(); jc.setBackground(Color.WHITE); jc.setPreferredSize(new Dimension(400,450)); Dimension dim = new Dimension(100,100); //***********************************XML********************************* /* * Etape 1 : récupération d'une instance de la classe "DocumentBuilderFactory" */ final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { /* * Etape 2 : création d'un parseur */ final DocumentBuilder builder = factory.newDocumentBuilder(); /* * Etape 3 : création d'un Document */ final Document document= builder.parse(new File("b_deuxiemeRectangleJava/repertoire.xml")); //Affiche du prologue System.out.println("*************PROLOGUE************"); System.out.println("version : " + document.getXmlVersion()); System.out.println("encodage : " + document.getXmlEncoding()); System.out.println("standalone : " + document.getXmlStandalone()); /* * Etape 4 : récupération de l'Element racine */ final Element racine = document.getDocumentElement(); //Affichage de l'élément racine System.out.println("\n*************RACINE************"); System.out.println(racine.getNodeName()); /* * Etape 5 : récupération des personnes */ final NodeList racineNoeuds = racine.getChildNodes(); final int nbRacineNoeuds = racineNoeuds.getLength(); for (int i = 0; i<nbRacineNoeuds; i++) { if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) { final Element graphique = (Element) racineNoeuds.item(i); //Affichage d'une personne System.out.println("\n*************GRAPHIQUE************"); System.out.println("type : " + graphique.getAttribute("type")); /* * Etape 6 : récupération du nom, du height, du width */ final Element nom = (Element) graphique.getElementsByTagName("nom").item(0); final Element height = (Element) graphique.getElementsByTagName("height").item(0); final Element width = (Element) graphique.getElementsByTagName("width").item(0); //Affichage du nom, du height, du width System.out.println("nom : " + nom.getTextContent()); System.out.println("height: " + height.getTextContent()); System.out.println("width: " + width.getTextContent()); //Création des graphiques int hauteur = Integer.parseInt(height.getTextContent()); int largueur = Integer.parseInt(width.getTextContent()); jc.addDrawable(new RectangleDrawable(Color.BLACK,new Point(hauteur,largueur),dim)); } } } catch (final ParserConfigurationException e) { e.printStackTrace(); } catch (final SAXException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } //******************************************************************************** GUIHelper.showOnFrame(jc,"Mes graphiques"); } }
GUIHelper.java
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 package b_deuxiemeRectangleJava; import java.awt.*; public abstract class FormDrawable implements IDrawable { protected Rectangle rect ; protected Color color; public FormDrawable(Color color, Point pos, Dimension dim){ this.color=color; this.rect = new Rectangle(pos,dim); } public abstract void draw(Graphics g) ; public Rectangle getRectangle(){ return (Rectangle) rect.clone(); } }
jCanvas.java
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 package b_deuxiemeRectangleJava; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JComponent; import javax.swing.JFrame; public class GUIHelper { public static void showOnFrame(JComponent component, String frameName) { JFrame frame = new JFrame(frameName); WindowAdapter wa = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame.addWindowListener(wa); frame.getContentPane().add(component); frame.pack(); frame.setVisible(true); } }
IDrawable.java
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 package b_deuxiemeRectangleJava; import java.awt.Color; import java.awt.Graphics; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.swing.JPanel; public class JCanvas extends JPanel { private List drawables = new LinkedList(); public void paint(Graphics g) { super.paint(g); for (Iterator iter = drawables.iterator(); iter.hasNext();) { IDrawable d = (IDrawable) iter.next(); d.draw(g); } } public void addDrawable(IDrawable d) { drawables.add(d); repaint(); } public void removeDrawable(IDrawable d) { drawables.remove(d); repaint(); } public void clear() { drawables.clear(); repaint(); } }
RectangleDrawable.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 package b_deuxiemeRectangleJava; import java.awt.Graphics; import java.awt.Rectangle; public interface IDrawable { public void draw(Graphics g); public Rectangle getRectangle(); }
Le fichier Xml où je viens piocher les dimensions de mes rectangles
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 package b_deuxiemeRectangleJava; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; public class RectangleDrawable extends FormDrawable { public RectangleDrawable(Color color, Point pos, Dimension dim) { super(color, pos, dim); } public void draw(Graphics g) { Color c = g.getColor(); g.setColor(color); //g.fillRect(rect.x,rect.y,rect.height,rect.width); //Rectangle plein g.drawRect(rect.x,rect.y,rect.height,rect.width); g.setColor(c); } }
Merci d'avance pour votre aide !
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 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <repertoire> <graphique type="linéaire"> <nom>Graph1</nom> <height>10</height> <width>10</width> </graphique> <graphique type="barchart"> <nom>Graph2</nom> <height>150</height> <width>10</width> </graphique> <graphique type="piechart"> <nom>Graph3</nom> <height>290</height> <width>10</width> </graphique> <graphique type="linéaire"> <nom>Graph4</nom> <height>10</height> <width>150</width> </graphique> <graphique type="barchart"> <nom>Graph5</nom> <height>150</height> <width>150</width> </graphique> <graphique type="piechart"> <nom>Graph6</nom> <height>290</height> <width>150</width> </graphique> <graphique type="linéaire"> <nom>Graph7</nom> <height>10</height> <width>300</width> </graphique> <graphique type="barchart"> <nom>Graph8</nom> <height>150</height> <width>300</width> </graphique> <graphique type="piechart"> <nom>Graph9</nom> <height>290</height> <width>300</width> </graphique> </repertoire>
Partager