Dear Java2D guru,
I'm having problem of display a vertical text in
multiple colors, Please help. Thank you so much.
Below is my source codes:
public class TimeGraphPanel extends JPanel
{
int offsetX = 30;
int offsetY = 30;
int lineSpacing = 40;
String timeString = "Local - Zulu Time";
AttributedString timeAttribText = new
AttributedString(timeString);
AttributedCharacterIterator iter;
AttributedCharacterIterator timeIter;
FontRenderContext frc;
TextLayout textLayout;
Shape shape = null;
public TimeGraphPanel()
{
setSize(400, 200);
Font font = new Font("serif.bold",
Font.PLAIN, 20);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Font font = new Font("serif.bold",
Font.PLAIN, 20);
super.paintComponent(g2);
// AffineTransform defaultTransform =
g2.getTransform();
Dimension dim = getSize();
g2.setPaint(Color.yellow);
g2.fillRect(offsetX, offsetY,
dim.width-offsetX*2, dim.height-offsetY*2);
g2.setPaint(Color.black);
int w = dim.width - 2 *offsetX;
int h = dim.height - 2 * offsetY;
for (int i=1; i < w/lineSpacing; i++)
{ // draw vertical line
g2.drawLine(lineSpacing*i+offsetX,
offsetY, lineSpacing*i+offsetX, dim.height-offsetY);
}
for (int j=1; j<h/lineSpacing; j++)
{ // draw horizontal line
g2.drawLine(offsetX,
lineSpacing*j+offsetY, dim.width-offsetX,
lineSpacing*j+offsetY);
}
timeAttribText.addAttribute(TextAttribute.FONT, font);
timeAttribText.addAttribute(TextAttribute.FOREGROUND,
Color.green, 0, 5); // Local text color
int index = timeString.indexOf("-");
timeAttribText.addAttribute(TextAttribute.FOREGROUND,
Color.cyan, index, index+1);
index = timeString.indexOf("Zulu");
timeAttribText.addAttribute(TextAttribute.FOREGROUND,
Color.blue, index, index+"Zulu".length());
index = timeString.indexOf("Time");
timeAttribText.addAttribute(TextAttribute.FOREGROUND,
Color.cyan, index, index+"Time".length());
AttributedCharacterIterator timeIter =
timeAttribText.getIterator();
frc = new FontRenderContext(new
AffineTransform(), true, true);
textLayout = new TextLayout(timeIter, frc);
shape = textLayout.getOutline(new
AffineTransform());
//
Rectangle2D bounds = textLayout.getBounds();
g2.translate(10,20);
AffineTransform rotation =
AffineTransform.getRotateInstance(Math.PI/2,
bounds.getX() + bounds.getWidth()/2,
bounds.getY() + bounds.getHeight()/2);
Shape baseShape =
rotation.createTransformedShape(shape);
g2.fill(baseShape);
}
}
__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".