KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjVerticalTextIcon


1 package com.genimen.djeneric.ui;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.awt.FontMetrics JavaDoc;
7 import java.awt.Graphics JavaDoc;
8 import java.awt.Graphics2D JavaDoc;
9 import java.awt.geom.AffineTransform JavaDoc;
10
11 import javax.swing.Icon JavaDoc;
12 import javax.swing.JLabel JavaDoc;
13 import javax.swing.SwingConstants JavaDoc;
14 import javax.swing.SwingUtilities JavaDoc;
15 import javax.swing.UIManager JavaDoc;
16
17 // Based on code of Santhosh Kumar,
18
// see http://www.jroller.com/comments/santhosh/Weblog/adobe_like_tabbedpane_in_swing
19

20 public class DjVerticalTextIcon implements Icon JavaDoc, SwingConstants JavaDoc
21 {
22   //USAGE:
23
//public static void addTab(JTabbedPane tabPane, String text, Component comp)
24
//{
25
// int tabPlacement = tabPane.getTabPlacement();
26
// switch (tabPlacement)
27
// {
28
// case JTabbedPane.LEFT :
29
// case JTabbedPane.RIGHT :
30
// tabPane.addTab(null, new VerticalTextIcon(text, tabPlacement == JTabbedPane.RIGHT), comp);
31
// return;
32
// default :
33
// tabPane.addTab(text, null, comp);
34
// }
35
//}
36
//
37
//public static JTabbedPane createTabbedPane(int tabPlacement)
38
//{
39
// switch (tabPlacement)
40
// {
41
// case JTabbedPane.LEFT :
42
// case JTabbedPane.RIGHT :
43
// Object textIconGap = UIManager.get("TabbedPane.textIconGap");
44
// Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
45
// UIManager.put("TabbedPane.textIconGap", new Integer(1));
46
// UIManager.put("TabbedPane.tabInsets", new Insets(tabInsets.left, tabInsets.top, tabInsets.right,
47
// tabInsets.bottom));
48
// JTabbedPane tabPane = new JTabbedPane(tabPlacement);
49
// UIManager.put("TabbedPane.textIconGap", textIconGap);
50
// UIManager.put("TabbedPane.tabInsets", tabInsets);
51
// return tabPane;
52
// default :
53
// return new JTabbedPane(tabPlacement);
54
// }
55
//}
56

57   private Font JavaDoc font = UIManager.getFont("Label.font");
58   FontMetrics JavaDoc metrics = new JLabel JavaDoc().getFontMetrics(font);
59
60   private String JavaDoc text;
61   private int width, height;
62   private boolean clockwize;
63
64   public DjVerticalTextIcon(String JavaDoc text, boolean clockwize)
65   {
66     this.text = text;
67     width = SwingUtilities.computeStringWidth(metrics, text);
68     height = metrics.getHeight();
69     this.clockwize = clockwize;
70   }
71
72   public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y)
73   {
74     Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
75     Font JavaDoc oldFont = g.getFont();
76     Color JavaDoc oldColor = g.getColor();
77     AffineTransform JavaDoc oldTransform = g2.getTransform();
78
79     g.setFont(font);
80     g.setColor(Color.black);
81     if (clockwize)
82     {
83       g2.translate(x + getIconWidth(), y);
84       g2.rotate(Math.PI / 2);
85     }
86     else
87     {
88       g2.translate(x, y + getIconHeight());
89       g2.rotate(-Math.PI / 2);
90     }
91     g.drawString(text, 0, metrics.getLeading() + metrics.getAscent());
92
93     g.setFont(oldFont);
94     g.setColor(oldColor);
95     g2.setTransform(oldTransform);
96   }
97
98   public int getIconWidth()
99   {
100     return height;
101   }
102
103   public int getIconHeight()
104   {
105     return width;
106   }
107
108 }
Popular Tags