01: /*
02: * Copyright (C) 2004 NNL Technology AB
03: * Visit www.infonode.net for information about InfoNode(R)
04: * products and how to contact NNL Technology AB.
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19: * MA 02111-1307, USA.
20: */
21:
22: // $Id: HighlightPainter.java,v 1.6 2005/04/14 11:59:19 johan Exp $
23: package net.infonode.gui;
24:
25: import net.infonode.util.ColorUtil;
26:
27: import java.awt.*;
28:
29: /**
30: * @author $Author: johan $
31: * @version $Revision: 1.6 $
32: */
33: public class HighlightPainter {
34: private HighlightPainter() {
35: }
36:
37: public static void drawLine(Graphics g, int x1, int y1, int x2,
38: int y2, boolean clockWise, boolean inside,
39: Color highlightColor, Color middleColor, Color shadowColor) {
40: int mul = clockWise ? 1 : -1;
41: int dx = (x2 - x1) * mul;
42: int dy = (y2 - y1) * mul;
43: int l2 = 2 * (dx * dx + dy * dy);
44: int a = dx - dy;
45: Color blendColor = a > 0 ? highlightColor : shadowColor;
46:
47: if (blendColor != null) {
48: g.setColor(ColorUtil.blend(middleColor, blendColor,
49: (float) a * a / l2));
50: int hx = inside ? getHighlightOffsetX(dx, dy) : 0;
51: int hy = inside ? getHighlightOffsetY(dx, dy) : 0;
52: GraphicsUtil.drawOptimizedLine(g, x1 + hx, y1 + hy,
53: x2 + hx, y2 + hy);
54: }
55:
56: }
57:
58: public static float getBlendFactor(int dx, int dy) {
59: int l2 = 2 * (dx * dx + dy * dy);
60: int a = dx - dy;
61: return 1 - (float) a * a / l2;
62: }
63:
64: protected static int getHighlightOffsetX(int deltaX, int deltaY) {
65: return deltaY - deltaX > 0 ? (deltaX + deltaY > 0 ? -1 : 0)
66: : (deltaX + deltaY > 0 ? 0 : 1); //-deltaY > deltaX ? 1 : 0;
67: }
68:
69: protected static int getHighlightOffsetY(int deltaX, int deltaY) {
70: return deltaY - deltaX > 0 ? (deltaX + deltaY > 0 ? 0 : -1)
71: : (deltaX + deltaY > 0 ? 1 : 0); //-deltaY > deltaX ? 1 : 0;
72: }
73:
74: }
|