Bonjour à tous (et à toutes).
Je suis en train de créer une vue Eclipse qui doit s'intégrer dans une application RCP sachant qu'on m'a demandé que cette vue (et celle là seule, pas le reste de l'application qui doit rester pareil) soit jolie, avec des png translucides, des dégradés qui bougent et toutes ces sortes de choses ...
Je me suis donc orienté vers Java2D (que j'intègre ensuite avec le pont SWT<->AWT).
Mon problème est que je dois pouvoir faire bouger pleins d'éléments dont des controles comme des boutons, des champs de texte, il doivent pouvoir se superposer etc.
J'ai donc commencé à faire une première ébauche, j'ai fait une classe qui étend JPanel, j'ai redéfini paintComponents au sein duquel je repositionne (voir redimensionne) mes composants.
Le problème est que l'affichage produit des effets très étranges, comme si paintComponent était appelé un grand nombre de fois pour un même composant et ce à chaque redimensionnement de ma fenêtre. La zone graphique de l'objet Graphics2D qui m'est passé à chaque fois à l'air de changer aussi...
Je me retrouver avec des résultats du genre de ceux que j'ai placé en pièce jointe.
Je fournis mon code au passage... (j'ai viré les imports je peux éditer si ils me sont demandés)
Code SwingView.java : 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 public class SwingView extends JPanel { private static final long serialVersionUID = 1L; private BufferedImage img; // notre banniere private double c; // C'est le rapport de coupure de l'hypothenuse. Pour // c=0.5 la césure entre les couleurs du dégradé suivra // une droite de coeff a sécant l'hypothenuse NO->SE au // millieu ... private double a; // Le coefficient de la droite qui représente la césure // entre les deux couleurs, permet donc d'orienter le // dégrader comme on le souhaite ... Pour l'instant il y // a pleins de bugs non gérés suivant l'inclinaison et // les dimensions de la fenêtre qui sont facile mais // long à gerer, je les laisse pour quand j'aurais du // temps (si j'en ai)... private double d; // Afin de pouvoir avoir des dégradés éxotiques (écrasés // en l'occurence) un double "d" est passé en parametre // qui permet de définir combien du dégradé entre c1 et // c2 aura déjà été effectué arrivé à la droite de // césure ... Ceci permet nottement d'avoir un coin // NordOuest tres clair prenant presque toute la page // avec un dégradé tres leger et focaliser la // "dégradation" sur le coin SudEst... private Color darkColor, lightColor;// Les couleurs à employer pour le // dégradé. private Color liseretColor; private BufferedImage icon; private BufferedImage noci; private int gW; private int gH; private int myPanelCount = 6; private NestedEntityModel[] myPanels; private static final int LOGOPANEL = 0; private static final int OGOLPANEL = 1; private static final int PRESENTATIONPANEL = 2; private static final int PERSOPANEL = 3; private static final int FAVORITESPANEL = 4; private static final int ALERTSPANEL = 5; /** * @param args */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite c = new Composite(shell, SWT.EMBEDDED); c.setLayout(new FillLayout()); Frame f = SWT_AWT.new_Frame(c); UIManager.getDefaults().put("Panel.background", new Color(0, 0, 255, 255)); SwingView gr = new SwingView(true); // f.setLayout(null); TODO virer ca gr.setVisible(true); gr.setBounds(1, 1, 1, 1); f.add(gr); f.setVisible(true); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } private void reScanSize(Graphics g) { this.gH = g.getClipBounds().height; this.gW = g.getClipBounds().width; } public SwingView(boolean b) { super(b); this.gW = 0; this.gH = 0; InputStream is = this.getClass().getResourceAsStream("banner.png"); this.img = null; try { this.img = ImageIO.read(is); } catch (Exception e) { e.printStackTrace(); } this.darkColor = new Color(0, 0, 0); this.lightColor = new Color(255, 255, 255); this.liseretColor = new Color(0, 255, 0); try { this.icon = ImageIO.read(this.getClass().getResourceAsStream( "logo.png")); this.noci = ImageIO.read(this.getClass().getResourceAsStream( "ogol.png")); } catch (Exception e) { e.printStackTrace(); } this.a = .2; this.c = .7; this.d = .85; this.myPanels = new NestedEntityModel[this.myPanelCount]; this.myPanels[LOGOPANEL] = new ViewLogoEntity(this); this.myPanels[OGOLPANEL] = new ViewOgolEntity(this); this.myPanels[PRESENTATIONPANEL] = new ViewPresentationEntity(this); this.myPanels[PERSOPANEL] = new ViewPersoEntity(this); this.myPanels[FAVORITESPANEL] = new ViewFavoritesEntity(this); this.myPanels[ALERTSPANEL] = new ViewAlertsEntity(this); for (NestedEntityModel current : this.myPanels) { this.add(current); } } public BufferedImage getIcon() { return icon; } public BufferedImage getNoci() { return noci; } private void reSizeContent(Graphics g) { this.myPanels[LOGOPANEL].setLocation(15, 15); this.myPanels[OGOLPANEL].setLocation(this.gW - this.myPanels[OGOLPANEL].getClaimedSize().width - 15, this.gH - this.myPanels[OGOLPANEL].getClaimedSize().height - 15); this.myPanels[PRESENTATIONPANEL] .setLocation((this.gW - this.myPanels[PRESENTATIONPANEL] .getClaimedSize().width) / 2, (this.gH - this.myPanels[PRESENTATIONPANEL] .getClaimedSize().height) / 2); this.myPanels[PERSOPANEL].setLocation((4 * this.gW / 5) - 5, 30); this.myPanels[FAVORITESPANEL].setLocation((4 * this.gW / 5) + 5, 30 + this.myPanels[PERSOPANEL].getClaimedSize().height + 5); this.myPanels[ALERTSPANEL].setLocation(40, this.gH - this.myPanels[ALERTSPANEL].getClaimedSize().height - 15); for (NestedEntityModel current : this.myPanels) { current.setSize(current.getClaimedSize()); } } @Override protected void paintComponent(Graphics g) { System.out.println("===[REPAINT DE SWING INTRO]==="); Long time = System.currentTimeMillis(); super.paintComponent(g); this.reSizeContent(g); this.reScanSize(g); Graphics2D g2 = (Graphics2D) g; this.paintLeanyGradient(g2, this.lightColor, this.darkColor, this.c, this.a, this.d); g2.setPaint(this.liseretColor); g2.setStroke(new BasicStroke(25.0f)); g2.drawLine(0, 200, g2.getClipBounds().width, 200); g2.drawImage(this.img, 30, this.gH - this.img.getHeight() - 110, this.img.getWidth(), this.img.getHeight(), null); System.out.println("===[/REPAINT DE SWING INTRO(" + (System.currentTimeMillis() - time) + "ms)]==="); } protected void paintLeanyGradient(Graphics g, Color c1, Color c2, double c, double a, double d) { a = -a; Graphics2D g2 = (Graphics2D) g; int x = (int) (g.getClipBounds().width * c); int y = (int) (g.getClipBounds().height * c); int b = (int) (y - a * x); int h = g.getClipBounds().height + 1; int w = g.getClipBounds().width + 1; y = -1; x = (int) ((y - b) / a); Polygon pno = new Polygon(); pno.addPoint(-1, -1); Polygon pse = new Polygon(); pse.addPoint(w, h); if (x > w) { x = w; pno.addPoint(x, -1); y = (int) ((a * x) + b); pno.addPoint(x, y + 1); pse.addPoint(x, y - 1); } else { pno.addPoint(x + 1, y); pse.addPoint(w, y); pse.addPoint(x - 1, y); } x = -1; y = (int) ((a * x) + b); if (y > h) { y = h; x = (int) ((y - b) / a); pno.addPoint(x + 1, y); pno.addPoint(-1, y); pse.addPoint(x - 1, y); } else { pno.addPoint(x, y + 1); pse.addPoint(x, y - 1); pse.addPoint(x, h); } x = (int) -((b * a) / (1 + (a * a))); y = (int) ((a * x) + b); Color c3 = new Color( (int) ((c1.getRed() * d) + (c2.getRed() * (1 - d))), (int) ((c1 .getGreen() * d) + (c2.getGreen() * (1 - d))), (int) ((c1.getBlue() * d) + (c2.getBlue() * (1 - d)))); GradientPaint gradient = new GradientPaint(-1, -1, c1, x + 1, y + 1, c3, false); g2.setPaint(gradient); g2.fill(pno); int b2 = (int) (h + (w / a)); x = (int) -(((b * a) - (a * b2)) / (1 + (a * a))); y = (int) ((a * x) + b); gradient = new GradientPaint(x - 1, y - 1, c3, w, h, c2, false); g2.setPaint(gradient); g2.fill(pse); } }
Code BridgedIntro.java : 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 public class BridgedIntro extends Composite implements ControlListener { private Frame f; private SwingView gr; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); BridgedIntro c = new BridgedIntro(shell, SWT.BORDER); shell.setText("Test de "+c.getClass()); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } public BridgedIntro(Composite parent, int style) { super(parent, style | SWT.EMBEDDED); this.setLayout(new GridLayout()); this.setBounds(parent.getClientArea()); this.f = SWT_AWT.new_Frame(this); UIManager.getDefaults().put("Panel.background", new Color(0, 0, 255, 10)); this.gr = new SwingView(true); this.f.setLayout(null); f.setVisible(true); gr.setVisible(true); System.out.println(this.getSize()); System.out.println(this.getLocation()); Rectangle r = new Rectangle(parent.getBounds().x, parent .getBounds().y, parent.getBounds().width, parent .getBounds().height); System.out.println(r); this.f.setBounds(r); this.gr.setBounds(r); this.gr.setVisible(true); parent.addControlListener(this); this.f.add(this.gr); this.f.setVisible(true); } @Override public void controlMoved(ControlEvent e) { System.out.println("########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################"); Rectangle r = new Rectangle(this.getParent().getClientArea().x, this .getParent().getClientArea().y, this.getParent().getClientArea().width, this .getParent().getClientArea().height); System.out.println(r); this.f.setBounds(r); this.gr.setBounds(r); System.out.println("########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################"); } @Override public void controlResized(ControlEvent e) { System.out.println("************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************"); Rectangle r = new Rectangle(this.getParent().getClientArea().x, this .getParent().getClientArea().y, this.getParent().getClientArea().width, this .getParent().getClientArea().height); System.out.println(r); this.f.setBounds(r); this.gr.setBounds(r); System.out.println("************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************"); } }
Code NestedEntityModel.java : 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 public class NestedEntityModel extends JComponent { protected int gW; protected int gH; private static final long serialVersionUID = 1L; protected SwingView intro; public NestedEntityModel(SwingView parent) { this.intro = parent; } public Dimension getClaimedSize() { return new Dimension(0, 0); }; protected void reScanSize(Graphics g) { this.gH = g.getClipBounds().height; this.gW = g.getClipBounds().width; } @Override protected void paintComponent(Graphics g) { System.out.println("===[CUSTOM REPAINT DE " + this.getClass().getSimpleName() + "]==="); long time = System.currentTimeMillis(); this.reSizeContent(g); super.paintComponents(g); this.reScanSize(g); this.rePaintContent(g); System.out.println("===[/CUSTOM REPAINT DE " + this.getClass().getSimpleName() + "(" + (System.currentTimeMillis() - time) + "ms)]==="); } protected void reSizeContent(Graphics g) { } protected void rePaintContent(Graphics g) { } }
Code ViewAlertsEntity.java : 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 public class ViewAlertsEntity extends NestedEntityModel { private static final long serialVersionUID = 1L; public ViewAlertsEntity(SwingView parent) { super(parent); } @Override protected void rePaintContent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.rePaintContent(g2); g2.setPaint(Color.RED); g2.drawRect(0, 0, this.gW, this.gH); } @Override public Dimension getClaimedSize() { return new Dimension(750,50); } @Override protected void reSizeContent(Graphics g) { super.reSizeContent(g); } }
Code ViewFavoritesEntity.java : 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 public class ViewFavoritesEntity extends NestedEntityModel { private static final long serialVersionUID = 1L; public ViewFavoritesEntity(SwingView parent) { super(parent); } @Override protected void rePaintContent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.rePaintContent(g2); g2.setPaint(Color.BLUE); g2.drawRect(0, 0, this.gW, this.gH); } @Override public Dimension getClaimedSize() { return new Dimension(150,400); } @Override protected void reSizeContent(Graphics g) { super.reSizeContent(g); } }
Code ViewLogoEntity.java : 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 public class ViewLogoEntity extends NestedEntityModel { private static final long serialVersionUID = 1L; private BufferedImage logo; public ViewLogoEntity(SwingView parent) { super(parent); this.logo = parent.getIcon(); } @Override protected void rePaintContent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.rePaintContent(g2); g2.drawImage(this.logo, 0, 0, this.logo.getWidth(), this.logo.getHeight(), null); } @Override public Dimension getClaimedSize() { return new Dimension(this.logo.getWidth(), this.logo.getHeight()); } @Override protected void reSizeContent(Graphics g) { super.reSizeContent(g); } }
Code ViewOgolEntity.java : 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 public class ViewOgolEntity extends NestedEntityModel { private BufferedImage ogol; private static final long serialVersionUID = 1L; public ViewOgolEntity(SwingView parent) { super(parent); this.ogol = parent.getNoci(); } @Override protected void rePaintContent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.rePaintContent(g2); g2.drawImage(this.ogol, 0, 0, this.ogol.getWidth(), this.ogol .getHeight(), null); } @Override public Dimension getClaimedSize() { return new Dimension(this.ogol.getWidth(), this.ogol.getHeight()); } @Override protected void reSizeContent(Graphics g) { super.reSizeContent(g); } }
Code ViewPersoEntity.java : 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 public class ViewPersoEntity extends NestedEntityModel { private static final long serialVersionUID = 1L; public ViewPersoEntity(SwingView parent) { super(parent); } @Override protected void rePaintContent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.rePaintContent(g2); g2.setPaint(Color.GREEN); g2.drawRect(0, 0, this.gW, this.gH); } @Override public Dimension getClaimedSize() { return new Dimension(220,65); } @Override protected void reSizeContent(Graphics g) { super.reSizeContent(g); } }
Code ViewPresentationEntity.java : 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 public class ViewPresentationEntity extends NestedEntityModel { private static final long serialVersionUID = 1L; public ViewPresentationEntity(SwingView parent) { super(parent); } @Override protected void rePaintContent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.rePaintContent(g2); g2.setPaint(Color.PINK); g2.drawRect(0, 0, this.gW, this.gH); } @Override public Dimension getClaimedSize() { return new Dimension(500,500); } @Override protected void reSizeContent(Graphics g) { super.reSizeContent(g); } }
Et le répertoire contenant mes classes contient en plus les trois fichiers png suivant (que j'ai aussi fournit en pièce jointe, ça me semble moins utile mais autant faire les choses jusqu'au bout !):
banner.png
logo.png
ogol.png
Donc voila toutes aides, recommandations, conseils, ou miracles seront apprécies ...







Répondre avec citation
Partager