001: /*
002: * $Id: JGraphShadowBorder.java,v 1.3 2005/11/25 15:53:11 david Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * This file is licensed under the JGraph software license, a copy of which
008: * will have been provided to you in the file LICENSE at the root of your
009: * installation directory. If you are unable to locate this file please
010: * contact JGraph sales for another copy.
011: */
012: package com.jgraph.example;
013:
014: import java.awt.Color;
015: import java.awt.Component;
016: import java.awt.Graphics;
017: import java.awt.Insets;
018: import java.io.Serializable;
019:
020: import javax.swing.border.Border;
021:
022: /**
023: * Example of a shadowed border
024: */
025: public class JGraphShadowBorder implements Border, Serializable {
026: /**
027: * Inset for groups
028: */
029: protected Insets insets;
030:
031: /**
032: * Shared class instance
033: */
034: public static JGraphShadowBorder sharedInstance = new JGraphShadowBorder();
035:
036: /**
037: * Creates a new shadow border with default dimensions
038: *
039: */
040: private JGraphShadowBorder() {
041: insets = new Insets(1, 1, 3, 3);
042: }
043:
044: /**
045: * @return the <code>insets</code> value
046: */
047: public Insets getBorderInsets(Component c) {
048: return insets;
049: }
050:
051: /**
052: * @return whether not the border is opaque
053: */
054: public boolean isBorderOpaque() {
055: // we'll be filling in our own background.
056: return true;
057: }
058:
059: /**
060: * The paint method for this border
061: */
062: public void paintBorder(Component c, Graphics g, int x, int y,
063: int w, int h) {
064: // choose which colors we want to use
065: Color bg = c.getBackground();
066: if (c.getParent() != null)
067: bg = c.getParent().getBackground();
068: Color mid = bg.darker();
069: Color rect = mid.darker();
070: Color edge = average(mid, bg);
071:
072: // fill in the corners with the parent-background
073: // so it looks see-through
074: g.setColor(bg);
075: g.fillRect(0, h - 3, 3, 3);
076: g.fillRect(w - 3, 0, 3, 3);
077: g.fillRect(w - 3, h - 3, 3, 3);
078:
079: // draw the outline
080: g.setColor(rect);
081: g.drawRect(0, 0, w - 3, h - 3);
082:
083: // draw the drop-shadow
084: g.setColor(mid);
085: g.drawLine(1, h - 2, w - 2, h - 2);
086: g.drawLine(w - 2, 1, w - 2, h - 2);
087:
088: g.setColor(edge);
089: g.drawLine(2, h - 1, w - 2, h - 1);
090: g.drawLine(w - 1, 2, w - 1, h - 2);
091: }
092:
093: /**
094: * Returns the average color between the two provided
095: * @param c1 the first color
096: * @param c2 the second color
097: * @return the average of the input colors
098: */
099: private static Color average(Color c1, Color c2) {
100: int red = c1.getRed() + (c2.getRed() - c1.getRed()) / 2;
101: int green = c1.getGreen() + (c2.getGreen() - c1.getGreen()) / 2;
102: int blue = c1.getBlue() + (c2.getBlue() - c1.getBlue()) / 2;
103: return new Color(red, green, blue);
104: }
105:
106: /**
107: * @return the shared instance of this class
108: */
109: public static JGraphShadowBorder getSharedInstance() {
110: return sharedInstance;
111: }
112: }
|