Java Code Examples for java.awt.FontMetrics#getStringBounds()

The following examples show how to use java.awt.FontMetrics#getStringBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DemoPanel.java    From beautyeye with Apache License 2.0 6 votes vote down vote up
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();
    Dimension size = getSize();

    Color textColor = Utilities.deriveColorHSB(getBackground(), 0, 0, -.3f);
    Color dotColor = Utilities.deriveColorHSB(textColor, 0, .2f, -.08f);
    g2.setColor(textColor);
    g2.setFont(UIManager.getFont("Label.font").deriveFont(32f));
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D rect = metrics.getStringBounds(message, g2);
    Rectangle2D dotRect = metrics.getStringBounds(".", g2);
    float x = (float) (size.width - (rect.getWidth() + 3 * dotRect.getWidth())) / 2;
    float y = (float) (size.height - rect.getHeight()) / 2;
    g2.drawString(message, x, y);
    int tri = getTriState();
    float dx = 0;
    for (int i = 0; i < 3; i++) {
        g2.setColor(animator.isRunning() && i == tri ?
                dotColor :
                textColor);
        g2.drawString(".", x + (float) (rect.getWidth() + dx), y);
        dx += dotRect.getWidth();
    }
}
 
Example 2
Source File: GameButton.java    From ShootPlane with Apache License 2.0 6 votes vote down vote up
@Override
   protected void paintComponent(Graphics g) {
super.paintComponent(g);
Image buttonBgImg = null;
if (this.buttonStatus.equals(BUTTON_NORMAL)) {
    buttonBgImg = new ImageIcon(Config.BUTTON_BG_IMG).getImage();
} else if (this.buttonStatus.equals(BUTTON_HOVER)) {
    buttonBgImg = new ImageIcon(Config.BUTTON_HOVER_BG_IMG).getImage();
}
int buttonWidth = buttonBgImg.getWidth(this);
int buttonHeight = buttonBgImg.getHeight(this);

this.setSize(buttonWidth, buttonHeight);
this.setPreferredSize(new Dimension(buttonWidth, buttonHeight));

Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(buttonBgImg, 0, 0, buttonWidth, buttonHeight, this);
FontMetrics metric = g.getFontMetrics();
Rectangle2D rect = metric.getStringBounds(text, g);
g2d.drawString(text, (float) (buttonWidth / 2 - rect.getWidth() / 2),
	(float) ((buttonHeight / 2) + ((metric.getAscent() + metric.getDescent()) / 2 - metric.getDescent())));
   }
 
Example 3
Source File: PestControlOverlay.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void renderProgressWidget(Graphics2D graphics)
{
	Widget bar = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_BAR).getChild(0);
	Rectangle2D bounds = bar.getBounds().getBounds2D();

	Widget prgs = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_PROGRESS).getChild(0);
	int perc = (int) ((prgs.getBounds().getWidth() / bounds.getWidth()) * 100);

	Color color = Color.GREEN;
	if (perc < 25)
	{
		color = Color.RED;
	}

	String text = String.valueOf(perc) + "%";

	FontMetrics fm = graphics.getFontMetrics();
	Rectangle2D textBounds = fm.getStringBounds(text, graphics);
	int x = (int) (bounds.getX() - textBounds.getWidth());
	int y = (int) (bounds.getY() + fm.getHeight() - 2);

	graphics.setColor(Color.BLACK);
	graphics.drawString(text, x + 1, y + 1);
	graphics.setColor(color);
	graphics.drawString(text, x, y);
}
 
Example 4
Source File: TextUtilities.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the bounds for the specified text.
 *
 * @param text  the text (<code>null</code> permitted).
 * @param g2  the graphics context (not <code>null</code>).
 * @param fm  the font metrics (not <code>null</code>).
 *
 * @return The text bounds (<code>null</code> if the <code>text</code>
 *         argument is <code>null</code>).
 */
public static Rectangle2D getTextBounds(String text, Graphics2D g2, 
        FontMetrics fm) {

    Rectangle2D bounds;
    if (TextUtilities.useFontMetricsGetStringBounds) {
        bounds = fm.getStringBounds(text, g2);
        // getStringBounds() can return incorrect height for some Unicode
        // characters...see bug parade 6183356, let's replace it with
        // something correct
        LineMetrics lm = fm.getFont().getLineMetrics(text,
                g2.getFontRenderContext());
        bounds.setRect(bounds.getX(), bounds.getY(), bounds.getWidth(),
                lm.getHeight());
    }
    else {
        double width = fm.stringWidth(text);
        double height = fm.getHeight();
        if (logger.isDebugEnabled()) {
            logger.debug("Height = " + height);
        }
        bounds = new Rectangle2D.Double(0.0, -fm.getAscent(), width,
                height);
    }
    return bounds;
}
 
Example 5
Source File: Perspective.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Calculates text position and centers on minimap depending on string length.
 *
 * @param client the game client
 * @param graphics the game graphics
 * @param localLocation local location of the tile
 * @param text string for width measurement
 * @return a {@link Point} on screen corresponding to the given
 * localLocation.
 */
public static Point getCanvasTextMiniMapLocation(
	@Nonnull Client client,
	@Nonnull Graphics2D graphics,
	@Nonnull LocalPoint localLocation,
	@Nonnull String text)
{
	Point p = Perspective.localToMinimap(client, localLocation);

	if (p == null)
	{
		return null;
	}

	FontMetrics fm = graphics.getFontMetrics();
	Rectangle2D bounds = fm.getStringBounds(text, graphics);
	int xOffset = p.getX() - (int) (bounds.getWidth() / 2);
	int yOffset = p.getY() - (int) (bounds.getHeight() / 2) + fm.getAscent();

	return new Point(xOffset, yOffset);
}
 
Example 6
Source File: TextUtilities.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the bounds for the specified text.
 *
 * @param text  the text (<code>null</code> permitted).
 * @param g2  the graphics context (not <code>null</code>).
 * @param fm  the font metrics (not <code>null</code>).
 *
 * @return The text bounds (<code>null</code> if the <code>text</code>
 *         argument is <code>null</code>).
 */
public static Rectangle2D getTextBounds(String text, Graphics2D g2,
        FontMetrics fm) {

    final Rectangle2D bounds;
    if (TextUtilities.useFontMetricsGetStringBounds) {
        bounds = fm.getStringBounds(text, g2);
        // getStringBounds() can return incorrect height for some Unicode
        // characters...see bug parade 6183356, let's replace it with
        // something correct
        LineMetrics lm = fm.getFont().getLineMetrics(text,
                g2.getFontRenderContext());
        bounds.setRect(bounds.getX(), bounds.getY(), bounds.getWidth(),
                lm.getHeight());
    }
    else {
        double width = fm.stringWidth(text);
        double height = fm.getHeight();
        bounds = new Rectangle2D.Double(0.0, -fm.getAscent(), width,
                height);
    }
    return bounds;
}
 
Example 7
Source File: DemoPanel.java    From littleluck with Apache License 2.0 6 votes vote down vote up
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();
    Dimension size = getSize();

    Color textColor = Utilities.deriveColorHSB(getBackground(), 0, 0, -.3f);
    Color dotColor = Utilities.deriveColorHSB(textColor, 0, .2f, -.08f);
    g2.setColor(textColor);
    g2.setFont(UIManager.getFont("Label.font").deriveFont(32f));
    FontMetrics metrics = g2.getFontMetrics();
    Rectangle2D rect = metrics.getStringBounds(message, g2);
    Rectangle2D dotRect = metrics.getStringBounds(".", g2);
    float x = (float) (size.width - (rect.getWidth() + 3 * dotRect.getWidth())) / 2;
    float y = (float) (size.height - rect.getHeight()) / 2;
    g2.drawString(message, x, y);
    int tri = getTriState();
    float dx = 0;
    for (int i = 0; i < 3; i++) {
        g2.setColor(animator.isRunning() && i == tri ?
                dotColor :
                textColor);
        g2.drawString(".", x + (float) (rect.getWidth() + dx), y);
        dx += dotRect.getWidth();
    }
}
 
Example 8
Source File: DiagramCanvas.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void drawTextBox(Graphics2D g2D, String text, int x0, int y0, Color color) {

        final FontMetrics fontMetrics = g2D.getFontMetrics();
        final Rectangle2D textBounds = fontMetrics.getStringBounds(text, g2D);
        x0 -= textBounds.getWidth() / 2;
        textBounds.setRect(textBounds.getX() - 1, textBounds.getY(), textBounds.getWidth(), textBounds.getHeight());
        Rectangle2D.Double r = new Rectangle2D.Double(x0 + textBounds.getX() - 2.0,
                                                      y0 + textBounds.getY() - 2.0,
                                                      textBounds.getWidth() + 4.0,
                                                      textBounds.getHeight() + 4.0);

        if (r.getMaxX() > getWidth()) {
            r.setRect(getWidth() - r.getWidth(), r.getY(), r.getWidth(), r.getHeight());
        }
        if (r.getMinX() < 0) {
            r.setRect(0, r.getY(), r.getWidth(), r.getHeight());
        }
        if (r.getMaxY() > getHeight()) {
            r.setRect(r.getX(), getHeight() - r.getHeight(), r.getWidth(), r.getHeight());
        }
        if (r.getMinY() < 0) {
            r.setRect(r.getX(), 0, r.getWidth(), r.getHeight());
        }
        g2D.setColor(color);
        g2D.fill(r);
        g2D.setColor(Color.black);
        g2D.draw(r);
        g2D.drawString(text, x0, y0);
    }
 
Example 9
Source File: LineNumbersActionsBar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean checkLinesWidth(Graphics g) {
    FontMetrics fm = g.getFontMetrics(getLinesFont());
    Rectangle2D rect = fm.getStringBounds(Integer.toString(linesCount), g);
    linesWidth = (int) rect.getWidth() + LINES_BORDER_WIDTH * 2;
    if (linesWidth != oldLinesWidth) {
        oldLinesWidth = linesWidth;
        revalidate();
        repaint();
        return true;
    }
    return false;
}
 
Example 10
Source File: WidgetOverlay.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
private void renderProgressWidget(Graphics2D graphics)
{
	String text;
	int percentage;

	Widget bar = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_BAR).getChild(0);
	Rectangle2D bounds = bar.getBounds().getBounds2D();

	Widget prgs = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_PROGRESS).getChild(0);

	// At 0% the inner widget changes and your progress will not increase anymore
	if ((int) (prgs.getBounds().getX()) - bounds.getX() != 2)
	{
		percentage = 0;
		text = "FAILED";
	}
	else
	{
		percentage = (int) ((prgs.getBounds().getWidth() / bounds.getWidth()) * 100);
		text = percentage + "%";
	}

	Color color = Color.GREEN;
	if (percentage < 25)
	{
		color = Color.RED;
	}

	FontMetrics fm = graphics.getFontMetrics();
	Rectangle2D textBounds = fm.getStringBounds(text, graphics);
	int x = (int) (bounds.getX() - textBounds.getWidth() - 4);
	int y = (int) (bounds.getY() + fm.getHeight() - 2);

	graphics.setColor(Color.BLACK);
	graphics.drawString(text, x + 1, y + 1);
	graphics.setColor(color);
	graphics.drawString(text, x, y);
}
 
Example 11
Source File: DisplayableCard.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
private void drawString(Graphics g, String s, Color background, Color foreground, int x, int y) {
	g.setFont(new Font("default", Font.BOLD, 12));
	g.setColor(background);
	FontMetrics fm = g.getFontMetrics();
	Rectangle2D rect = fm.getStringBounds(s, g);
	g.fillRect(x, y - fm.getAscent(), (int) rect.getWidth(), (int) rect.getHeight());
	g.setColor(foreground);
	g.drawString(s, x, y);
}
 
Example 12
Source File: DevToolsOverlay.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
private void renderInventory(Graphics2D graphics)
{
	Widget inventoryWidget = client.getWidget(WidgetInfo.INVENTORY);
	if (inventoryWidget == null || inventoryWidget.isHidden())
	{
		return;
	}

	for (WidgetItem item : inventoryWidget.getWidgetItems())
	{
		Rectangle slotBounds = item.getCanvasBounds();

		String idText = "" + item.getId();
		FontMetrics fm = graphics.getFontMetrics();
		Rectangle2D textBounds = fm.getStringBounds(idText, graphics);

		int textX = (int) (slotBounds.getX() + (slotBounds.getWidth() / 2) - (textBounds.getWidth() / 2));
		int textY = (int) (slotBounds.getY() + (slotBounds.getHeight() / 2) + (textBounds.getHeight() / 2));

		graphics.setColor(new Color(255, 255, 255, 65));
		graphics.fill(slotBounds);

		graphics.setColor(Color.BLACK);
		graphics.drawString(idText, textX + 1, textY + 1);
		graphics.setColor(YELLOW);
		graphics.drawString(idText, textX, textY);
	}
}
 
Example 13
Source File: PlafPaintUtils.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Paint a String centered at a given (x,y) coordinate.
 */
public static void paintCenteredString(Graphics2D g, String str, Font font,
		int centerX, int centerY) {
	g.setFont(font);
	FontMetrics fm = g.getFontMetrics();
	Rectangle2D r = fm.getStringBounds(str, g);
	float x = (float) (centerX - r.getWidth() / 2);
	float y = (float) (centerY - r.getHeight() / 2 - r.getY());
	g.drawString(str, x, y);
}
 
Example 14
Source File: WidgetInspectorOverlay.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderWiw(Graphics2D g, Object wiw, Color color)
{
	g.setColor(color);

	if (wiw instanceof WidgetItem)
	{
		WidgetItem wi = (WidgetItem) wiw;
		Rectangle bounds = wi.getCanvasBounds();
		g.draw(bounds);

		String text = wi.getId() + "";
		FontMetrics fm = g.getFontMetrics();
		Rectangle2D textBounds = fm.getStringBounds(text, g);

		int textX = (int) (bounds.getX() + (bounds.getWidth() / 2) - (textBounds.getWidth() / 2));
		int textY = (int) (bounds.getY() + (bounds.getHeight() / 2) + (textBounds.getHeight() / 2));

		g.setColor(Color.BLACK);
		g.drawString(text, textX + 1, textY + 1);
		g.setColor(Color.ORANGE);
		g.drawString(text, textX, textY);
	}
	else
	{
		Widget w = (Widget) wiw;
		g.draw(w.getBounds());
	}
}
 
Example 15
Source File: AbstractAWTLayout.java    From ReactionDecoder with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param g
 * @param text
 * @param cX
 * @param cY
 * @return
 */
public Point2f getTextPoint(Graphics g, String text, double cX, double cY) {
    FontMetrics metrics = g.getFontMetrics();
    Rectangle2D stringBounds = metrics.getStringBounds(text, g);
    double halfWidth = stringBounds.getWidth() / 2;
    double halfHeight = stringBounds.getHeight() / 2;
    double ascent = metrics.getAscent();
    float x = (float) (cX - halfWidth);
    float y = (float) (cY - halfHeight + ascent);
    return new Point2f(x, y);
}
 
Example 16
Source File: ReflectedImageLabel.java    From radiance with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** 
 * Paints the component
 * 
 * @param graphics The graphics context
 */
public void paintComponent(Graphics graphics) {
    // Don't paint if I'm off screen
    if ((getX() + getWidth() < 0) && (getY() + getHeight() < 0)) {
        return;
    }

    Graphics2D g = (Graphics2D) graphics;
    Image image = bufferedImage;

    int drawHeight = (int) ((double) getHeight() / 1.5);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    // SiAlphaCompositemple scale only
    Composite oldAc = g.getComposite();
    g.setComposite(alphaComposite);
    g.drawImage(image, 0, 0, getWidth(), getHeight(), 0, 0, image
            .getWidth(null), image.getHeight(null), null);
    // Draw text if there is any...
    if ((text != null) && (text.length() > 0)) {
        Graphics2D g2d = (Graphics2D) graphics;
        Rectangle2D bounds = reference.getStringBounds(text, g2d
                .getFontRenderContext());
        double scaleFactor = (double) getWidth() / image.getWidth(null);
        double scaleFactor2 = (double) getWidth() / bounds.getWidth();
        int fontSize = (int) Math.min(25.0 * scaleFactor,
                14.0 * scaleFactor2);
        Font font = new Font("Arial", Font.BOLD, fontSize);
        g2d.setFont(font);
        int dx = (getWidth() - (int) font.getStringBounds(text,
                g2d.getFontRenderContext()).getWidth()) / 2;
        int dy = drawHeight + 2 * (int) (bounds.getHeight() * scaleFactor);
        Color background = this.getBackground();
        int red = background.getRed();
        int green = background.getRed();
        int blue = background.getRed();
        graphics.setColor(new Color(red,green,blue,96));
        FontMetrics fm = g2d.getFontMetrics();
        Rectangle2D rect = fm.getStringBounds(text,graphics);
       
        graphics.fillRoundRect(dx-(int)rect.getHeight()/2, dy - (int) g2d.getFontMetrics().getAscent(),
                (int)rect.getWidth()+((int)rect.getHeight()), fm.getAscent() + fm.getDescent(),(int)rect.getHeight(),(int)rect.getHeight());
        graphics.setColor(this.getForeground());
        graphics.drawString(text, dx, dy);
    }
    g.setComposite(oldAc);

}
 
Example 17
Source File: PButton.java    From PolyGlot with MIT License 4 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
    boolean enabled = isEnabled();
    Color bgColor;
    Color fontColor;
    final int thisHeight = getHeight();
    final int thisWidth = getWidth();
    
    if (!enabled) {
        bgColor = PGTUtil.COLOR_DISABLED_BG;
        fontColor = PGTUtil.COLOR_DISABLED_FOREGROUND;
    } else if (mousePressed && mouseEntered) {
        bgColor = PGTUtil.COLOR_SELECTED_BG;
        fontColor = getForeground();
    } else {
        bgColor = PGTUtil.COLOR_ENABLED_BG;
        fontColor = getForeground();
    }
    
    if (activeSelected) {
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    
    // turn on anti-alias mode
    Graphics2D antiAlias = (Graphics2D) g;
    antiAlias.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.setColor(bgColor);
    
    g.fillRect(2, 2, thisWidth - 4, thisHeight - 4);

    // draw black border on mouseover if button enabled
    if (mouseEntered && enabled) {
        g.setColor(PGTUtil.COLOR_MOUSEOVER_BORDER);
        g.drawRect(1, 1, thisWidth - 3, thisHeight - 3);
    }
    
    g.setColor(fontColor);
    g.setFont(getFont());
    
    char[] text = getText().toCharArray();
    FontMetrics fm = g.getFontMetrics(getFont());
    Rectangle2D rec = fm.getStringBounds(getText(), g);
    int stringW = (int) Math.round(rec.getWidth());
    int stringH = (int) Math.round(rec.getHeight());
    g.drawChars(text, 0, text.length, (thisWidth/2) - (stringW/2), thisHeight/2 + stringH/4);
    
    Icon icon = this.getIcon();
    if (icon != null) {
        icon.paintIcon(this, g, (thisWidth -icon.getIconWidth())/2, (thisHeight - icon.getIconHeight())/2);
    }
}
 
Example 18
Source File: LabeledRectangle.java    From WhiteRabbit with Apache License 2.0 4 votes vote down vote up
public void paint(Graphics g) {
	if (!isVisible)
		return;		
	
	Graphics2D g2d = (Graphics2D) g.create();		
	
	g2d.setColor(transparentColor);
	g2d.fillRect(x, y, width, height);
	if (isSelected) {
		g2d.setColor(Color.BLACK);
		g2d.setStroke(dashed);
	} else {
		g2d.setColor(baseColor);
		g2d.setStroke(stroke);
	}
	g2d.drawRect(x, y, width, height);
	g2d.setColor(Color.BLACK);

	g2d.setFont(new Font("default", Font.PLAIN, FONT_SIZE));
	FontMetrics fm = g2d.getFontMetrics();

	Rectangle2D r = fm.getStringBounds(item.outputName(), g2d);
	if (r.getWidth() >= width) {
		int breakPoint = 0;
		int index = nextBreakPoint(item.outputName(), 0);
		double midPoint = item.outputName().length() / 2d;
		while (index != -1) {
			if (Math.abs(index - midPoint) < Math.abs(breakPoint - midPoint))
				breakPoint = index;
			index = nextBreakPoint(item.outputName(), index + 1);
		}
		if (breakPoint == 0) {
			breakPoint = (int) midPoint + 1;
		} else {
			breakPoint++;
		}
		String line1 = item.outputName().substring(0, breakPoint);
		String line2 = item.outputName().substring(breakPoint);

		Rectangle2D r1 = fm.getStringBounds(line1, g2d);
		Rectangle2D r2 = fm.getStringBounds(line2, g2d);
		if (r1.getWidth() >= width) {
			line1 = item.outputName().substring(0, (int) midPoint);
			line2 = item.outputName().substring((int) midPoint);
			r1 = fm.getStringBounds(line1, g2d);
			r2 = fm.getStringBounds(line2, g2d);
		} else if (r2.getWidth() >= width) {
			line1 = item.outputName().substring(0, (int) midPoint);
			line2 = item.outputName().substring((int) midPoint);
			r1 = fm.getStringBounds(line1, g2d);
			r2 = fm.getStringBounds(line2, g2d);
		}
		// If both lines are too wide, then the text will still go out of bounds
		int textX1 = (this.getWidth() - (int) r1.getWidth()) / 2;
		int textY1 = (this.getHeight() / 2 - (int) r1.getHeight()) / 2 + fm.getAscent();
		g2d.drawString(line1, x + textX1, y + textY1);

		int textX2 = (this.getWidth() - (int) r2.getWidth()) / 2;
		int textY2 = (int) Math.round(this.getHeight() * 1.5 - (int) r2.getHeight()) / 2 + fm.getAscent();
		g2d.drawString(line2, x + textX2, y + textY2);
	} else {
		int textX = (this.getWidth() - (int) r.getWidth()) / 2;
		int textY = (this.getHeight() - (int) r.getHeight()) / 2 + fm.getAscent();
		g2d.drawString(item.outputName(), x + textX, y + textY);
	}
}
 
Example 19
Source File: WorldMapRegionOverlay.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void drawRegionOverlay(Graphics2D graphics)
{
	RenderOverview ro = client.getRenderOverview();
	Widget map = client.getWidget(WidgetInfo.WORLD_MAP_VIEW);
	Float pixelsPerTile = ro.getWorldMapZoom();

	if (map == null)
	{
		return;
	}

	Rectangle worldMapRect = map.getBounds();
	graphics.setClip(worldMapRect);

	int widthInTiles = (int) Math.ceil(worldMapRect.getWidth() / pixelsPerTile);
	int heightInTiles = (int) Math.ceil(worldMapRect.getHeight() / pixelsPerTile);

	Point worldMapPosition = ro.getWorldMapPosition();

	// Offset in tiles from anchor sides
	int yTileMin = worldMapPosition.getY() - heightInTiles / 2;
	int xRegionMin = (worldMapPosition.getX() - widthInTiles / 2) & REGION_TRUNCATE;
	int xRegionMax = ((worldMapPosition.getX() + widthInTiles / 2) & REGION_TRUNCATE) + REGION_SIZE;
	int yRegionMin = (yTileMin & REGION_TRUNCATE);
	int yRegionMax = ((worldMapPosition.getY() + heightInTiles / 2) & REGION_TRUNCATE) + REGION_SIZE;
	int regionPixelSize = (int) Math.ceil(REGION_SIZE * pixelsPerTile);

	for (int x = xRegionMin; x < xRegionMax; x += REGION_SIZE)
	{
		for (int y = yRegionMin; y < yRegionMax; y += REGION_SIZE)
		{
			graphics.setColor(WHITE_TRANSLUCENT);

			int yTileOffset = -(yTileMin - y);
			int xTileOffset = x + widthInTiles / 2 - worldMapPosition.getX();

			int xPos = ((int) (xTileOffset * pixelsPerTile)) + (int) worldMapRect.getX();
			int yPos = (worldMapRect.height - (int) (yTileOffset * pixelsPerTile)) + (int) worldMapRect.getY();
			// Offset y-position by a single region to correct for drawRect starting from the top
			yPos -= regionPixelSize;

			graphics.drawRect(xPos, yPos, regionPixelSize, regionPixelSize);

			int regionId = ((x >> 6) << 8) | (y >> 6);
			String regionText = String.valueOf(regionId);
			FontMetrics fm = graphics.getFontMetrics();
			Rectangle2D textBounds = fm.getStringBounds(regionText, graphics);
			int labelWidth = (int) textBounds.getWidth() + 2 * LABEL_PADDING;
			int labelHeight = (int) textBounds.getHeight() + 2 * LABEL_PADDING;
			graphics.fillRect(xPos, yPos, labelWidth, labelHeight);
			graphics.setColor(Color.BLACK);
			graphics.drawString(regionText, xPos + LABEL_PADDING, yPos + (int) textBounds.getHeight() + LABEL_PADDING);
		}
	}
}
 
Example 20
Source File: WorldMapRegionOverlay.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
private void drawRegionOverlay(Graphics2D graphics)
{
	RenderOverview ro = client.getRenderOverview();
	Widget map = client.getWidget(WidgetInfo.WORLD_MAP_VIEW);
	float pixelsPerTile = ro.getWorldMapZoom();

	if (map == null)
	{
		return;
	}

	Rectangle worldMapRect = map.getBounds();
	graphics.setClip(worldMapRect);

	int widthInTiles = (int) Math.ceil(worldMapRect.getWidth() / pixelsPerTile);
	int heightInTiles = (int) Math.ceil(worldMapRect.getHeight() / pixelsPerTile);

	Point worldMapPosition = ro.getWorldMapPosition();

	// Offset in tiles from anchor sides
	int yTileMin = worldMapPosition.getY() - heightInTiles / 2;
	int xRegionMin = (worldMapPosition.getX() - widthInTiles / 2) & REGION_TRUNCATE;
	int xRegionMax = ((worldMapPosition.getX() + widthInTiles / 2) & REGION_TRUNCATE) + REGION_SIZE;
	int yRegionMin = (yTileMin & REGION_TRUNCATE);
	int yRegionMax = ((worldMapPosition.getY() + heightInTiles / 2) & REGION_TRUNCATE) + REGION_SIZE;
	int regionPixelSize = (int) Math.ceil(REGION_SIZE * pixelsPerTile);

	for (int x = xRegionMin; x < xRegionMax; x += REGION_SIZE)
	{
		for (int y = yRegionMin; y < yRegionMax; y += REGION_SIZE)
		{
			graphics.setColor(WHITE_TRANSLUCENT);

			int yTileOffset = -(yTileMin - y);
			int xTileOffset = x + widthInTiles / 2 - worldMapPosition.getX();

			int xPos = ((int) (xTileOffset * pixelsPerTile)) + (int) worldMapRect.getX();
			int yPos = (worldMapRect.height - (int) (yTileOffset * pixelsPerTile)) + (int) worldMapRect.getY();
			// Offset y-position by a single region to correct for drawRect starting from the top
			yPos -= regionPixelSize;

			graphics.drawRect(xPos, yPos, regionPixelSize, regionPixelSize);

			int regionId = ((x >> 6) << 8) | (y >> 6);
			String regionText = String.valueOf(regionId);
			FontMetrics fm = graphics.getFontMetrics();
			Rectangle2D textBounds = fm.getStringBounds(regionText, graphics);
			int labelWidth = (int) textBounds.getWidth() + 2 * LABEL_PADDING;
			int labelHeight = (int) textBounds.getHeight() + 2 * LABEL_PADDING;
			graphics.fillRect(xPos, yPos, labelWidth, labelHeight);
			graphics.setColor(Color.BLACK);
			graphics.drawString(regionText, xPos + LABEL_PADDING, yPos + (int) textBounds.getHeight() + LABEL_PADDING);
		}
	}
}