demonstrate the custom CurvedBorder class : Border « Swing JFC « Java






demonstrate the custom CurvedBorder class

demonstrate the custom CurvedBorder class
   
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.darkGray);
    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;
  }
}
           
         
    
    
  








Related examples in the same category

1.Border Demo Border Demo
2.Different Swing bordersDifferent Swing borders
3.An example of the MatteBorder classAn example of the MatteBorder class
4.A simple demonstration of the LineBorder class built with rounded cornersA simple demonstration of the LineBorder class built with rounded corners
5.An example of using a titled border on a labelAn example of using a titled border on a label
6.Borders with a BevelBorder used on JLabels as a highlightBorders with a BevelBorder used on JLabels as a highlight
7.MatteBorder DemoMatteBorder Demo
8.TitledBorder DemoTitledBorder Demo
9.Custom Border SampleCustom Border Sample
10.How to create the borderHow to create the border
11.Solid border button border 3D borderSolid border button border 3D border
12.Oval borderOval border
13.Border of all kindsBorder of all kinds
14.EtchedBorderEtchedBorder
15.Red Green BorderRed Green Border
16.TitledBorderTitledBorder
17.TitledBorder 3TitledBorder 3
18.BevelBorder 2BevelBorder 2
19.Icon MatteBorderIcon MatteBorder
20.Soft BevelBorderSoft BevelBorder
21.Another TitledBorder 1Another TitledBorder 1
22.BevelBorderBevelBorder
23.EmptyBorderEmptyBorder
24.LineBorder DemoLineBorder Demo
25.CompoundBorder DemoCompoundBorder Demo
26.Adding a Title to a Border
27.Create a title border from another border
28.Change border text Justification alignment style
29.Bottom Border
30.Right border
31.Rectangle border
32.Set title position
33.Flex Border