~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/ImageBorder.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
 * ImageBorder.java
 
3
 *
 
4
 * Created on January 15, 2007, 6:54 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 javax.swing.border.AbstractBorder;
 
23
 
 
24
/**
 
25
 * ImageBorder takes an image and breaks out the corners and the top, left, right, and bottom
 
26
 * borders stretching them to fill the space around which the border is drawn
 
27
 * @author nigel
 
28
 */
 
29
public class ImageBorder extends AbstractBorder{
 
30
  protected     AbstractImageBorder borderRenderer;
 
31
  boolean           paintBorder = true;
 
32
 
 
33
  /** 
 
34
   * Creates a new ImageBorder using the supplied image and the insets
 
35
   * 
 
36
   * @param borderImage The image to be used as the border
 
37
   * @param imageInsets The insets around the edge of the image that allow the cookie-cut-and-stretch of the image
 
38
   * around the edge of the border
 
39
   */
 
40
  public ImageBorder(BufferedImage borderImage, Insets imageInsets) {
 
41
    borderRenderer= new AbstractImageBorder(borderImage, imageInsets);
 
42
  }
 
43
      
 
44
  
 
45
  /**
 
46
   * Creates a new ImageBofder loading the image from the supplied URL
 
47
   * @param imageURL The location of the image to use
 
48
   * @param imageInsets The insets around the edge of the image that allow the cookie-cut-and-stretch of the image 
 
49
   * around the edge of the border
 
50
   */
 
51
  public ImageBorder(URL imageURL, Insets imageInsets){
 
52
    borderRenderer = new AbstractImageBorder(imageURL,imageInsets);
 
53
  }
 
54
 
 
55
    /** 
 
56
     * Paints the border around the specified component
 
57
     * 
 
58
     * @param c The component to paint the border on 
 
59
     * @param g The graphics context
 
60
     * @param x The x offset
 
61
     * @param y The y offset
 
62
     * @param width The width
 
63
     * @param height The height
 
64
     */
 
65
    @Override
 
66
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
 
67
        if (!paintBorder){
 
68
            return;
 
69
        }
 
70
        borderRenderer.paintBorder(c.getWidth(),c.getHeight(),g,x,y,width,height);
 
71
    }  
 
72
  
 
73
  /** 
 
74
   * Controls wether or not the border is actually painted or not. 
 
75
   *
 
76
   * @param paintBorder If false then will not draw the border. Useful if the border is being used to show a selected item
 
77
   */
 
78
  public void setPaintBorder(boolean paintBorder){
 
79
      this.paintBorder = paintBorder;
 
80
  }
 
81
  
 
82
  /**
 
83
   * Gets the insets of the image back (subtracting from the component size would give you the renderable
 
84
   * area
 
85
   * 
 
86
   * @param c The component to which the border will be applied
 
87
   * @return The insets of the border 
 
88
   */
 
89
  @Override
 
90
  public Insets getBorderInsets(Component c) {
 
91
    return borderRenderer.getImageInsets();
 
92
  }
 
93
  
 
94
  /**
 
95
   * Gets the insets of the image and returns in the in the supplied Insets instance
 
96
   *
 
97
   * @param c The component to which the border will be applied
 
98
   * @param i A pre-created insets object
 
99
   * @return The insets of the border 
 
100
   */
 
101
  @Override
 
102
  public Insets getBorderInsets(Component c, Insets i){
 
103
      Insets imageInsets = borderRenderer.getImageInsets();
 
104
      i.top = imageInsets.top;
 
105
      i.bottom = imageInsets.bottom;
 
106
      i.left = imageInsets.left;
 
107
      i.right = imageInsets.right;
 
108
      return i;
 
109
  }
 
110
 
 
111
    public void paintCenter(Graphics2D g2, Component c) {
 
112
        if (paintBorder){
 
113
            borderRenderer.paintCenter(g2,c.getWidth(),c.getHeight());
 
114
        }
 
115
    }
 
116
 
 
117
 
 
118
 
 
119
}