2
* AbstractImageBorder.java
4
* Created on March 27, 2007, 9:19 AM
6
* Copyright 2006-2007 Nigel Hughes
8
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9
* in compliance with the License. You may obtain a copy of the License at http://www.apache.org/
10
* licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
13
* governing permissions and limitations under the License.
16
package org.pushingpixels.lafwidget.contrib.blogofbug.swing.borders;
19
import java.awt.image.BufferedImage;
22
import org.pushingpixels.lafwidget.contrib.blogofbug.utility.ImageUtilities;
29
public class AbstractImageBorder {
32
* Creates a new ImageBofder loading the image from the supplied URL
33
* @param imageURL The location of the image to use
34
* @param imageInsets The insets around the edge of the image that allow the cookie-cut-and-stretch of the image
35
* around the edge of the border
37
public AbstractImageBorder(URL imageURL, Insets imageInsets){
38
this.imageInsets = imageInsets;
39
borderImage = ImageUtilities.loadCompatibleImage(imageURL.toString());
43
* Creates a new ImageBorder using the supplied image and the insets
45
* @param borderImage The image to be used as the border
46
* @param imageInsets The insets around the edge of the image that allow the cookie-cut-and-stretch of the image
47
* around the edge of the border
49
public AbstractImageBorder(BufferedImage borderImage, Insets imageInsets) {
50
this.borderImage = borderImage;
51
this.imageInsets = imageInsets;
55
protected BufferedImage borderImage;
58
protected Insets imageInsets;
62
* Paints the border around the specified component
64
* @param compWidth width of the target component
65
* @param compHeight height of the target component
66
* @param g The graphics context
67
* @param x The x offset
68
* @param y The y offset
69
* @param width The width
70
* @param height The height
72
public void paintBorder(int compWidth, int compHeight, Graphics g, int x, int y, int width, int height) {
74
Graphics2D g2 = (Graphics2D) g;
75
int imageWidth = borderImage.getWidth();
76
int imageHeight = borderImage.getHeight();
79
drawSlice(g2,0,0,imageInsets.left,imageInsets.top,0,0);
81
drawSlice(g2,imageWidth-imageInsets.right,0,imageInsets.right,imageInsets.bottom,
82
compWidth-imageInsets.right,0);
84
drawSlice(g2,0,imageHeight-imageInsets.bottom,imageInsets.left,imageInsets.bottom,0,compHeight-imageInsets.bottom);
87
drawSlice(g2,imageWidth-imageInsets.right,imageHeight-imageInsets.bottom,imageInsets.left,imageInsets.bottom,compWidth-imageInsets.right,compHeight-imageInsets.bottom);
90
g2.drawImage(borderImage,0,imageInsets.top,imageInsets.left,compHeight-imageInsets.bottom,
91
0,imageInsets.top,imageInsets.left,imageHeight-imageInsets.bottom,null);
94
g2.drawImage(borderImage,compWidth-imageInsets.right ,imageInsets.top+6,compWidth,compHeight-imageInsets.bottom,
95
imageWidth-imageInsets.right,imageInsets.top,imageWidth,imageHeight-imageInsets.bottom,null);
98
g2.drawImage(borderImage,imageInsets.left ,0,compWidth-imageInsets.left,imageInsets.top,
99
imageInsets.left,0,imageWidth-imageInsets.right,imageInsets.top,null);
102
g2.drawImage(borderImage,imageInsets.left ,compHeight-imageInsets.bottom,compWidth-imageInsets.left,compHeight,
103
imageInsets.left,imageHeight-imageInsets.bottom,imageWidth-imageInsets.right,imageHeight,null);
109
* Sets the insets around the edge of the image to be used to cookie cut the image into a border
111
* @param insets The edges of the image
113
public void setInsets(Insets insets){
114
this.imageInsets = insets;
117
public Insets getImageInsets(){
118
return (Insets) imageInsets.clone();
122
* Paints a stretched version of the center of the image (as the border is drawn
123
* first, then the component paints itself) so that the component can use it in
124
* its own paint if the border lends itself to having a centre area over-painted
126
* @param g2 The graphics context
127
* @param compWidth width of the target component
128
* @param compHeight height of the target component
130
public void paintCenter(Graphics2D g2, int compWidth,int compHeight){
131
int imageWidth = borderImage.getWidth();
132
int imageHeight = borderImage.getHeight();
135
g2.drawImage(borderImage, imageInsets.left,imageInsets.top,compWidth-imageInsets.right,compHeight-imageInsets.bottom,
136
imageInsets.left,imageInsets.top,imageWidth-imageInsets.right,imageHeight-imageInsets.bottom,null);
140
* Draws a slicde from the specified image onto the graphics area
142
* @param g2 The graphics context to draw into
143
* @param sliceX The x-cordinate of the slice
144
* @param sliceY The y-cordinate of the slice
145
* @param sliceWidth The width of the slice
146
* @param sliceHeight The height of the slice
147
* @param destX The target location of the drawn slice
148
* @param destY The target location of the drawn slice
150
private void drawSlice(Graphics2D g2,int sliceX, int sliceY, int sliceWidth, int sliceHeight, int destX, int destY){
151
g2.drawImage(borderImage,destX,destY,destX+sliceWidth,destY+sliceHeight,
152
sliceX,sliceY,sliceX+sliceWidth,sliceY+sliceHeight,null);