Esempio n. 1
0
 protected float drawBoxedString(Graphics2D g2, String s, Color c1, Color c2, double x) {
   // Calculate the width of the string.
   FontRenderContext frc = g2.getFontRenderContext();
   TextLayout subLayout = new TextLayout(s, mFont, frc);
   float advance = subLayout.getAdvance();
   // Fill the background rectangle with a gradient.
   GradientPaint gradient = new GradientPaint((float) x, 0, c1, (float) (x + advance), 0, c2);
   g2.setPaint(gradient);
   Rectangle2D bounds = mLayout.getBounds();
   Rectangle2D back = new Rectangle2D.Double(x, 0, advance, bounds.getHeight());
   g2.fill(back);
   // Draw the string over the gradient rectangle.
   g2.setPaint(Color.white);
   g2.setFont(mFont);
   g2.drawString(s, (float) x, (float) -bounds.getY());
   return advance;
 }
Esempio n. 2
0
 protected void drawText(Graphics2D g2) {
   // Find the bounds of the entire string.
   FontRenderContext frc = g2.getFontRenderContext();
   mLayout = new TextLayout(mMessage, mFont, frc);
   // Find the dimensions of this component.
   int width = getSize().width;
   int height = getSize().height;
   // Place the first full string, horizontally centered,
   //   at the bottom of the component.
   Rectangle2D bounds = mLayout.getBounds();
   double x = (width - bounds.getWidth()) / 2;
   double y = height - bounds.getHeight();
   drawString(g2, x, y, 0);
   // Now draw a second version, anchored to the right side
   //   of the component and rotated by -PI / 2.
   drawString(g2, width - bounds.getHeight(), y, -Math.PI / 2);
 }