2011-08-12 14 views
9

मुझे केवल एक घटक के एक तरफ पर गोलाकार सीमा बनाने की आवश्यकता है। ,एक तरफ घुमावदार सीमा केवल जावा

new LineBorder(Color.RED, 3, true) 

मैं this thread देखा है आप कैसे एक मैट सीमा है कि केवल एक घटक के एक तरफ इस्तेमाल किया जा सकता बनाने के लिए पता चलता है जो हालांकि एक मैट सीमा नहीं है:

इस कोड को एक गोल सीमा बनाता है गोल।

क्या एक तरफ एक गोलाकार सीमा हो सकती है?

संपादित करें:

मैं इस तरह यौगिक सीमा का उपयोग कर की कोशिश की है:

cell.setBorder(BorderFactory.createCompoundBorder(
     new LineBorder(borderColor, 3, true), 
     BorderFactory.createMatteBorder(0, 3, 0, 0, Color.black))); 

लेकिन यह काम नहीं करता ...

उत्तर

3

LineBorder केवल सभी कोने गोल है या नहीं का समर्थन करता है। एक यौगिक सीमा (जावाडॉक राज्यों के रूप में) बाहरी के लिए एक सीमा और अंदर के लिए एक का उपयोग करती है और इस प्रकार बाएं/दाएं या ऊपर/नीचे के बीच अंतर नहीं होती है।

मुझे डर है कि आपको या तो अपना खुद का Border कार्यान्वयन लिखना होगा या किसी अन्य व्यक्ति (बाहरी पुस्तकालय) द्वारा पहले से ही बनाई गई है।

6

आप LineBorder की विधि को ओवरराइड और तुम वहाँ LineBorder

public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 
     Color oldColor = g.getColor(); 
     int i; 

    /// PENDING(klobad) How/should do we support Roundtangles? 
     g.setColor(lineColor); 
     for(i = 0; i < thickness; i++) { 
     if(!roundedCorners) 
       g.drawRect(x+i, y+i, width-i-i-1, height-i-i-1); 
     else 
SET CLIP HERE TO DRAW ONLY NECESSARY PART 
       g.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, thickness, thickness); 
     } 
     g.setColor(oldColor); 
    } 
+0

Relacing 'g.drawRoundRect (...)' तुच्छ नहीं लगता है, IMO। लेकिन यह एक शुरुआत है - आपको गोल कोनों और किनारों को अलग से खींचना होगा। – Thomas

+0

@ थॉमस ईसीयर ने ऐसा करने से कहा: पी यह मेरे पास आर्क के साथ गोलाकार कोनों को आकर्षित करने के लिए है। मैं कोशिश कर रहा हूं लेकिन अभी तक प्रबंधित नहीं किया है। – David

+0

अब हमें केवल drawRoundRect का स्रोत कोड चाहिए ... – David

3

के स्रोतों से जरूरत है यहाँ कैसे Graphics2D#clipRect() का उपयोग करने पर एक उदाहरण है सब आकर्षित कर सकते हैं। यह कोड बस दो दाएं गोलाकार कोनों को रखें और बाईं ओर सामान्य सीमाएं हैं। जैसा ऊपर बताया गया है, आपको इस कोड का उपयोग अपने कस्टम LineBorder के अंदर करना होगा।

Graphics2D g2d = (Graphics2D) g; 

g2d.clipRect(150, 10, 100, 100); 
g2d.draw(new RoundRectangle2D.Double(100, 10, 80, 30, 15, 15)); 

g2d.setClip(null); 
g2d.clipRect(100, 10, 50, 100); 
g2d.draw(new Rectangle2D.Double(100, 10, 80, 30)); 
+0

कि कोड एक गोल सीमा आकर्षित नहीं करता है। :( – David

0

यहाँ एक उदाहरण है,

JPanel content = new JPanel(); content.setBorder(BorderFactory.createEmptyBorder(1,30,1,1));

0
public static final int TOP_LEFT = 1; 
public static final int TOP_RIGHT = 2; 
public static final int BOTTOM_LEFT = 4; 
public static final int BOTTOM_RIGHT = 8; 
public static final int ALL_CORNERS = TOP_LEFT + TOP_RIGHT + BOTTOM_LEFT + BOTTOM_RIGHT; 

public static void drawRoundRect(Graphics g, Color fillColor, Color borderColor, int x, int y, int width, int height, int radius, int cornerMask) 
{ 
    // // XXX Old code (without selectively disabled round corners): 
    // if (fillColor != null) 
    // { 
    // og.setColor(fillColor); 
    // og.fillRoundRect(x, y, width - 1, height - 1, radius, radius); 
    // } 
    // if (borderColor != null && !borderColor.equals(fillColor)) 
    // { 
    // og.setColor(borderColor); 
    // og.drawRoundRect(x, y, width - 1, height - 1, radius, radius); 
    // } 
    radius += radius % 2; // so we don't have to deal with rounding issues for odd numbers 
    int radiusHalf = radius/2; 
    width--; 
    height--; 
    if (fillColor != null) 
    { 
     g.setColor(fillColor); 
     // og.fillRoundRect(x, y, width - 1, height - 1, radius, radius); 
     if ((cornerMask & TOP_LEFT) > 0) 
     { 
      g.fillArc(x, y, radius, radius, 90, 90); 
     } 
     else 
     { 
      g.fillRect(x, y, radiusHalf, radiusHalf); 
     } 
     if ((cornerMask & TOP_RIGHT) > 0) 
     { 
      g.fillArc(x + width - radius, y, radius, radius, 0, 90); 
     } 
     else 
     { 
      g.fillRect(x + width - radiusHalf, y, radiusHalf, radiusHalf); 
     } 
     if ((cornerMask & BOTTOM_RIGHT) > 0) 
     { 
      g.fillArc(x + width - radius, y + height - radius, radius, radius, 270, 90); 
     } 
     else 
     { 
      g.fillRect(x + width - radiusHalf, y + height - radiusHalf, radiusHalf, radiusHalf); 
     } 
     if ((cornerMask & BOTTOM_LEFT) > 0) 
     { 
      g.fillArc(x, y + height - radius, radius, radius, 180, 90); 
     } 
     else 
     { 
      g.fillRect(x, y + height - radiusHalf, radiusHalf, radiusHalf); 
     } 

     g.fillRect(x + radiusHalf, y, width - radius, radiusHalf); 
     g.fillRect(x + radiusHalf, y + height - radiusHalf, width - radius, radiusHalf); 
     g.fillRect(x, y + radiusHalf, radiusHalf, height - radius); 
     g.fillRect(x + width - radiusHalf, y + radiusHalf, radiusHalf, height - radius); 
     g.fillRect(x + radiusHalf, y + radiusHalf, width - radius, height - radius); 
    } 
    if (borderColor != null && !borderColor.equals(fillColor)) 
    { 
     g.setColor(borderColor); 

     // XXX: there are problems with this when using semi-transparent colors + borderSize > 1 
     // XXX: this could be changed to to use ONE draw action using drawShape with GeneralPath.curveTo() 
     // XXX: this then could also be used to FILL the shape (see above) 
     if ((cornerMask & TOP_LEFT) > 0) 
     { 
      g.drawArc(x, y, radius, radius, 90, 90); 
     } 
     else 
     { 
      g.drawLine(x, y, x + radiusHalf, y); 
      g.drawLine(x, y, x, y + radiusHalf); 
     } 
     if ((cornerMask & TOP_RIGHT) > 0) 
     { 
      g.drawArc(x + width - radius, y, radius, radius, 0, 90); 
     } 
     else 
     { 
      g.drawLine(x + width - radiusHalf, y, x + width, y); 
      g.drawLine(x + width, y, x + width, y + radiusHalf); 
     } 
     if ((cornerMask & BOTTOM_RIGHT) > 0) 
     { 
      g.drawArc(x + width - radius, y + height - radius, radius, radius, 270, 90); 
     } 
     else 
     { 
      g.drawLine(x + width - radiusHalf, y + height, x + width, y + height); 
      g.drawLine(x + width, y + height - radiusHalf, x + width, y + height); 
     } 
     if ((cornerMask & BOTTOM_LEFT) > 0) 
     { 
      g.drawArc(x, y + height - radius, radius, radius, 180, 90); 
     } 
     else 
     { 
      g.drawLine(x, y + height, x + radiusHalf, y + height); 
      g.drawLine(x, y + height - radiusHalf, x, y + height); 
     } 

     g.drawLine(x + radiusHalf, y, x + width - radiusHalf, y); // top 
     g.drawLine(x + width, y + radiusHalf, x + width, y + height - radiusHalf); // right 
     g.drawLine(x + radiusHalf, y + height, x + width - radiusHalf, y + height); // bottom 
     g.drawLine(x, y + radiusHalf, x, y + height - radiusHalf); // left 
    } 
} 
संबंधित मुद्दे