The java.awt.font.TextLayout.getBounds() method returns a box that is shifted upwards compared to the box that is returned by Sun's official JRE.
Sample code: package swingbug;
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
public class SwingBug extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) { new SwingBug().setVisible(true); } public SwingBug() { setTitle("Swing bug"); setSize(400, 400); repaint(); } @Override public void paint(Graphics g) { Graphics2D graphics=(Graphics2D) g; graphics.setColor(Color.BLUE); graphics.fillRect(0,0,400,400); drawString(graphics, "Hello Kitty!", 100, 100); }
public void drawString( Graphics2D graphics, String string, int x, int y) { if (string.length() > 0) { Font f = new Font("times", 0, 16); graphics.setFont(f); FontRenderContext fontRendererContext = new FontRenderContext(null, true, true); TextLayout textLayout = new TextLayout(string, f, fontRendererContext); Rectangle2D rectangleOfText=textLayout.getBounds(); Rectangle backgroundRectangle=rectangleOfText.getBounds(); graphics.setColor(new Color(1.0f,1.0f,1.0f,0.5f)); graphics.fillRect(backgroundRectangle.x+x,backgroundRectangle.y+y,backgroundRectangle.width,backgroundRectangle.height); graphics.setColor(new Color(0,0,0)); textLayout.draw(graphics, x, y); } } }
The java.awt. font.TextLayout .getBounds( ) method returns a box that is shifted upwards compared to the box that is returned by Sun's official JRE.
Sample code:
package swingbug;
import java.awt.Color; Graphics2D; font.FontRender Context; font.TextLayout ; geom.Rectangle2 D;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.
import java.awt.Rectangle;
import java.awt.
import java.awt.
import java.awt.
import javax.swing.JFrame;
public class SwingBug extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) { ).setVisible( true); (Graphics2D) g; setColor( Color.BLUE) ; fillRect( 0,0,400, 400); graphics, "Hello Kitty!", 100, 100);
new SwingBug(
}
public SwingBug() {
setTitle("Swing bug");
setSize(400, 400);
repaint();
}
@Override
public void paint(Graphics g) {
Graphics2D graphics=
graphics.
graphics.
drawString(
}
public void drawString( setFont( f); ntext fontRendererContext = new FontRenderConte xt(null, true, true); text); =textLayout. getBounds( ); ngle=rectangleO fText.getBounds (); setColor( new Color(1. 0f,1.0f, 1.0f,0. 5f)); fillRect( backgroundRecta ngle.x+ x,backgroundRec tangle. y+y,backgroundR ectangle. width,backgroun dRectangle. height) ; setColor( new Color(0,0,0)); draw(graphics, x, y);
Graphics2D graphics, String string, int x, int y) {
if (string.length() > 0) {
Font f = new Font("times", 0, 16);
graphics.
FontRenderCo
TextLayout textLayout = new TextLayout(string, f, fontRendererCon
Rectangle2D rectangleOfText
Rectangle backgroundRecta
graphics.
graphics.
graphics.
textLayout.
}
}
}