绘制矩形边框粗细
是否可以以简单的方式绘制具有给定边框粗细的矩形?
如果要在 Graphics2D 对象上绘图,则可以使用以下方法:setStroke()
Graphics2D g2;
double thickness = 2;
Stroke oldStroke = g2.getStroke();
g2.setStroke(new BasicStroke(thickness));
g2.drawRect(x, y, width, height);
g2.setStroke(oldStroke);
如果这是在 Swing 组件上完成的,并且正在传递一个对象,则可以将其向下转换为 .Graphics
Graphics2D
Graphics2D g2 = (Graphics2D) g;
操作方法如下:用彩色线条边框,厚度为5。
Border linebor = BorderFactory.createLineBorder(new Color(0xAD85FF), 5);