1 /*
2  * $Id: ShadowBorder.java,v 1.2 2009-11-24 12:00:28 gaudenz Exp $
3  * Copyright (c) 2001-2005, Gaudenz Alder
4  *
5  * All rights reserved.
6  *
7  * This file is licensed under the JGraph software license, a copy of which
8  * will have been provided to you in the file LICENSE at the root of your
9  * installation directory. If you are unable to locate this file please
10  * contact JGraph sales for another copy.
11  */
12 package com.mxgraph.examples.swing.editor;
13 
14 import java.awt.Color;
15 import java.awt.Component;
16 import java.awt.Graphics;
17 import java.awt.Insets;
18 import java.io.Serializable;
19 
20 import javax.swing.border.Border;
21 
22 /**
23  * Border with a drop shadow.
24  */
25 public class ShadowBorder implements Border, Serializable
26 {
27 	/**
28 	 *
29 	 */
30 	private static final long serialVersionUID = 6854989457150641240L;
31 
32 	private Insets insets;
33 
34 	public static ShadowBorder sharedInstance = new ShadowBorder();
35 
ShadowBorder()36 	private ShadowBorder()
37 	{
38 		insets = new Insets(0, 0, 2, 2);
39 	}
40 
getBorderInsets(Component c)41 	public Insets getBorderInsets(Component c)
42 	{
43 		return insets;
44 	}
45 
isBorderOpaque()46 	public boolean isBorderOpaque()
47 	{
48 		return false;
49 	}
50 
paintBorder(Component c, Graphics g, int x, int y, int w, int h)51 	public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)
52 	{
53 		// choose which colors we want to use
54 		Color bg = c.getBackground();
55 
56 		if (c.getParent() != null)
57 		{
58 			bg = c.getParent().getBackground();
59 		}
60 
61 		if (bg != null)
62 		{
63 			Color mid = bg.darker();
64 			Color edge = average(mid, bg);
65 
66 			g.setColor(bg);
67 			g.drawLine(0, h - 2, w, h - 2);
68 			g.drawLine(0, h - 1, w, h - 1);
69 			g.drawLine(w - 2, 0, w - 2, h);
70 			g.drawLine(w - 1, 0, w - 1, h);
71 
72 			// draw the drop-shadow
73 			g.setColor(mid);
74 			g.drawLine(1, h - 2, w - 2, h - 2);
75 			g.drawLine(w - 2, 1, w - 2, h - 2);
76 
77 			g.setColor(edge);
78 			g.drawLine(2, h - 1, w - 2, h - 1);
79 			g.drawLine(w - 1, 2, w - 1, h - 2);
80 		}
81 	}
82 
average(Color c1, Color c2)83 	private static Color average(Color c1, Color c2)
84 	{
85 		int red = c1.getRed() + (c2.getRed() - c1.getRed()) / 2;
86 		int green = c1.getGreen() + (c2.getGreen() - c1.getGreen()) / 2;
87 		int blue = c1.getBlue() + (c2.getBlue() - c1.getBlue()) / 2;
88 		return new Color(red, green, blue);
89 	}
90 
getSharedInstance()91 	public static ShadowBorder getSharedInstance()
92 	{
93 		return sharedInstance;
94 	}
95 }