1   package org.wcb.gui.component.border;
2   
3   import org.wcb.gui.util.UIHelper;
4   
5   import javax.swing.border.AbstractBorder;
6   import java.awt.image.BufferedImage;
7   import java.awt.Component;
8   import java.awt.Graphics;
9   import java.awt.Insets;
10  
11  /**
12   * <small>
13   * <p/>
14   * Copyright (c)  2006  wbogaardt.
15   * This library is free software; you can redistribute it and/or
16   * modify it under the terms of the GNU Lesser General Public
17   * License as published by the Free Software Foundation; either
18   * version 2.1 of the License, or (at your option) any later version.
19   *
20   * This library is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   * Lesser General Public License for more details.
24   *
25   * You should have received a copy of the GNU Lesser General Public
26   * License along with this library; if not, write to the Free Software
27   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
28   * <p/>
29   * $File:  $ <br>
30   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 20, 2006 3:45:22 PM $ <br>
31   * </small>
32   *
33   * @author wbogaardt
34   *         This draws a border that makes a panel look like an open page of a book.
35   */
36  
37  public class BookBorder extends AbstractBorder {
38  
39      private BufferedImage topImg;
40      private BufferedImage bottomImg;
41      private BufferedImage rightImg;
42      private BufferedImage topRightImg;
43      private BufferedImage bottomRightImg;
44      private BufferedImage leftSideImg;
45      private BufferedImage topLeftImg;
46      private BufferedImage bottomLeftImg;
47  
48      /**
49       * Creates a booklike border.
50       */
51      public BookBorder() {
52          bottomImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/bottom.jpg");
53          topImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/top.jpg");
54          rightImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/rightside.jpg");
55          topRightImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/top-right-corner.jpg");
56          bottomRightImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/bottom-right-corner.jpg");
57          leftSideImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/rightpage-west.jpg");
58          bottomLeftImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/rightpage-southwest.jpg");
59          topLeftImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/rightpage-northwest.jpg");
60      }
61  
62      /**
63       * Overrides the component's paint method.
64       * @param c the component.
65       * @param g graphics object ref
66       * @param x position
67       * @param y position
68       * @param width width of component
69       * @param height height of component
70       */
71      public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
72          int imgWidth;
73          int imgHeight;
74          //Draw right side leftside border
75          imgHeight = rightImg.getHeight();
76          imgWidth = rightImg.getWidth();
77          for (int i = y; i < y + height; i += imgHeight) {
78              g.drawImage(rightImg, x + width - imgWidth, i, null);
79              g.drawImage(leftSideImg, 0, i, null);
80          }
81  
82          //Draw top border
83          for (int i = 0; i < width; i += imgWidth) {
84              g.drawImage(topImg, i, 0, null);
85          }
86          //draw bottom border
87           imgHeight = bottomImg.getHeight();
88          imgWidth = bottomImg.getWidth();
89          for (int i = 0; i < width; i += imgWidth) {
90              g.drawImage(bottomImg, i, height - imgHeight, null);
91          }
92  
93  
94          //Draw top right corner
95          imgWidth = topRightImg.getWidth();
96          g.drawImage(topRightImg, x + width - imgWidth, 0, null);
97  
98          //Draw bottom right corner
99          imgHeight = bottomRightImg.getHeight();
100         imgWidth = bottomRightImg.getWidth();
101         g.drawImage(bottomRightImg, x + width - imgWidth, height - imgHeight, null);
102 
103         //Draw top left corner
104         g.drawImage(topLeftImg, 0, 0, null);
105 
106         //draw bottom left corner
107         imgHeight = bottomLeftImg.getHeight();
108         g.drawImage(bottomLeftImg, 0, height - imgHeight, null);
109 
110 /*        NoiseFilter noise = new NoiseFilter();
111         noise.setDistribution(NoiseFilter.GAUSSIAN);
112         noise.setMonochrome(true);*/
113     }
114 
115     /**
116      * Reference to the component insents.
117      * @param c The component.
118      * @return the insents of the image border.
119      */
120     public Insets getBorderInsets(Component c) {
121         return new Insets(topImg.getHeight(), rightImg.getWidth(), bottomImg.getHeight(), rightImg.getWidth());
122     }
123 
124     /**
125      * Sets and lets the user know the border is opaque.
126      * @return always return false
127      */
128     public boolean isBorderOpaque() {
129         return false;
130     }
131 
132 }