bevelborder.java

001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 /***
018 * @author Alexander T. Simbirtsev
019 */
020 package javax.swing.border;
021
022 import java.awt.Color;
023 import java.awt.Component;
024 import java.awt.Graphics;
025 import java.awt.Insets;
026
027 import org.apache.harmony.x.swing.internal.nls.Messages;
028
029
030 public class BevelBorder extends AbstractBorder {
031
032 public static final int RAISED = 0;
033 public static final int LOWERED = 1;
034
035 private static final int INSETS_SIZE = 2;
036
037 protected int bevelType;
038
039 protected Color highlightOuter;
040 protected Color highlightInner;
041 protected Color shadowInner;
042 protected Color shadowOuter;
043
044 public BevelBorder(final int bevelType) {
045 this.bevelType = bevelType;
046 }
047
048 public BevelBorder(final int bevelType, final Color highlightOuterColor, final Color highlightInnerColor, final Color shadowOuterColor, final Color shadowInnerColor) {
049 this(bevelType);
050
051 if (highlightOuterColor == null || highlightInnerColor == null ||
052 shadowOuterColor == null || shadowInnerColor == null) {
053 throw new NullPointerException(Messages.getString("swing.68")); //$NON-NLS-1$
054 }
055 highlightOuter = highlightOuterColor;
056 highlightInner = highlightInnerColor;
057 shadowOuter = shadowOuterColor;
058 shadowInner = shadowInnerColor;
059 }
060
061 public BevelBorder(final int bevelType, final Color highlight, final Color shadow) {
062 this(bevelType, highlight, highlight, shadow, shadow);
063 }
064
065 int getInsetSize() {
066 return INSETS_SIZE;
067 }
068
069 public Insets getBorderInsets(final Component c, final Insets insets) {
070 int insetSize = getInsetSize();
071
072 insets.left = insetSize;
073 insets.top = insetSize;
074 insets.bottom = insetSize;
075 insets.right = insetSize;
076
077 return insets;
078 }
079
080 public Insets getBorderInsets(final Component c) {
081 int insetSize = getInsetSize();
082 return new Insets(insetSize, insetSize, insetSize, insetSize);
083 }
084
085 protected void paintRaisedBevel(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
086 Color oldColor = g.getColor();
087 Color color = getShadowOuterColor(c);
088 g.setColor(color);
089 g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
090 g.drawLine(x + width - 1, y, x + width - 1, y + height - 1);
091
092 color = getShadowInnerColor(c);
093 g.setColor(color);
094 g.drawLine(x + 1, y + height - 2, x + width - 2, y + height - 2);
095 g.drawLine(x + width - 2, y + 1, x + width - 2, y + height - 2);
096
097 color = getHighlightOuterColor(c);
098 g.setColor(color);
099 g.drawLine(x + 1, y, x + width - 2, y);
100 g.drawLine(x, y + 1, x, y + height - 2);
101
102 color = getHighlightInnerColor(c);
103 g.setColor(color);
104 g.drawLine(x + 2, y + 1, x + width - 3, y + 1);
105 g.drawLine(x + 1, y + 2, x + 1, y + height - 3);
106 g.setColor(oldColor);
107 }
108
109 protected void paintLoweredBevel(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
110 Color oldColor = g.getColor();
111 Color color = getShadowInnerColor(c);
112 g.setColor(color);
113 g.drawLine(x, y, x + width - 1, y);
114 g.drawLine(x, y, x, y + height - 1);
115
116 color = getShadowOuterColor(c);
117 g.setColor(color);
118 g.drawLine(x + 1, y + 1, x + width - 2, y + 1);
119 g.drawLine(x + 1, y + 1, x + 1, y + height - 2);
120
121 color = getHighlightOuterColor(c);
122 g.setColor(color);
123 g.drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
124 g.drawLine(x + width - 1, y + 1, x + width - 1, y + height - 2);
125
126 color = getHighlightInnerColor(c);
127 g.setColor(color);
128 g.drawLine(x + 2, y + height - 2, x + width - 2, y + height - 2);
129 g.drawLine(x + width - 2, y + 2, x + width - 2, y + height - 3);
130 g.setColor(oldColor);
131 }
132
133 public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width, final int height) {
134 if (bevelType == BevelBorder.LOWERED) {
135 paintLoweredBevel(c, g, x, y, width, height);
136 } else if (bevelType == BevelBorder.RAISED){
137 paintRaisedBevel(c, g, x, y, width, height);
138 }
139 }
140
141 public Color getShadowOuterColor(final Component c) {
142 return (shadowOuter != null) ? shadowOuter : c.getBackground().darker().darker();
143 }
144
145 public Color getShadowInnerColor(final Component c) {
146 return (shadowInner != null) ? shadowInner : c.getBackground().darker();
147 }
148
149 public Color getHighlightOuterColor(final Component c) {
150 return (highlightOuter != null) ? highlightOuter : c.getBackground().brighter().brighter();
151 }
152
153 public Color getHighlightInnerColor(final Component c) {
154 return (highlightInner != null) ? highlightInner : c.getBackground().brighter();
155 }
156
157 public Color getShadowOuterColor() {
158 return shadowOuter;
159 }
160
161 public Color getShadowInnerColor() {
162 return shadowInner;
163 }
164
165 public Color getHighlightOuterColor() {
166 return highlightOuter;
167 }
168
169 public Color getHighlightInnerColor() {
170 return highlightInner;
171 }
172
173 public boolean isBorderOpaque() {
174 return true;
175 }
176
177 public int getBevelType() {
178 return bevelType;
179 }
180
181 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值