Red Green Border : Border « Swing JFC « Java






Red Green Border

Red Green Border
   
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;

public class RedGreenBorder extends AbstractBorder {
  public boolean isBorderOpaque() {
    return true;
  }

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

  public void paintBorder(Component c, Graphics g, int x, int y, int width,
      int height) {
    Insets insets = getBorderInsets(c);
    Color horizontalColor;
    Color verticalColor;
    if (c.isEnabled()) {
      boolean pressed = false;
      if (c instanceof AbstractButton) {
        ButtonModel model = ((AbstractButton) c).getModel();
        pressed = model.isPressed();
      }
      if (pressed) {
        horizontalColor = Color.red;
        verticalColor = Color.green;
      } else {
        horizontalColor = Color.green;
        verticalColor = Color.red;
      }
    } else {
      horizontalColor = Color.lightGray;
      verticalColor = Color.lightGray;
    }
    g.setColor(horizontalColor);

    g.translate(x, y);

    // top
    g.fillRect(0, 0, width, insets.top);
    // bottom
    g.fillRect(0, height - insets.bottom, width, insets.bottom);

    g.setColor(verticalColor);
    // left
    g.fillRect(0, insets.top, insets.left, height - insets.top  - insets.bottom);
      g.fillRect(width - insets.right, insets.top, insets.right, height- insets.top - insets.bottom);
    g.translate(-x, -y);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("My Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border border = new RedGreenBorder();
    JButton helloButton = new JButton("Hello");
    helloButton.setBorder(border);
    JButton braveButton = new JButton("Brave New");
    braveButton.setBorder(border);
    braveButton.setEnabled(false);
    JButton worldButton = new JButton("World");
    worldButton.setBorder(border);
    Container contentPane = frame.getContentPane();
    contentPane.add(helloButton, BorderLayout.NORTH);
    contentPane.add(braveButton, BorderLayout.CENTER);
    contentPane.add(worldButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}

           
         
    
    
  








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.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