How to draw text with border in Java

I had a requirement to draw texts using bufferedimage which can have border. So here is the code I found. Basically you write the text 2 times with small shifts

public static void drawStringWithBorder(Graphics g, String string, int x, int y,
			Color c,Color border) {
		g.setColor(border);
		g.drawString(string, ShiftWest(x, 1), ShiftNorth(y, 1));
		g.drawString(string, ShiftWest(x, 1), ShiftSouth(y, 1));
		g.drawString(string, ShiftEast(x, 1), ShiftNorth(y, 1));
		g.drawString(string, ShiftEast(x, 1), ShiftSouth(y, 1));
		g.setColor(c);
		g.drawString(string, x, y);
 
	}
 
	static int ShiftNorth(int p, int distance) {
		return (p - distance);
	}
 
	static int ShiftSouth(int p, int distance) {
		return (p + distance);
	}
 
	static int ShiftEast(int p, int distance) {
		return (p + distance);
	}
 
	static int ShiftWest(int p, int distance) {
		return (p - distance);
	}

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *