2016-05-04 12 views
-1

Mit Graphics2D in Java, Wie würden Sie ein Rechteck erstellen, das nur seine Ecken (wie unten dargestellt) mit der DrawLine-Funktion innerhalb des Graphics2D-Objekts gezeichnet hat?Java Graphics2D - Zeichnen von Ecken nur auf einem Rechteck

****       **** 
*        * 
*        * 

      CONTENT 

*        * 
*        * 
****       **** 
+1

Ja, es ist möglich! Natürlich ... –

+0

Warum nicht einfach 8 Zeilen zeichnen? (2 pro Ecke) –

Antwort

1

ich eine Funktion für diese gemacht haben:

public void drawCornerRectangle(Graphics2D g, int x, int y, int width, int height, int cornerLength) { 
     //check width, height and cornerLength 
     if ((width < 0) || (height < 0) || (cornerLength < 1)) { 
      return; 
     } 
     //check if width or height is 0 
     if(width == 0) { 
      g.drawLine(x, y, x, y + cornerLength); 
      g.drawLine(x, y + height, x, (y + height) - cornerLength); 
      return; 
     } else if (height == 0) { 
      g.drawLine(x, y, x + cornerLength, y); 
      g.drawLine(x + width, y, (x + width) - cornerLength, y); 
      return; 
     } 
     //check cornerLength 
     if(cornerLength > width/2 && cornerLength > height/2) { 
      g.drawRect(x, y, width, height); 
     } else { 
      //left up corner 
      g.drawLine(x, y, x + cornerLength, y); 
      g.drawLine(x, y, x, y + cornerLength); 

      //right up corner 
      g.drawLine(x + width, y, (x + width) - cornerLength, y); 
      g.drawLine(x + width, y, x + width, y + cornerLength); 

      //left down corner 
      g.drawLine(x, y + height, x + cornerLength, y + height); 
      g.drawLine(x, y + height, x, (y + height) - cornerLength); 

      //right down corner 
      g.drawLine(x + width, y + height, (x + width) - cornerLength, y + height); 
      g.drawLine(x + width, y + height, x + width, (y + height) - cornerLength); 
     } 
    } 

wie nennen:

//for square: 
drawCornerRectangle(g, 10, 10, 200, 200, 20); //the same width and height 

//for rectangle  
drawCornerRectangle(g, 10, 10, 100, 150, 20); 

Hoffnung, das hilft.