关闭

swing自定义弧形边框 CurvedBorder

标签: Swing
425人阅读 评论(0) 收藏 举报
分类:
目录:

java代码:

package cn.cun.border;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.AbstractBorder;

public class CurvedExample extends JPanel {

  public CurvedExample() {
    super(true);

    JSlider mySlider = new JSlider();
    mySlider.setMajorTickSpacing(20);
    mySlider.setMinorTickSpacing(10);
    mySlider.setPaintTicks(true);
    mySlider.setPaintLabels(true);

    CurvedBorder border = new CurvedBorder(10, Color.RED);
    
    mySlider.setBorder(border);

    add(mySlider);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("Custom Curved Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 150);
    frame.setContentPane(new CurvedExample());
    frame.setVisible(true);
  }
}
class CurvedBorder extends AbstractBorder {
  private Color wallColor = Color.gray;

  private int sinkLevel = 10;

  public CurvedBorder() {
  }

  public CurvedBorder(int sinkLevel) {
    this.sinkLevel = sinkLevel;
  }

  public CurvedBorder(Color wall) {
    this.wallColor = wall;
  }

  public CurvedBorder(int sinkLevel, Color wall) {
    this.sinkLevel = sinkLevel;
    this.wallColor = wall;
  }

  public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    g.setColor(getWallColor());

    //  Paint a tall wall around the component
    for (int i = 0; i < sinkLevel; i++) {
      g.drawRoundRect(x + i, y + i, w - i - 1, h - i - 1, sinkLevel - i,
          sinkLevel);
      g.drawRoundRect(x + i, y + i, w - i - 1, h - i - 1, sinkLevel,
          sinkLevel-i);
      g.drawRoundRect(x + i, y, w - i - 1, h - 1, sinkLevel - i,
          sinkLevel);
      g.drawRoundRect(x, y + i, w - 1, h - i - 1, sinkLevel, sinkLevel
          - i);
    }
  }

  public Insets getBorderInsets(Component c) {
    return new Insets(sinkLevel, sinkLevel, sinkLevel, sinkLevel);
  }

  public Insets getBorderInsets(Component c, Insets i) {
    i.left = i.right = i.bottom = i.top = sinkLevel;
    return i;
  }

  public boolean isBorderOpaque() {
    return true;
  }

  public int getSinkLevel() {
    return sinkLevel;
  }

  public Color getWallColor() {
    return wallColor;
  }
}

效果图:

                            ||==========》  《===疑问===》《=========||

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26776次
    • 积分:562
    • 等级:
    • 排名:千里之外
    • 原创:31篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类