1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.awt.*; |
|
29 import javax.swing.border.Border; |
|
30 |
|
31 public class DottedBorder implements Border { |
|
32 // |
|
33 // Instance data |
|
34 // |
|
35 |
|
36 private int width; |
|
37 private Color color; |
|
38 |
|
39 // |
|
40 // Constructors |
|
41 // |
|
42 |
|
43 /** |
|
44 * Constructs a {@code DottedBorder} with the given color and width. |
|
45 * |
|
46 * @param color |
|
47 * the color to paint the border, or {@code null} to use the |
|
48 * foreground color of the {@code Component} |
|
49 * |
|
50 * @param width |
|
51 * the width of the border |
|
52 */ |
|
53 public DottedBorder(Color color, int width) { |
|
54 this.color = color; |
|
55 this.width = width; |
|
56 } |
|
57 |
|
58 /** |
|
59 * Constructs a {@code DottedBorder} with the given color and default width |
|
60 * (1). |
|
61 * |
|
62 * @param color |
|
63 * the color to paint the border, or {@code null} to use the |
|
64 * foreground color of the {@code Component} |
|
65 */ |
|
66 public DottedBorder(Color color) { |
|
67 this(color, 1); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Constructs a {@code DottedBorder} with a {@code null} color and given |
|
72 * width. |
|
73 * |
|
74 * @param width |
|
75 * the width of the border |
|
76 */ |
|
77 public DottedBorder(int width) { |
|
78 this(null, width); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Constructs a {@code DottedBorder} with a {@code null} color and default |
|
83 * width (1). |
|
84 */ |
|
85 public DottedBorder() { |
|
86 this(null); |
|
87 } |
|
88 |
|
89 // |
|
90 // Border methods |
|
91 // |
|
92 |
|
93 @Override |
|
94 public Insets getBorderInsets(Component c) { |
|
95 int width = getWidth(); |
|
96 return new Insets(width, width, width, width); |
|
97 } |
|
98 |
|
99 @Override |
|
100 public boolean isBorderOpaque() { |
|
101 return false; |
|
102 } |
|
103 |
|
104 @Override |
|
105 public void paintBorder(Component c, Graphics g, int x, int y, int w, |
|
106 int h) { |
|
107 |
|
108 Color color = getColor(); |
|
109 if (color == null) { |
|
110 color = c.getForeground(); |
|
111 } |
|
112 |
|
113 g.setColor(color); |
|
114 |
|
115 int width2 = width << 1; |
|
116 |
|
117 for (int xOff = width2; xOff < w; xOff += width2) { |
|
118 // Top |
|
119 g.fillRect(x + xOff, y, width, width); |
|
120 |
|
121 // Bottom |
|
122 g.fillRect(x + w - width - xOff, y + h - width, width, width); |
|
123 } |
|
124 |
|
125 for (int yOff = width2; yOff < h; yOff += width2) { |
|
126 // Left |
|
127 g.fillRect(x + w - width, y + yOff, width, width); |
|
128 |
|
129 // Right |
|
130 g.fillRect(x, y + h - width - yOff, width, width); |
|
131 } |
|
132 } |
|
133 |
|
134 // |
|
135 // DottedBorder methods |
|
136 // |
|
137 |
|
138 public int getWidth() { |
|
139 return width; |
|
140 } |
|
141 |
|
142 public Color getColor() { |
|
143 return color; |
|
144 } |
|
145 } |
|