Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  PictureBorder – A nice Border with images!  (Read 1476 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Posted 2006-01-24 07:39:58 »

This is a very useful extension of the AbstractBorder class I made, and it's excellent for use with pretty GUI's. Swing's included Border's are not really very pretty, remaining colorless and dull. If you want a professional-looking and fun interface, this is a great class to use in your Java app. It lets you use images loaded from your computer as the borders around any component, instead of a line or some text like in Swing's included Borders. For more details on the abilities of this class, read the comments in the class. The code is not complicated at all, most people should be able to understand it regardless of experience. [EDIT] I forgot the imports, oops.[/EDIT]

For an example of the possible prettiness, take a look at this screenshot of what I originally made this class for:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
import javax.swing.border.AbstractBorder;
import javax.swing.ImageIcon;
import java.awt.Insets;

/**
 * PictureBorder is an extension of AbstractBorder that is used to draw images
 * into the insets around a component in a GUI. The images are automatically
 * resized as needed, and you have the ability of using one single image
 * four times or four separate images. Feel free to add or remove anything
 * if you feel it might help you on your project.
 * @author Eli Delventhal, useable by anyone for free, please just credit me
 */

public class PictureBorder extends AbstractBorder
{
   //Each ImageIcon represents a different Image's direction
  private ImageIcon north;
   private ImageIcon south;
   private ImageIcon east;
   private ImageIcon west;
   //The insets control how large the images are when drawn
  private Insets insets = new Insets(25,25,25,25);
   //If topBottom is true, the top and bottom are drawn last,
  //taking up some space of the left and right sides.
  //In the opposite case, they are drawn first.
  private boolean topBottom = true;
   //If constrain is true, the insets are maintained unless
  //manually changed. If it is false, the insets are resized
  //to fit the original image sizes, so they are not warped
  private boolean constrained;
   
   /**
    * This constructor displays the same image, of location str,
    * four times around the component. The insets are resized
    * by default to maintain the same image proportions.
    * @param str The location on the HD of the image
    */

   public PictureBorder(String str)
   {
      north = south = east = west = new ImageIcon(str);
      constrained = false;
   }
   
   /**
    * This constructor displays the same image, of location str,
    * four times around the component. The images are skewed to
    * fit the supplied insets, regardless of window or image size.
    * @param s The location on the HD of the image
    * @param i The insets (border sizes) for the images
    */

   public PictureBorder(String s, Insets i)
   {
      north = south = east = west = new ImageIcon(s);
      insets = i;
      constrained = true;
   }
   
   /**
    * This constructor displays four separate images around the
    * component. The insets are resized by default to maintain
    * the same image proportions.
    * @param n The location on the HD of the northern image
    * @param s The location on the HD of the southern image
    * @param e The location on the HD of the eastern image
    * @param w The location on the HD of the western image
    */

   public PictureBorder(String n, String s, String e, String w)
   {
      north = new ImageIcon(n);
      south = new ImageIcon(s);
      east = new ImageIcon(e);
      west = new ImageIcon(w);
      constrained = false;
   }
   
   /**
    * This constructor displays four separate images around the
    * component. The images are skewed to fit the supplied
    * insets, regardless of window or image size.
    * @param n The location on the HD of the northern image
    * @param s The location on the HD of the southern image
    * @param e The location on the HD of the eastern image
    * @param w The location on the HD of the western image
    * @param i The insets (border sizes) for the images
    */

   public PictureBorder(String n, String s, String e, String w, Insets i)
   {
      north = new ImageIcon(n);
      south = new ImageIcon(s);
      east = new ImageIcon(e);
      west = new ImageIcon(w);
      insets = i;
      constrained = true;
   }
   
   /**
    * If true, then the top and bottom banners will be drawn on last.
    * If false, they will be overlapped by the east and west banners.
    * @param b T/F top and bottom are drawn last.
    */

   public void setDrawTopBottomLast(boolean b)
   {
      topBottom = b;
   }
   
   /**
    * If constrain is true, the insets are maintained unless
    * manually changed. If it is false, the insets are resized
    * to fit the original image sizes, so they are not warped
    * @param b T/F the insets are constrained upon creation
    */

   public void setConstrained(boolean b)
   {
      constrained = b;
   }
   
   /**
    * @return true if top and bottom drawn last (in front), false otherwise
    */

   public boolean drawsTopBottomLast()
   {
      return topBottom;
   }
   
   /**
    * @return true if the insets are constrained on creation, false otherwise
    */

   public boolean isConstrained()
   {
      return constrained;
   }
   
   /**
    * Paints the border for the component. You will never have to call this method,
    * it is used by swing to draw the borders as needed.
    */

   public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
   {
      //Draw the top and bottom on top
     if (topBottom)
      {
         int hi = insets.top + insets.bottom;
         if (!constrained)
         {
            //We use the stretch ratios to determine exactly how much we're shaving off in the end
           insets.top = (int) ((width+0.0)/north.getIconWidth() * north.getIconHeight());
            insets.bottom = (int) ((width+0.0)/south.getIconWidth() * south.getIconHeight());
            hi = insets.top + insets.bottom;
            insets.right = (int) ((height+0.0-hi)/east.getIconHeight() * east.getIconWidth());
            insets.left = (int) ((height+0.0-hi)/west.getIconHeight() * west.getIconWidth());
         }
         //Actually draw the images with the correct dimensions now
        g.drawImage(east.getImage(),x+width-insets.right,y+insets.top,insets.right,height-hi,east.getImageObserver());
         g.drawImage(west.getImage(),x,y+insets.top,insets.left,height-hi,west.getImageObserver());
         g.drawImage(north.getImage(),x,y,width,insets.top,north.getImageObserver());
         g.drawImage(south.getImage(),x,y+height-insets.bottom,width,insets.bottom,south.getImageObserver());
      }
      //Draw the left and right on top
     else
      {
         int wi = insets.left + insets.right;
         if (!constrained)
         {
            //We use the stretch ratios to determine exactly how much we're shaving off in the end
           insets.right = (int) ((height+0.0)/east.getIconHeight() * east.getIconWidth());
            insets.left = (int) ((height+0.0)/west.getIconHeight() * west.getIconWidth());
            wi = insets.left + insets.right;
            insets.top = (int) ((width+0.0-wi)/north.getIconWidth() * north.getIconHeight());
            insets.bottom = (int) ((width+0.0-wi)/south.getIconWidth() * south.getIconHeight());
         }
         //Actually draw the images with the correct dimensions now
        g.drawImage(north.getImage(),x+insets.left,y,width-wi,insets.top,north.getImageObserver());
         g.drawImage(south.getImage(),x+insets.left,y+height-insets.bottom,width-wi,insets.bottom,south.getImageObserver());
         g.drawImage(east.getImage(),x+width-insets.right,y,insets.right,height,east.getImageObserver());
         g.drawImage(west.getImage(),x,y,insets.left,height,west.getImageObserver());
      }
   }
   
   /**
    * Simply returns the insets of the image, used by Swing
    * to determine how much of the component to cut off. You
    * will never have to call this method.
    */

   public Insets getBorderInsets(Component c)
   {
      return insets;
   }
}

See my work:
OTC Software
Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2006-01-24 19:42:58 »

Wow, thats really nice! Thanks Smiley

Kev

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #2 - Posted 2006-01-24 20:09:47 »

Thanks a lot. I'm glad you didn't tell me this existed already as a library function, because I was shocked when I found it wasn't. Smiley

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (208 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!