~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to laf-widget/src/main/java/org/pushingpixels/lafwidget/contrib/blogofbug/swing/borders/AbstractImageBorder.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * AbstractImageBorder.java
 
3
 *
 
4
 * Created on March 27, 2007, 9:19 AM
 
5
 *
 
6
 * Copyright 2006-2007 Nigel Hughes
 
7
 *
 
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.
 
14
 */
 
15
 
 
16
package org.pushingpixels.lafwidget.contrib.blogofbug.swing.borders;
 
17
 
 
18
import java.awt.*;
 
19
import java.awt.image.BufferedImage;
 
20
import java.net.URL;
 
21
 
 
22
import org.pushingpixels.lafwidget.contrib.blogofbug.utility.ImageUtilities;
 
23
 
 
24
 
 
25
/**
 
26
 *
 
27
 * @author nigel
 
28
 */
 
29
public class AbstractImageBorder {
 
30
    
 
31
  /**
 
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
 
36
   */
 
37
  public AbstractImageBorder(URL imageURL, Insets imageInsets){
 
38
        this.imageInsets = imageInsets;
 
39
        borderImage = ImageUtilities.loadCompatibleImage(imageURL.toString());        
 
40
  }    
 
41
    
 
42
  /** 
 
43
   * Creates a new ImageBorder using the supplied image and the insets
 
44
   * 
 
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
 
48
   */
 
49
  public AbstractImageBorder(BufferedImage borderImage, Insets imageInsets) {
 
50
    this.borderImage = borderImage;
 
51
    this.imageInsets = imageInsets;
 
52
  }
 
53
 
 
54
 
 
55
    protected BufferedImage borderImage;
 
56
 
 
57
 
 
58
    protected Insets imageInsets;
 
59
 
 
60
 
 
61
    /** 
 
62
     * Paints the border around the specified component
 
63
     * 
 
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
 
71
     */
 
72
    public void paintBorder(int compWidth, int compHeight, Graphics g, int x, int y, int width, int height) {
 
73
 
 
74
        Graphics2D g2 = (Graphics2D) g;
 
75
        int imageWidth = borderImage.getWidth();
 
76
        int imageHeight = borderImage.getHeight();
 
77
        
 
78
        //Top-left corner
 
79
        drawSlice(g2,0,0,imageInsets.left,imageInsets.top,0,0);
 
80
        //Top-right corner
 
81
        drawSlice(g2,imageWidth-imageInsets.right,0,imageInsets.right,imageInsets.bottom,
 
82
                compWidth-imageInsets.right,0);
 
83
        //Bottom-left corner
 
84
        drawSlice(g2,0,imageHeight-imageInsets.bottom,imageInsets.left,imageInsets.bottom,0,compHeight-imageInsets.bottom);
 
85
 
 
86
        //Bottom-right corner
 
87
        drawSlice(g2,imageWidth-imageInsets.right,imageHeight-imageInsets.bottom,imageInsets.left,imageInsets.bottom,compWidth-imageInsets.right,compHeight-imageInsets.bottom);
 
88
 
 
89
        //Draw left side
 
90
        g2.drawImage(borderImage,0,imageInsets.top,imageInsets.left,compHeight-imageInsets.bottom,
 
91
                0,imageInsets.top,imageInsets.left,imageHeight-imageInsets.bottom,null);
 
92
 
 
93
        //Draw right side
 
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);
 
96
 
 
97
        //Draw top side
 
98
        g2.drawImage(borderImage,imageInsets.left ,0,compWidth-imageInsets.left,imageInsets.top,
 
99
                imageInsets.left,0,imageWidth-imageInsets.right,imageInsets.top,null);
 
100
        
 
101
        //Draw bottom side
 
102
        g2.drawImage(borderImage,imageInsets.left ,compHeight-imageInsets.bottom,compWidth-imageInsets.left,compHeight,
 
103
                imageInsets.left,imageHeight-imageInsets.bottom,imageWidth-imageInsets.right,imageHeight,null);
 
104
 
 
105
    }
 
106
 
 
107
  
 
108
    /** 
 
109
     * Sets the insets around the edge of the image to be used to cookie cut the image into a border
 
110
     * 
 
111
     * @param insets The edges of the image
 
112
     */
 
113
    public void setInsets(Insets insets){
 
114
        this.imageInsets = insets;
 
115
    }
 
116
    
 
117
    public Insets getImageInsets(){
 
118
        return (Insets) imageInsets.clone();
 
119
    }
 
120
    
 
121
  /**
 
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
 
125
   *
 
126
   * @param g2 The graphics context
 
127
   * @param compWidth width of the target component
 
128
   * @param compHeight height of the target component
 
129
   */
 
130
  public void paintCenter(Graphics2D g2, int compWidth,int compHeight){
 
131
    int imageWidth = borderImage.getWidth();
 
132
    int imageHeight = borderImage.getHeight();
 
133
 
 
134
    //draw center
 
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);       
 
137
  }
 
138
  
 
139
  /**
 
140
   * Draws a slicde from the specified image onto the graphics area
 
141
   * 
 
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
 
149
   */
 
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);
 
153
  }    
 
154
    
 
155
}