1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2002 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 import java.awt.*;
25 
26 
27 /**
28  * Provides graphics related utilities.
29  */
30 
31 public class GraphicsUtilities{
32 
33 
34   /**
35    * Calculates the largest size of the given font for which the given string
36    * will fit into the given size. This method uses the default toolkit.
37    */
38 
getMaxFittingFontSize(Graphics g, Font font, String string, Dimension size)39   public static int getMaxFittingFontSize(Graphics g, Font font, String string, Dimension size){
40     return getMaxFittingFontSize(g, font, string, size.width, size.height);
41   }
42 
43 
44 
45   /**
46    * Calculates the largest size of the given font for which the given string
47    * will fit into the given size.
48    */
49 
getMaxFittingFontSize(Graphics g, Font font, String string, int width, int height)50   public static int getMaxFittingFontSize(Graphics g, Font font, String string, int width, int height){
51     int minSize = 0;
52     int maxSize = 288;
53     int curSize = font.getSize();
54 
55     while (maxSize - minSize > 2){
56       FontMetrics fm = g.getFontMetrics(new Font(font.getName(), font.getStyle(), curSize));
57       int fontWidth = fm.stringWidth(string);
58       int fontHeight = fm.getLeading() + fm.getMaxAscent() + fm.getMaxDescent();
59 
60       if ((fontWidth > width) || (fontHeight > height)){
61         maxSize = curSize;
62         curSize = (maxSize + minSize) / 2;
63       }
64       else{
65         minSize = curSize;
66         curSize = (minSize + maxSize) / 2;
67       }
68     }
69 
70     return curSize;
71   }
72 
73 
74 
75 
76   /**
77    * Returns the <code>FontMetrics</code> for the specified <code>Font</code>.
78    */
79 
getFontMetrics(Font font)80   public static FontMetrics getFontMetrics(Font font){
81     return Toolkit.getDefaultToolkit().getFontMetrics(font); // DEPRECATED
82   }
83 
84 
85 
86 
87 
88   /**
89    * Returns <code>true</code> if the first rectangle completely contains the
90    * second one. Returns <code>false</code> otherwise. This method is needed
91    * because there is no Rectangle.contains(Rectangle) method in JDK 1.1
92    * (unlike claimed in the 1.2 documentation).
93    */
94 
contains(Rectangle rect1, Rectangle rect2)95   public static boolean contains(Rectangle rect1, Rectangle rect2){
96     return (rect2.x >= rect1.x) &&
97            (rect2.y >= rect1.y) &&
98            (rect2.x+rect2.width <= rect1.x+rect1.width) &&
99            (rect2.y+rect2.height <= rect1.y+rect1.height);
100   }
101 
102 
103 
104 
105   /**
106    * Returns true if the two specified rectangles intersect. Two rectangles
107    * intersect if their intersection is nonempty. This method is not really
108    * needed because Rectangle.intersects(Rectangle) exists in JDK1.1, but I
109    * still like having it here for symmetry.
110    */
111 
intersect(Rectangle rect1, Rectangle rect2)112   public static boolean intersect(Rectangle rect1, Rectangle rect2){
113     return rect1.intersects(rect2);
114   }
115 
116 
117 
118 }
119