KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > welcome > TitlePanel50


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.bluej.welcome;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.awt.Cursor JavaDoc;
26 import java.awt.Dimension JavaDoc;
27 import java.awt.GradientPaint JavaDoc;
28 import java.awt.Graphics JavaDoc;
29 import java.awt.Graphics2D JavaDoc;
30 import java.awt.Image JavaDoc;
31 import java.awt.LayoutManager JavaDoc;
32 import java.awt.Paint JavaDoc;
33 import java.awt.Point JavaDoc;
34 import java.awt.geom.Point2D JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
38 import org.openide.util.Utilities;
39
40 /**
41  * Wecome screen UI, mostly assembled at runtime. It has custom:
42  * <ul>
43  * <li>paint() method to draw background
44  * <li>layout manager to precisely place components on background image
45  * </ul>
46  *
47  * <p>The cod eit full of static constants to avoid expensive
48  * runtime computations (during startup).
49  *
50  * @author Petr Kuzel
51  */

52 public class TitlePanel50 extends javax.swing.JPanel JavaDoc implements Runnable JavaDoc, java.awt.event.ActionListener JavaDoc, java.awt.event.MouseListener JavaDoc, LayoutManager JavaDoc {
53     
54     private final ImageIcon JavaDoc background;
55     private int backgroundWidth = -1;
56     private int backgroundHeight = -1;
57     
58     private static final Color JavaDoc TOP_COLOR = new Color JavaDoc(224,221,209); // measured from gradient.png
59
private static final Color JavaDoc BOT_COLOR = new Color JavaDoc(217,207,174); // measured from gradient.png
60

61     private static final int BUTTONS_X_OFFSET = 380; // taken from measuring background.png
62

63     // it's tiled, values measured from gradient.png
64
private static final int GRADIENT_WIDTH = 53;
65     private static final int GRADIENT_HEIGHT = 89;
66     
67     // background has tranparent edge
68
private static final int TRANSPARENT_EDGE_WIDTH = 15;
69     
70     public TitlePanel50() {
71         background = new ImageIcon JavaDoc(Utilities.loadImage("org/netbeans/bluej/welcome/background.png")); // NOI18N
72
initComponents();
73         
74         ((OvalButton)jButton2).setScale(1.333f);
75         setLayout(this);
76         setOpaque(false);
77     }
78
79     /** Paint custom background. */
80     public void paint(Graphics JavaDoc g) {
81         
82 // long start = System.currentTimeMillis();
83

84         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)g;
85         int xx = getWidth();
86         int yy = getHeight();
87
88         int offset_x = (xx - backgroundWidth())/2;
89         int offset_y = (yy - backgroundHeight())/2;
90                 
91         // gradient background
92
g2d.setColor(TOP_COLOR);
93         g2d.fillRect(0, 0, xx, (yy-GRADIENT_HEIGHT)/2);
94         Image JavaDoc image = Utilities.loadImage("org/netbeans/bluej/welcome/gradient.png"); // NOI18N
95
for (int i = 0; i<xx; i+=GRADIENT_WIDTH) {
96             if (i > (offset_x + TRANSPARENT_EDGE_WIDTH) && i< (offset_x + backgroundWidth() - GRADIENT_WIDTH - TRANSPARENT_EDGE_WIDTH)) {
97                 continue;
98             }
99             g2d.drawImage(image, i, (yy-GRADIENT_HEIGHT)/2, this);
100         }
101         g2d.setColor(BOT_COLOR);
102         g2d.fillRect(0, (yy+GRADIENT_HEIGHT)/2, xx, yy);
103
104         
105         g.drawImage(background.getImage(), offset_x, offset_y, this);
106
107         pc(jButton2, g);
108         pc(jButton3, g);
109         pc(jButton4, g);
110         pc(jLabel3, g);
111         
112 // long end = System.currentTimeMillis();
113
// System.err.println("Paint:" + (end - start) + "ms");
114
}
115     
116     private void pc (JComponent JavaDoc jc, Graphics JavaDoc g) {
117         int x = jc.getX();
118         int y = jc.getY();
119         g.translate (x, y);
120         jc.paint(g);
121         g.translate(-x, -y);
122     }
123     
124     private int backgroundWidth() {
125         if (backgroundWidth == -1) {
126             backgroundWidth = background.getIconWidth();
127         }
128         return backgroundWidth;
129     }
130
131     private int backgroundHeight() {
132         if (backgroundHeight == -1) {
133             backgroundHeight = background.getIconHeight();
134         }
135         return backgroundHeight;
136     }
137     
138     public void addNotify() {
139         super.addNotify();
140         //Get cursor lookup out of the way
141
SwingUtilities.invokeLater(this);
142     }
143     
144     public void run() {
145         //XXX better to do this on a timer, but I can't override
146
//actionPerformed() and I don't want to create another class
147
Cursor JavaDoc cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
148         jLabel3.setCursor(cur);
149         jButton2.setCursor(cur);
150         jButton3.setCursor(cur);
151         jButton4.setCursor(cur);
152     }
153     /**
154      * Layout driven by background image.
155      *
156      * There is a horizontal line and two button groups:
157      * <ul>
158      * <li> two big buttons above the line
159      * <li> four small buttons under the line
160      * </ul>
161      * The line position is constant and buttons
162      * should expand out it. There are following boxes:
163      *
164         <pre>
165
166                                       1
167
168                                  222222
169                 333333 222222
170             ---------------------------------
171                                  444444
172                                  444444
173                                  444444
174                 55 444444
175
176
177
178           1 is for Java logo: 44x26
179           2s are for big buttons: 22x200
180           3 is for product logo: 35x160
181           4s are small buttons: 18x200
182           5 is for Sun logo: 31x71
183           --- stands for background golden "base" line
184         </pre>
185      */

186 // private class TitleLayout implements LayoutManager {
187
public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
188         }
189
190         public void removeLayoutComponent(Component JavaDoc comp) {
191         }
192
193         public Dimension JavaDoc preferredLayoutSize(Container JavaDoc parent) {
194             return new Dimension JavaDoc(backgroundWidth(), backgroundHeight());
195         }
196
197         public Dimension JavaDoc minimumLayoutSize(Container JavaDoc parent) {
198             return preferredLayoutSize(parent);
199         }
200
201         public void layoutContainer(Container JavaDoc parent) {
202             
203             int offset_x = (parent.getSize().width - backgroundWidth())/2;
204             int offset_y = (parent.getSize().height - backgroundHeight())/2;
205             
206             int base_x = offset_x + BUTTONS_X_OFFSET;
207             int base_y = offset_y + 287; // taken from background.png
208
int SMALL_SPACE = 5;
209             int NB_LOGO_ALIGN = 4;
210             int BOT_GROUP_ALIGN = 16; // 12 is real align with Sun logo by Leos said that 16 is optical align
211
int TOP_BUTTON_HEIGHT = 22;
212             int BOT_BUTTON_HEIGHT = 18;
213             
214             int y = base_y;
215             int w = maxWidth();
216             
217             y -= TOP_BUTTON_HEIGHT + SMALL_SPACE + NB_LOGO_ALIGN;
218             jButton2.setLocation(base_x, y);
219             resize(jButton2, w, TOP_BUTTON_HEIGHT);
220             
221 // y -= TOP_BUTTON_HEIGHT + SMALL_SPACE;
222
// jButton1.setLocation(base_x, y);
223
// resize(jButton1, w, TOP_BUTTON_HEIGHT);
224

225             // horizontal line = base_y
226

227             y = base_y + SMALL_SPACE + BOT_GROUP_ALIGN;
228             jButton3.setLocation(base_x, y);
229             resize(jButton3, w, BOT_BUTTON_HEIGHT);
230             
231             y += BOT_BUTTON_HEIGHT + SMALL_SPACE;
232             jButton4.setLocation(base_x, y);
233             resize(jButton4, w, BOT_BUTTON_HEIGHT);
234                                     
235             // NB logo
236
jLabel3.setSize(160, 35); // measured image size
237
jLabel3.setLocation(50 + offset_x, 246 + offset_y);
238             
239         }
240
241         private void resize(JComponent JavaDoc target, int width, int height) {
242             target.setSize(width, height);
243         }
244         
245         private int maxWidth() {
246             return backgroundWidth() - BUTTONS_X_OFFSET - TRANSPARENT_EDGE_WIDTH; // 15pixels margin taken from measuring background.png
247
}
248         
249 // }
250

251     
252     /** This method is called from within the constructor to
253      * initialize the form.
254      * WARNING: Do NOT modify this code. The content of this method is
255      * always regenerated by the Form Editor.
256      */

257     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
258
private void initComponents() {
259         jButton2 = new OvalButton();
260         jButton3 = new OvalButton();
261         jButton4 = new OvalButton();
262         jLabel3 = new javax.swing.JLabel JavaDoc();
263
264         org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(TitlePanel50.class, "BK0002"));
265         jButton2.setFocusable(false);
266         jButton2.addActionListener(this);
267
268         add(jButton2);
269
270         org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(TitlePanel50.class, "BK0003"));
271         jButton3.setFocusable(false);
272         jButton3.addActionListener(this);
273
274         add(jButton3);
275
276         org.openide.awt.Mnemonics.setLocalizedText(jButton4, org.openide.util.NbBundle.getMessage(TitlePanel50.class, "BK0004"));
277         jButton4.setFocusable(false);
278         jButton4.addActionListener(this);
279
280         add(jButton4);
281
282         jLabel3.addMouseListener(this);
283
284         add(jLabel3);
285
286     }
287
288     // Code for dispatching events from components to event handlers.
289

290     public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
291         if (evt.getSource() == jButton2) {
292             TitlePanel50.this.jButton2ActionPerformed(evt);
293         }
294         else if (evt.getSource() == jButton3) {
295             TitlePanel50.this.jButton3ActionPerformed(evt);
296         }
297         else if (evt.getSource() == jButton4) {
298             TitlePanel50.this.jButton4ActionPerformed(evt);
299         }
300     }
301
302     public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
303         if (evt.getSource() == jLabel3) {
304             TitlePanel50.this.jLabel3MouseClicked(evt);
305         }
306     }
307
308     public void mouseEntered(java.awt.event.MouseEvent JavaDoc evt) {
309     }
310
311     public void mouseExited(java.awt.event.MouseEvent JavaDoc evt) {
312     }
313
314     public void mousePressed(java.awt.event.MouseEvent JavaDoc evt) {
315     }
316
317     public void mouseReleased(java.awt.event.MouseEvent JavaDoc evt) {
318     }// </editor-fold>//GEN-END:initComponents
319

320     private void jLabel3MouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_jLabel3MouseClicked
321
BusinessLogic.perform(103, this);
322     }//GEN-LAST:event_jLabel3MouseClicked
323

324     private void jButton4ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton4ActionPerformed
325
BusinessLogic.perform(4, this);
326     }//GEN-LAST:event_jButton4ActionPerformed
327

328     private void jButton3ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton3ActionPerformed
329
BusinessLogic.perform(3, this);
330     }//GEN-LAST:event_jButton3ActionPerformed
331

332     private void jButton2ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton2ActionPerformed
333
BusinessLogic.perform(2, this);
334     }//GEN-LAST:event_jButton2ActionPerformed
335

336     
337     // Variables declaration - do not modify//GEN-BEGIN:variables
338
private javax.swing.JButton JavaDoc jButton2;
339     private javax.swing.JButton JavaDoc jButton3;
340     private javax.swing.JButton JavaDoc jButton4;
341     private javax.swing.JLabel JavaDoc jLabel3;
342     // End of variables declaration//GEN-END:variables
343

344 }
345
Popular Tags