Advertisement
Guest User

Untitled

a guest
May 1st, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 KB | None | 0 0
  1. /*
  2. * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package javax.swing.plaf.basic;
  26.  
  27. import javax.swing.*;
  28. import java.awt.Component;
  29. import java.awt.Color;
  30. import java.awt.Dimension;
  31. import java.awt.Font;
  32. import java.awt.FontMetrics;
  33. import java.awt.Graphics;
  34. import java.awt.Insets;
  35. import java.awt.Rectangle;
  36. import java.awt.Toolkit;
  37. import java.awt.event.KeyEvent;
  38. import java.awt.event.InputEvent;
  39.  
  40. import sun.swing.SwingUtilities2;
  41.  
  42.  
  43. /*
  44. * @author Hans Muller
  45. */
  46.  
  47. public class BasicGraphicsUtils2
  48. {
  49.  
  50. private static final Insets GROOVE_INSETS = new Insets(2, 2, 2, 2);
  51. private static final Insets ETCHED_INSETS = new Insets(2, 2, 2, 2);
  52.  
  53. public static void drawEtchedRect(Graphics g, int x, int y, int w, int h,
  54. Color shadow, Color darkShadow,
  55. Color highlight, Color lightHighlight)
  56. {
  57. Color oldColor = g.getColor(); // Make no net change to g
  58. g.translate(x, y);
  59.  
  60. g.setColor(shadow);
  61. g.drawLine(0, 0, w-1, 0); // outer border, top
  62. g.drawLine(0, 1, 0, h-2); // outer border, left
  63.  
  64. g.setColor(darkShadow);
  65. g.drawLine(1, 1, w-3, 1); // inner border, top
  66. g.drawLine(1, 2, 1, h-3); // inner border, left
  67.  
  68. g.setColor(lightHighlight);
  69. g.drawLine(w-1, 0, w-1, h-1); // outer border, bottom
  70. g.drawLine(0, h-1, w-1, h-1); // outer border, right
  71.  
  72. g.setColor(highlight);
  73. g.drawLine(w-2, 1, w-2, h-3); // inner border, right
  74. g.drawLine(1, h-2, w-2, h-2); // inner border, bottom
  75.  
  76. g.translate(-x, -y);
  77. g.setColor(oldColor);
  78. }
  79.  
  80.  
  81. /**
  82. * Returns the amount of space taken up by a border drawn by
  83. * <code>drawEtchedRect()</code>
  84. *
  85. * @return the inset of an etched rect
  86. */
  87. public static Insets getEtchedInsets() {
  88. return ETCHED_INSETS;
  89. }
  90.  
  91.  
  92. public static void drawGroove(Graphics g, int x, int y, int w, int h,
  93. Color shadow, Color highlight)
  94. {
  95. Color oldColor = g.getColor(); // Make no net change to g
  96. g.translate(x, y);
  97.  
  98. g.setColor(shadow);
  99. g.drawRect(0, 0, w-2, h-2);
  100.  
  101. g.setColor(highlight);
  102. g.drawLine(1, h-3, 1, 1);
  103. g.drawLine(1, 1, w-3, 1);
  104.  
  105. g.drawLine(0, h-1, w-1, h-1);
  106. g.drawLine(w-1, h-1, w-1, 0);
  107.  
  108. g.translate(-x, -y);
  109. g.setColor(oldColor);
  110. }
  111.  
  112. /**
  113. * Returns the amount of space taken up by a border drawn by
  114. * <code>drawGroove()</code>
  115. *
  116. * @return the inset of a groove border
  117. */
  118. public static Insets getGrooveInsets() {
  119. return GROOVE_INSETS;
  120. }
  121.  
  122.  
  123. public static void drawBezel(Graphics g, int x, int y, int w, int h,
  124. boolean isPressed, boolean isDefault,
  125. Color shadow, Color darkShadow,
  126. Color highlight, Color lightHighlight)
  127. {
  128. Color oldColor = g.getColor(); // Make no net change to g
  129. g.translate(x, y);
  130.  
  131. if (isPressed && isDefault) {
  132. g.setColor(darkShadow);
  133. g.drawRect(0, 0, w - 1, h - 1);
  134. g.setColor(shadow);
  135. g.drawRect(1, 1, w - 3, h - 3);
  136. } else if (isPressed) {
  137. drawLoweredBezel(g, x, y, w, h,
  138. shadow, darkShadow, highlight, lightHighlight);
  139. } else if (isDefault) {
  140. g.setColor(darkShadow);
  141. g.drawRect(0, 0, w-1, h-1);
  142.  
  143. g.setColor(lightHighlight);
  144. g.drawLine(1, 1, 1, h-3);
  145. g.drawLine(2, 1, w-3, 1);
  146.  
  147. g.setColor(highlight);
  148. g.drawLine(2, 2, 2, h-4);
  149. g.drawLine(3, 2, w-4, 2);
  150.  
  151. g.setColor(shadow);
  152. g.drawLine(2, h-3, w-3, h-3);
  153. g.drawLine(w-3, 2, w-3, h-4);
  154.  
  155. g.setColor(darkShadow);
  156. g.drawLine(1, h-2, w-2, h-2);
  157. g.drawLine(w-2, h-2, w-2, 1);
  158. } else {
  159. g.setColor(lightHighlight);
  160. g.drawLine(0, 0, 0, h-1);
  161. g.drawLine(1, 0, w-2, 0);
  162.  
  163. g.setColor(highlight);
  164. g.drawLine(1, 1, 1, h-3);
  165. g.drawLine(2, 1, w-3, 1);
  166.  
  167. g.setColor(shadow);
  168. g.drawLine(1, h-2, w-2, h-2);
  169. g.drawLine(w-2, 1, w-2, h-3);
  170.  
  171. g.setColor(darkShadow);
  172. g.drawLine(0, h-1, w-1, h-1);
  173. g.drawLine(w-1, h-1, w-1, 0);
  174. }
  175. g.translate(-x, -y);
  176. g.setColor(oldColor);
  177. }
  178.  
  179. public static void drawLoweredBezel(Graphics g, int x, int y, int w, int h,
  180. Color shadow, Color darkShadow,
  181. Color highlight, Color lightHighlight) {
  182. g.setColor(darkShadow);
  183. g.drawLine(0, 0, 0, h-1);
  184. g.drawLine(1, 0, w-2, 0);
  185.  
  186. g.setColor(shadow);
  187. g.drawLine(1, 1, 1, h-2);
  188. g.drawLine(1, 1, w-3, 1);
  189.  
  190. g.setColor(lightHighlight);
  191. g.drawLine(0, h-1, w-1, h-1);
  192. g.drawLine(w-1, h-1, w-1, 0);
  193.  
  194. g.setColor(highlight);
  195. g.drawLine(1, h-2, w-2, h-2);
  196. g.drawLine(w-2, h-2, w-2, 1);
  197. }
  198.  
  199.  
  200. /** Draw a string with the graphics <code>g</code> at location (x,y)
  201. * just like <code>g.drawString</code> would.
  202. * The first occurrence of <code>underlineChar</code>
  203. * in text will be underlined. The matching algorithm is
  204. * not case sensitive.
  205. */
  206. public static void drawString(Graphics g,String text,int underlinedChar,int x,int y) {
  207. int index=-1;
  208.  
  209. if (underlinedChar != '\0') {
  210. char uc = Character.toUpperCase((char)underlinedChar);
  211. char lc = Character.toLowerCase((char)underlinedChar);
  212. int uci = text.indexOf(uc);
  213. int lci = text.indexOf(lc);
  214.  
  215. if(uci == -1) {
  216. index = lci;
  217. }
  218. else if(lci == -1) {
  219. index = uci;
  220. }
  221. else {
  222. index = (lci < uci) ? lci : uci;
  223. }
  224. }
  225. drawStringUnderlineCharAt(g, text, index, x, y);
  226. }
  227.  
  228. /**
  229. * Draw a string with the graphics <code>g</code> at location
  230. * (<code>x</code>, <code>y</code>)
  231. * just like <code>g.drawString</code> would.
  232. * The character at index <code>underlinedIndex</code>
  233. * in text will be underlined. If <code>index</code> is beyond the
  234. * bounds of <code>text</code> (including &lt; 0), nothing will be
  235. * underlined.
  236. *
  237. * @param g Graphics to draw with
  238. * @param text String to draw
  239. * @param underlinedIndex Index of character in text to underline
  240. * @param x x coordinate to draw at
  241. * @param y y coordinate to draw at
  242. * @since 1.4
  243. */
  244. public static void drawStringUnderlineCharAt(Graphics g, String text,
  245. int underlinedIndex, int x,int y) {
  246. SwingUtilities2.drawStringUnderlineCharAt(null, g, text,
  247. underlinedIndex, x, y);
  248. }
  249.  
  250. public static void drawDashedRect(Graphics g,int x,int y,int width,int height) {
  251. int vx,vy;
  252.  
  253. // draw upper and lower horizontal dashes
  254. for (vx = x; vx < (x + width); vx+=2) {
  255. g.fillRect(vx, y, 1, 1);
  256. g.fillRect(vx, y + height-1, 1, 1);
  257. }
  258.  
  259. // draw left and right vertical dashes
  260. for (vy = y; vy < (y + height); vy+=2) {
  261. g.fillRect(x, vy, 1, 1);
  262. g.fillRect(x+width-1, vy, 1, 1);
  263. }
  264. }
  265.  
  266. public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
  267. {
  268. if(b.getComponentCount() > 0) {
  269. return null;
  270. }
  271.  
  272. Icon icon = b.getIcon();
  273. String text = b.getText();
  274.  
  275. Font font = b.getFont();
  276. FontMetrics fm = b.getFontMetrics(font);
  277.  
  278. Rectangle iconR = new Rectangle();
  279. Rectangle textR = new Rectangle();
  280. Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
  281.  
  282. SwingUtilities.layoutCompoundLabel(
  283. b, fm, text, icon,
  284. b.getVerticalAlignment(), b.getHorizontalAlignment(),
  285. b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  286. viewR, iconR, textR, (text == null ? 0 : textIconGap)
  287. );
  288.  
  289. /* The preferred size of the button is the size of
  290. * the text and icon rectangles plus the buttons insets.
  291. */
  292.  
  293. Rectangle r = iconR.union(textR);
  294.  
  295. Insets insets = b.getInsets();
  296. r.width += insets.left + insets.right;
  297. r.height += insets.top + insets.bottom;
  298.  
  299. return r.getSize();
  300. }
  301.  
  302. /*
  303. * Convenience function for determining ComponentOrientation. Helps us
  304. * avoid having Munge directives throughout the code.
  305. */
  306. static boolean isLeftToRight( Component c ) {
  307. return c.getComponentOrientation().isLeftToRight();
  308. }
  309.  
  310. static boolean isMenuShortcutKeyDown(InputEvent event) {
  311. return (event.getModifiers() &
  312. Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
  313. }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement