package com.javaranch.common ; import java.awt.*; import java.awt.event.*; /** contains a collection of static methods related to GUI's (AWT only).

- - - - - - - - - - - - - - - - -

Copyright (c) 1998-2000 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

@author Paul Wheaton */ public class AWT { AWT(){} // just making sure that nobody tries to instantiate this class /** For creating quick GridBagConstraints objects.

*/ public static GridBagConstraints gbc( int x, int y , int wide , int high , double weightx , double weighty ) { GridBagConstraints g = new GridBagConstraints(); g.gridx = x ; g.gridy = y ; g.gridwidth = wide ; g.gridheight = high ; g.weightx = weightx ; g.weighty = weighty ; g.fill = GridBagConstraints.BOTH ; return g ; } /** For creating quick GridBagConstraints objects.

*/ public static GridBagConstraints gbc( int x , int y ) { return gbc( x , y , 1 , 1 , 1 , 1 ); } /** Returns an object that can be used for whitespace in GridLayout.

@return An object that will take up a bit of space.

*/ public static Button blank() { Button b = new Button(); b.setVisible( false ); return b; } /** Given any component, find the closest parent frame.

@param c Any AWT or Swing component.

@return The nearest Frame object that c uses.

*/ public static Frame getFrame( Component c ) { synchronized ( c.getTreeLock() ) { while( c != null && !( c instanceof Frame ) ) { c = c.getParent(); } } return (Frame)c ; } /** Pop up a dialog to pass a message to the user.

@param c Any component.

@param s The message.

*/ public static void tellUser( Component c , String s ) { new QuickDialog( getFrame( c ) , s ); } /** Center any GUI object on the screen.

@param c The component to be centered.

*/ public static void center( Component c ) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension cSize = c.getSize(); c.setLocation( ( screenSize.width - cSize.width ) / 2 , ( screenSize.height - cSize.height ) / 2); } /** Get a rectangle that fits perfectly within another rectangle.

@param r The rectangle that the new rectangle will fit inside of.

@return The new rectangle that fits perfectly within r.

*/ public static Rectangle shrink( Rectangle r ) { return new Rectangle( r.x + 1 , r.y + 1 , r.width - 2 , r.height - 2 ); } /** Draw a beveled rectangle.

@param g A Graphics object that will be drawn on.

@param r The rectangle that defines the outermost edge of the beveled rectangle.

@param Thickness How many lines thick should this beveled rectangle be?

@param Outtie Should the bevel be made to look like a button sticking out (true) or an indent in the canvas (false).

@return The rectangle that fits perfectly within the beveled rectangle.

*/ public static Rectangle drawBeveledRect( Graphics g , Rectangle r , int Thickness , boolean Outtie , Color c ) { g.setColor( c ); while ( Thickness > 0 ) { g.draw3DRect( r.x , r.y , r.width - 1 , r.height - 1 , Outtie ); r = shrink(r); Thickness--; } return r; } /** Draw a beveled rectangle and assumes the color of gray.

@param g A Graphics object that will be drawn on.

@param r The rectangle that defines the outermost edge of the beveled rectangle.

@param Thickness How many lines thick should this beveled rectangle be?

@param Outtie Should the bevel be made to look like a button sticking out (true) or an indent in the canvas (false).

@return The rectangle that fits perfectly within the beveled rectangle.

*/ public static Rectangle drawBeveledRect( Graphics g , Rectangle r , int thickness , boolean outtie ) { return drawBeveledRect( g , r , thickness , outtie , Color.gray ); } // brightness is determined by the sum of the red, green and blue. // the higher the number, the brighter. private static int getBrightSum( Color c ) { return c.getRed() + c.getGreen() + c.getBlue() ; } // the x and y of r specify the coordinates of the upper left outside corner pixel. // the width and height of r specify the number of pixels on the outside edge. /** Draw a beveled rectangle.

@param g A Graphics object that will be drawn on.

@param r The rectangle that defines the outermost edge of the beveled rectangle.

@param topColor The color to be used on the top edge.

@param rightColor The color to be used on the right edge.

@param bottomColor The color to be used on the bottom edge.

@param leftColor The color to be used on the left edge.

@param borderThickness How many lines thick should this beveled rectangle be?

@return The rectangle that fits perfectly within the beveled rectangle.

*/ public static IRect drawBeveledRect( Graphics g , IRect r , Color topColor , Color rightColor , Color bottomColor , Color leftColor , int borderThickness ) { int left = r.getLeft(); int right = r.getRight(); int top = r.getTop(); int bottom = r.getBottom(); int topBright = getBrightSum( topColor ); int bottomBright = getBrightSum( bottomColor ); int leftBright = getBrightSum( leftColor ); int rightBright = getBrightSum( rightColor ); // the top line can be drawn as a solid line without bevels g.setColor( topColor ); for( int i = 0 ; i < borderThickness ; i++ ) { g.drawLine( left , top + i , right , top + i ); } // the right line can be flat at the bottom, but we need to do the bevel at the top g.setColor( rightColor ); // the brighter color dominates at the corners. int dominantFactor = ( topBright > rightBright ) ? 1 : 0 ; for( int i = 0 ; i < borderThickness ; i++ ) { // start from the outside and work our way in g.drawLine( right - i , top + i + dominantFactor , right - i , bottom ); } // the bottom line is beveled at the right and can be flat at the left g.setColor( bottomColor ); dominantFactor = ( rightBright > bottomBright ) ? 1 : 0 ; for( int i = 0 ; i < borderThickness ; i++ ) { // start from the bottom and work our way up g.drawLine( left , bottom - i , right - ( i + dominantFactor ) , bottom - i ); } // the left line is beveled at the top and bottom g.setColor( leftColor ); int topDominantFactor = ( topBright > leftBright ) ? 1 : 0 ; int bottomDominantFactor = ( bottomBright > leftBright ) ? 1 : 0 ; for( int i = 0 ; i < borderThickness ; i++ ) { // start from the left and work our way in g.drawLine( left + i , top + i + topDominantFactor , left + i , bottom - ( i + bottomDominantFactor ) ); } IRect returnVal = new IRect( r ); returnVal.shrink( borderThickness ); return returnVal ; } /** Get the ascent associated with the font used by a component.

*/ public static int ascent( Component c ) { Font f = c.getFont(); return c.getFontMetrics( f ).getAscent(); } /** Get the height associated with the font used by a component.

*/ public static int FontHeight( Component c ) { Font f = c.getFont(); return c.getFontMetrics( f ).getHeight(); } public static void unitTest( TestLog log ) { // xxx needs tests } }