Java Utililty Methods Draw Centered

List of utility methods to do Draw Centered

  1. HOME
  2. Java
  3. D
  4. Draw Centered

Description

The list of methods to do Draw Centered are organized into topic(s).

Method

void drawCenterCircle(Graphics2D g, Point p, int radius)
Draws a circle arround the given midpoint and radius
g.drawOval(p.x - radius, p.y - radius, 2 * radius, 2 * radius);
void drawCenteredCircle(Graphics2D g, int x, int y, int r)
Draws a centered circle
x = x - (r / 2);
y = y - (r / 2);
g.drawOval(x, y, r, r);
void drawCenterString(Graphics g, Point p, String text)
Draws a centered string
Graphics2D g2d = (Graphics2D) g;
FontMetrics metrics = g2d.getFontMetrics();
int x = p.x - (metrics.stringWidth(text) / 2);
int y = p.y + (int) (metrics.getHeight() / 3);
g2d.drawString(text, x, y);
void drawCentred(Graphics2D g2, String message, Rectangle2D box)
Draws a string centred in the given box.
FontMetrics metrics = g2.getFontMetrics();
Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext());
float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0);
float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0);
g2.drawString(message, x, y);
void drawCentredText(Graphics g, String str, int x, int y, int width, int height)
Draw a string at a given location on screen centered in a given rectangle.
Left justifies the string if it is too long to fit all of the string inside the rectangle.
FontMetrics fm = g.getFontMetrics();
Shape oldClip = g.getClip();
g.clipRect(x, y, width, height);
int xOffset = (width - fm.stringWidth(str)) / 2;
if (xOffset < 0) {
 xOffset = 0;
int yOffset = fm.getAscent() + ((height - fm.getAscent() - fm.getDescent()) / 2);
...
void drawEllipseOld(Graphics g, double xCenter, double yCenter, double semiMA, double semiMI, double angle)
draw Ellipse Old
angle = angle * Math.PI / 180.0;
int nbIt = 30;
Point[] p = new Point[nbIt];
double x, y, tmpX, tmpY;
double curAngle;
for (int i = 0; i < nbIt; i++) {
 curAngle = 2.0 * i / nbIt * Math.PI;
 tmpX = semiMA * Math.cos(curAngle);
...
void drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
draw String Center
Font f = new Font("Serif", Font.PLAIN, fontHeight);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
g.setColor(c);
g.drawString(text, (int) rect.getCenterX() - fm.stringWidth(text) / 2, rect.y + rect.height);
void drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g)
draw String Center
int w = r.x + (r.width - g.getFontMetrics(f).stringWidth(o.toString())) / 2;
int h = r.y + g.getFontMetrics(f).getAscent();
h -= g.getFontMetrics(f).getDescent();
h += (r.height - f.getSize()) / 2;
g.setFont(f);
g.drawString(o.toString(), w, h);
void drawStringCentered(final Graphics2D g2d, final Dimension dim, final String drawString, final int fontSize)
Draw a given String to a graphic object centered
Rectangle2D bounds;
Object KEY_ANTIALIASING_before = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
Object KEY_TEXT_ANTIALIASING_before = g2d.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
float posx = 0, posy = 0;
g2d.setFont(g2d.getFont().deriveFont(Font.PLAIN, fontSize));
FontRenderContext frc = g2d.getFontRenderContext();
...
void drawStringCentered(Graphics g, String str, int x, int y, int width, int height)
Draw a string centered within a rectangle.
FontMetrics fm = g.getFontMetrics(g.getFont());
int xpos = x + ((width - fm.stringWidth(str)) / 2);
int ypos = y + ((height + fm.getAscent()) / 2);
g.drawString(str, xpos, ypos);


AltStyle によって変換されたページ (->オリジナル) /