Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw rectangle border thickness

Tags:

java

swing

awt

Is it possible to do draw a rectangle with a given border thickness in an easy way?

like image 216
JPC Avatar asked Nov 18 '10 20:11

JPC


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

If you are drawing on a Graphics2D object, you can use the setStroke() method:

Graphics2D g2; double thickness = 2; Stroke oldStroke = g2.getStroke(); g2.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.

Graphics2D g2 = (Graphics2D) g; 
like image 198
jjnguy Avatar answered Sep 21 '22 09:09

jjnguy