Icon MatteBorder : Border « Swing JFC « Java






Icon MatteBorder

Icon MatteBorder
   

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class IconMatteBorder {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Icon Matted Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon diamondIcon = new DiamondIcon(Color.red, true, 10, 10);
    Border diamondBorder = new MatteBorder(10, 10, 10, 10, diamondIcon);
    JButton diamondButton = new JButton("diamonds");
    diamondButton.setBorder(diamondBorder);
    Icon diamondIcon2 = new ImageIcon("diamond.gif");
    Border diamondBorder2 = new MatteBorder(36, 40, 36, 40, diamondIcon2);
    JButton diamondButton2 = new JButton("diamonds2");
    diamondButton2.setBorder(diamondBorder2);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(2, 1));
    contentPane.add(diamondButton);
    contentPane.add(diamondButton2);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

class DiamondIcon implements Icon {
  private Color color;

  private boolean selected;

  private int width;

  private int height;

  private Polygon poly;

  private static final int DEFAULT_WIDTH = 10;

  private static final int DEFAULT_HEIGHT = 10;

  public DiamondIcon(Color color) {
    this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected) {
    this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected, int width, int height) {
    this.color = color;
    this.selected = selected;
    this.width = width;
    this.height = height;
    initPolygon();
  }

  private void initPolygon() {
    poly = new Polygon();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    poly.addPoint(0, halfHeight);
    poly.addPoint(halfWidth, 0);
    poly.addPoint(width, halfHeight);
    poly.addPoint(halfWidth, height);
  }

  public int getIconHeight() {
    return height;
  }

  public int getIconWidth() {
    return width;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(color);
    g.translate(x, y);
    if (selected) {
      g.fillPolygon(poly);
    } else {
      g.drawPolygon(poly);
    }
    g.translate(-x, -y);
  }
}

           
         
    
    
  








Related examples in the same category

1.Border Demo Border Demo
2.Different Swing bordersDifferent Swing borders
3.demonstrate the custom CurvedBorder classdemonstrate the custom CurvedBorder class
4.An example of the MatteBorder classAn example of the MatteBorder class
5.A simple demonstration of the LineBorder class built with rounded cornersA simple demonstration of the LineBorder class built with rounded corners
6.An example of using a titled border on a labelAn example of using a titled border on a label
7.Borders with a BevelBorder used on JLabels as a highlightBorders with a BevelBorder used on JLabels as a highlight
8.MatteBorder DemoMatteBorder Demo
9.TitledBorder DemoTitledBorder Demo
10.Custom Border SampleCustom Border Sample
11.How to create the borderHow to create the border
12.Solid border button border 3D borderSolid border button border 3D border
13.Oval borderOval border
14.Border of all kindsBorder of all kinds
15.EtchedBorderEtchedBorder
16.Red Green BorderRed Green Border
17.TitledBorderTitledBorder
18.TitledBorder 3TitledBorder 3
19.BevelBorder 2BevelBorder 2
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