2010-11-18 179 views

回答

30

如果你是一個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; 
+2

我必須來取消行程 – JPC 2010-11-18 21:04:55

+0

@JPC,是的。中風會保持較厚。我將添加解決該問題的代碼。支持。 – jjnguy 2010-11-18 21:05:35

+0

@JPC,看看我添加的代碼。 – jjnguy 2010-11-18 21:06:22

2

這裏是如何做到這一點:邊框用彩色線條與厚度5

Border linebor = BorderFactory.createLineBorder(new Color(0xAD85FF), 5); 
0
**Tested code with buffered image with different thickness values**: 

Graphics2D g = bufferedImage.createGraphics(); 

int height = //image height 

int width = //image height 

int borderWidth = //border thickness 

int borderControl = 1; 

//set border color 

g.setColor(Color.BLACK); 

//set border thickness 

g.setStroke(new BasicStroke(borderWidth)); 

//to fix issue for even numbers 

if(borderWidth%2 == 0){ 

borderControl = 0; 

} 

g.drawLine(0, 0, 0, height); 

g.drawLine(0, 0, width, 0); 

g.drawLine(0, height – borderControl, width, height – borderControl); 

g.drawLine(width – borderControl, height – borderControl, width – borderControl, 0);