Source Code : Red Green Border

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

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);
  }
}

           
         
    
    
  

Thank with us