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());
g.fillRect(x, 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 x = (int) ((getWidth() - (int) bounds.getWidth()) / 2); int y = (int) ((getHeight() - (bounds.getHeight() - txt.getDescent())) / 2); y += txt.getAscent() - txt.getDescent();
g2d.drawString("Cat", x, y); x += box.getWidth();
rectangle.getX(), rectangle.getY(), rectangle.getX() + rectangle.getWidth(), rectangle.getY())); rectangle.getX(), rectangle.getY() + rectangle.getHeight(), rectangle.getX() + rectangle.getWidth(), rectangle.getY() + rectangle.getHeight())); rectangle.getX() + rectangle.getWidth(), rectangle.getY(), rectangle.getX() + rectangle.getWidth(), rectangle.getY() + rectangle.getHeight()));
Rectangle bounds = getBounds(); Rectangle2D stringBounds = font2.getStringBounds(getText(), fontRenderContext2); bounds.width = (int) stringBounds.getWidth(); bounds.height = (int) stringBounds.getHeight(); setBounds(bounds); Rectangle bounds = getBounds(); Rectangle2D stringBounds = font1.getStringBounds(getText(), fontRenderContext1); bounds.width = (int) stringBounds.getWidth(); bounds.height = (int) stringBounds.getHeight(); setBounds(bounds);
private VertexWrapper(Object vertex) { this.vertex = vertex; if (vertex != null) { RectangularShape shape = getVertexShape(vertex); width = (shape != null ? shape.getWidth() : 0); } }
/** * Returns the width of the rectangle. This width is expressed * in logical coordinates, not pixel coordinates. */ public double getWidth() { return logicalShape.getWidth(); }
float x = (float)bounds.getX(); 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, float x = (float)bounds.getX(); 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, float x = (float)bounds.getX(); 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, float x = (float)bounds.getX(); 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,
/** * @return the ratio between the lens height and lens width */ public double getRatio() { return lensShape.getHeight()/lensShape.getWidth(); }
// 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); }
FontRenderContext frc = new FontRenderContext(new AffineTransform (), true, true); TextLayout layout = new TextLayout (text, metrics.getFont(), frc); nextX += layout.getBounds().getWidth();
double GetWidthOfAttributedString(Graphics2D graphics2D, AttributedString attributedString) { AttributedCharacterIterator characterIterator = attributedString.getIterator(); FontRenderContext fontRenderContext = graphics2D.getFontRenderContext(); LineBreakMeasurer lbm = new LineBreakMeasurer(characterIterator, fontRenderContext); TextLayout textLayout = lbm.nextLayout(Integer.MAX_VALUE); return textLayout.getBounds().getWidth(); }
public static void transformRectCoords(java.awt.geom.RectangularShape rect, java.awt.geom.AffineTransform xform, float[] rv) { double[] coords = {rect.getX(), rect.getY()}; double[] size = {rect.getWidth(), rect.getHeight()}; xform.transform(coords, 0, coords, 0, 1); xform.deltaTransform(size, 0, size, 0, 1); rv[0] = (float)coords[0]; rv[1] = (float)coords[1]; rv[2] = (float)size[0]; rv[3] = (float)size[1]; } /* public static java.awt.geom.Rectangle2D.Float transformRectCoords(java.awt.geom.RectangularShape rect, java.awt.geom.AffineTransform xform)
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()); }