Changing AWT line thickness

23 Jun 2013 . posts . Comments
#AWT #Java #UI

To change the line thickness from thin bands to thicker bands, like below:

line thickness

Set the “stroke” size used on the Graphics2D object:

 
   Graphics2D graphicsSG2D = ((Graphics2D) graphics);
   if(graphics instanceof Graphics2D) 
   {
      stroke = graphicsSG2D.getStroke();
      graphicsSG2D.setStroke(new BasicStroke(3));
   }

   if(graphics instanceof Graphics2D && stroke != null)
   {
      graphicsSG2D.setStroke(stroke);
   }

Don’t forget to save the previous stroke type, using

graphics2D.getStroke()

and then reset it after drawing this alternative-thickness line.


Me

Anthony is an Australian software engineer and mathematician. As a UTS MBT graduate, Anthony is the technology co-founder of a property and cycling-tech startup and enjoys teaching and learning coding with the Australian startup scene.