2010-11-18 3 views

Répondre

30

Si vous dessinez sur un objet Graphics2D, vous pouvez utiliser la méthode setStroke():

Graphics2D g2; 
double thickness = 2; 
Stroke oldStroke = g2.getStroke(); 
g2.setStroke(new BasicStroke(thickness)); 
g2.drawRect(x, y, width, height); 
g2.setStroke(oldStroke); 

Si cela est fait sur un composant Swing et vous êtes passé un objet Graphics, vous pouvez downcaster à un Graphics2D.

Graphics2D g2 = (Graphics2D) g; 
+2

Dois-je désinitialiser la course – JPC

+0

@JPC, oui. Le coup restera alors plus épais. Je vais ajouter du code qui résout ce problème. Etre prêt. – jjnguy

+0

@JPC, jetez un oeil sur le code que j'ai ajouté. – jjnguy

2

Voici comment faire: la frontière avec la ligne colorée avec une épaisseur 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); 
Questions connexes