import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; ... String text = "Hello World"; AffineTransform affinetransform = new AffineTransform(); FontRenderContext frc = new FontRenderContext(affinetransform,true,true); Font font = new Font("Tahoma", Font.PLAIN, 12); int textwidth = (int)(font.getStringBounds(text, frc).getWidth()); int textheight = (int)(font.getStringBounds(text, frc).getHeight());
y - fm.getAscent(), (int) rect.getWidth(), (int) rect.getHeight());
// Find the size of string s in font f in the current Graphics context g. FontMetrics fm = g.getFontMetrics(f); java.awt.geom.Rectangle2D rect = fm.getStringBounds(s, g); int textHeight = (int)(rect.getHeight()); int textWidth = (int)(rect.getWidth()); int panelHeight= this.getHeight(); int panelWidth = this.getWidth(); // Center text horizontally and vertically int x = (panelWidth - textWidth) / 2; int y = (panelHeight - textHeight) / 2 + fm.getAscent(); g.drawString(s, x, y); // Draw the string.
int y = (int) ((getHeight() - (bounds.getHeight() - txt.getDescent())) / 2); y += txt.getAscent() - txt.getDescent();
new Line2D.Double( rectangle.getX(), rectangle.getY() + rectangle.getHeight(), rectangle.getX() + rectangle.getWidth(), rectangle.getY() + rectangle.getHeight())); rectangle.getY(), rectangle.getX(), rectangle.getY() + rectangle.getHeight())); rectangle.getY(), rectangle.getX() + rectangle.getWidth(), rectangle.getY() + rectangle.getHeight()));
Rectangle2D stringBounds = font2.getStringBounds(getText(), fontRenderContext2); bounds.width = (int) stringBounds.getWidth(); bounds.height = (int) stringBounds.getHeight(); setBounds(bounds); Rectangle2D stringBounds = font1.getStringBounds(getText(), fontRenderContext1); bounds.width = (int) stringBounds.getWidth(); bounds.height = (int) stringBounds.getHeight(); setBounds(bounds);
/** * Returns the height of the rectangle. This height is expressed * in logical coordinates, not pixel coordinates. */ public double getHeight() { return logicalShape.getHeight(); }
public double getViewRadius() { return lensShape.getHeight()/2; }
float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f }, float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f }, float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f }, float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f },
/** * @return the ratio between the lens height and lens width */ public double getRatio() { return lensShape.getHeight()/lensShape.getWidth(); }
double setupVertexShape(double levelY) { if (vertex == null) return levelY; RectangularShape shape = getVertexShape(vertex); double height = shape.getHeight(); shape.setFrame(centerX - width / 2, levelY, width, height); return (levelY + height); } }
// What's this shape? A rectangle? An ellipse? Does it use ints? floats? doubles? RectangularShape something = ......; // We don't care! System.out.println("The shape (whatever it is) occupies an area of:"); System.out.println(something.getWidth() + " × " + something.getHeight());
/** @return the ratio between the lens height and lens width */ public double getRatio() { return lensShape.getHeight() / lensShape.getWidth(); }
public void setViewCenter(Point2D viewCenter) { double width = lensShape.getWidth(); double height = lensShape.getHeight(); lensShape.setFrame(viewCenter.getX()-width/2, viewCenter.getY()-height/2, width, height); }
public void setCenter(Point2D viewCenter) { double width = lensShape.getWidth(); double height = lensShape.getHeight(); lensShape.setFrame( viewCenter.getX() - width / 2, viewCenter.getY() - height / 2, width, height); }
/** * Defines the new range of values covered by the rectangle according to * the <var>x</var> axis. The values covered along the <var>y</var> axis * will not be changed. The values must be expressed in logical coordinates * * @see #getMinX * @see #getMaxX * @see #getCenterX */ public final void setX(final double min, final double max) { setFrame(Math.min(min,max), logicalShape.getY(), Math.abs(max-min), logicalShape.getHeight()); }
public double getXHeight(Font font) { FontRenderContext fc = new FontRenderContext(null, false, false); TextLayout layout = new TextLayout("x", font, fc); return layout.getBounds().getHeight(); }
public void setGeometry(Attribute shapeAttribute) { RectangularShape shape = (RectangularShape)shapeAttribute.get(userVertex); if (shape != null) { width = shape.getWidth(); height = shape.getHeight(); centerX = shape.getCenterX(); centerY = shape.getCenterY(); } else width = height = centerX = centerY = 0; }
/** * Moves this rectangle by a given x- and y-offset * @param r the rectangle to move * @param dx the x-offset * @param dy the y-offset */ public static void translate(RectangularShape r, double dx, double dy) { r.setFrame(r.getX() + dx, r.getY() + dy, r.getWidth(), r.getHeight()); }