2012-04-09 32 views
5

Tôi đã thử ví dụ sau đây để áp dụng gradient tuyến tính: GradientPaintDemo2D.Java Swing Matte viền với Linear Gradient?

Nó hoạt động tốt, nhưng tôi muốn màu gradient được áp dụng vào viền Java Swing mờ. Tôi cố gắng này:

javax.swing.BorderFactory.createMatteBorder(1, 50, 1, 50, color) 

Nhưng đó chỉ áp dụng một loại Color, không GradientPaint.

Như chúng ta đã biết, GradientPaint chứa hai màu được trộn lẫn hoàn toàn, như được minh họa trong ví dụ được liên kết ở trên. Vậy câu trả lời thay thế cho trường hợp này là gì?

Trả lời

8

Làm thế nào về việc sử dụng mới MatteBorder(int, int, int, int, javax.swing.Icon)

//Another example: 
import java.awt.*; 
import java.awt.geom.*; 
import javax.swing.*; 

public class GradientMatteBorderTest { 
    public JComponent makeUI() { 
    Icon icon = new Icon() { 
     @Override public void paintIcon(Component c, Graphics g, int x, int y) { 
     Graphics2D g2 = (Graphics2D)g.create(); 
     Point2D start = new Point2D.Float(0f, 0f); 
     Point2D end = new Point2D.Float(99f, 0f); 
     float[] dist = {0.0f, 0.5f, 1.0f}; 
     Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN }; 
     g2.setPaint(new LinearGradientPaint(start, end, dist, colors)); 
     g2.fillRect(x, y, 100, 10); 
     g2.dispose(); 
     } 
     @Override public int getIconWidth() { return 100; } 
     @Override public int getIconHeight() { return 10; } 
    }; 
    JLabel label = new JLabel("GradientMatteBorder"); 
    label.setBorder(BorderFactory.createMatteBorder(10,5,10,0,icon)); 
    JPanel p = new JPanel(new BorderLayout()); 
    p.setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); 
    p.add(label, BorderLayout.NORTH); 
    return p; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
     createAndShowGUI(); 
     } 
    }); 
    } 
    public static void createAndShowGUI() { 
    JFrame f = new JFrame(); 
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    f.getContentPane().add(new GradientMatteBorderTest().makeUI()); 
    f.setSize(320, 240); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
    } 
} 
10

Đối với trường hợp cụ thể như vậy, bạn sẽ cần phải tạo đường viền của riêng bạn.
Dưới đây là một ví dụ với lớp biên giới gradient:

public static class GradientBorder implements Border 
{ 
    private Insets margin; 

    public GradientBorder (int top, int left, int bottom, int right) 
    { 
     super(); 
     margin = new Insets (top, left, bottom, right); 
    } 

    public void paintBorder (Component c, Graphics g, int x, int y, int width, int height) 
    { 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setPaint (new GradientPaint (x, y, Color.RED, x + width, y, Color.BLUE)); 

     Area border = new Area (new Rectangle (x, y, width, height)); 
     border.subtract (new Area (new Rectangle (x + margin.left, y + margin.top, 
       width - margin.left - margin.right, height - margin.top - margin.bottom))); 
     g2d.fill (border); 
    } 

    public Insets getBorderInsets (Component c) 
    { 
     return margin; 
    } 

    public boolean isBorderOpaque() 
    { 
     return true; 
    } 
} 

Ofcourse, bạn có thể chỉ định bất kỳ hướng gradient khác, màu sắc e.t.c. Bạn có thể đặt khởi tạo của họ vào constructor (nếu bạn cần). Để sử dụng nó, bạn sẽ chỉ cần phải thiết lập biên giới (trong bất kỳ kế JComponent):

jComponent.setBorder (new GradientBorder (25, 50, 25, 50)); 

Về cơ bản, bạn có thể tạo ra bất kỳ biên giới bạn thích cách này với bất kỳ màu/hoạt hình/hình dạng e.t.c.

Và bằng cách này - phương thức isBorderOpaque() sẽ trả về true, nếu bạn muốn tạo đường viền nửa trong suốt (ví dụ với màu bán trong suốt, có góc tròn và như vậy), nếu không bạn sẽ phải xử lý vấn đề sơn lại thành phần.