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.Graphics;
8   import java.awt.Component;
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 LeftPageBookBorder extends AbstractBorder {
38  
39      private BufferedImage topImg;
40      private BufferedImage bottomImg;
41      private BufferedImage rightImg;
42      private BufferedImage topRightImg;
43      private BufferedImage bottomRightImg;
44  
45  
46      /**
47       * Makes a book border for a left side of the book.
48       */
49      public LeftPageBookBorder() {
50          bottomImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/bottom.jpg");
51          topImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/top.jpg");
52          rightImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/leftpage-east.jpg");
53          topRightImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/leftpage-northeast.jpg");
54          bottomRightImg = (BufferedImage) UIHelper.loadImage("org/wcb/resources/gui/book/leftpage-southeast.jpg");
55      }
56  
57      /**
58       * Overrides the component's paint method.
59       * @param c the component.
60       * @param g graphics object ref
61       * @param x position
62       * @param y position
63       * @param width width of component
64       * @param height height of component
65       */
66      public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
67          int imgWidth = topImg.getWidth();
68          int imgHeight;
69          //Draw right side leftside border
70          imgHeight = rightImg.getHeight();
71          imgWidth = rightImg.getWidth();
72          for (int i = y; i < y + height; i += imgHeight) {
73              g.drawImage(rightImg, x + width - imgWidth, i, null);
74          }
75  
76          //Draw top border
77          for (int i = 0; i < width; i += imgWidth) {
78              g.drawImage(topImg, i, 0, null);
79          }
80          //draw bottom border
81          imgHeight = bottomImg.getHeight();
82          imgWidth = bottomImg.getWidth();
83          for (int i = 0; i < width; i += imgWidth) {
84              g.drawImage(bottomImg, i, height - imgHeight, null);
85          }
86  
87          //Draw top right corner
88          imgWidth = topRightImg.getWidth();
89          g.drawImage(topRightImg, x + width - imgWidth, 0, null);
90  
91          //Draw bottom right corner
92           imgHeight = bottomRightImg.getHeight();
93           imgWidth = bottomRightImg.getWidth();
94           g.drawImage(bottomRightImg, x + width - imgWidth, height - imgHeight, null);
95  
96      }
97  
98      /**
99       * Reference to the component insents.
100      * @param c The component.
101      * @return the insents of the image border.
102      */
103     public Insets getBorderInsets(Component c) {
104         return new Insets(topImg.getHeight(), rightImg.getWidth(), bottomImg.getHeight(), rightImg.getWidth());
105     }
106 
107     /**
108      * Sets and lets the user know the border is opaque.
109      * @return always return false
110      */
111     public boolean isBorderOpaque() {
112         return false;
113     }
114 
115 }