Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border in DrawRectangle

Tags:

c#

graphics

Well, I'm coding the OnPaint event for my own control and it is very nescessary for me to make it pixel-accurate.

I've got a little problem with borders of rectangles.

See picture:

removed dead ImageShack link

These two rectangles were drawn with the same location and size parameters, but using different size of the pen. See what happend? When border became larger it has eaten the free space before the rectangle (on the left).

I wonder if there is some kind of property which makes border be drawn inside of the rectangle, so that the distance to rectangle will always be the same. Thanks.

like image 474
undsoft Avatar asked May 29 '09 11:05

undsoft


People also ask

How do you draw a rectangle with a border in Java?

setStroke(new BasicStroke(thickness)); g2. drawRect(x, y, width, height); g2. setStroke(oldStroke); If this is being done on a Swing component and you are being passed a Graphics object, you can downcast it to a Graphics2D .


1 Answers

You can do this by specifying PenAlignment

Pen pen = new Pen(Color.Black, 2); pen.Alignment = PenAlignment.Inset; //<-- this g.DrawRectangle(pen, rect); 
like image 70
hultqvist Avatar answered Sep 17 '22 17:09

hultqvist