/* * [Borders.java] * * Summary: displays a variety of Swing Border types that can be applied to JPanels. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2003-05-30 initial version * 1.1 2005-08-04 * 1.2 2006-02-15 added Fancy Titled Border. removed some stray innocuous code from an example. * plain About button. * 1.3 2006-03-05 reformat with IntelliJ, add Javadoc * 1.4 2008-01-01 add pad and icons. * 1.5 2008-04-06 add build to title, tidy code, fix spelling errors. */ package com.mindprod.borders; import com.mindprod.common18.Build; import com.mindprod.common18.CMPAboutJBox; import com.mindprod.common18.Common18; import com.mindprod.common18.FontFactory; import com.mindprod.common18.HybridJ; import com.mindprod.common18.JEButton; import com.mindprod.common18.VersionCheck; import javax.swing.BorderFactory; import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.border.BevelBorder; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; import java.awt.Choice; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; /** * displays a variety of Swing Border types that can be applied to JPanels. * * @author Roedy Green, Canadian Mind Products * @version 1.5 2008-04-06 add build to title, tidy code, fix spelling errors. * @since 2003-05-30 */ public final class Borders extends JApplet { /** * height of Applet box in pixels. Does not include surrounding frame. */ private static final int APPLET_HEIGHT = 336; /** * Width of Applet box in pixels. */ private static final int APPLET_WIDTH = 590; private static final int FIRST_COPYRIGHT_YEAR = 2003; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2008-04-06"; private static final String TITLE_STRING = "Borders"; private static final String VERSION_STRING = "1.5"; /** * How each type of Border was done. */ private static final String[] hows = { "jPanel.setBorder\n" + "( BorderFactory.createBevelBorder\n" + " ( BevelBorder.LOWERED,\n" + " paleGoldenrod, darkKhaki ) );\n", "jPanel.setBorder\n" + "( BorderFactory.createBevelBorder\n" + " ( BevelBorder.RAISED,\n" + " paleGoldenrod, darkKhaki ) );\n", "jPanel.setBorder\n" + "( BorderFactory.createCompoundBorder\n" + " ( BorderFactory.createMatteBorder\n" + " ( 4, 6, 4, 5, darkOliveGreen ),\n" + " BorderFactory.createMatteBorder\n" + " ( 4, 6, 4, 5, oliveDrab ) ) )\n", "jPanel.setBorder\n" + "( BorderFactory.createEmptyBorder() );\n", "jPanel.setBorder\n" + "( BorderFactory.createEtchedBorder\n" + " ( EtchedBorder.LOWERED, lightSkyBlue, midnightBlue ) );\n", "jPanel.setBorder\n" + "( BorderFactory.createEtchedBorder\n" + " ( EtchedBorder.RAISED, lightSkyBlue, midnightBlue ) );\n", "jPanel.setBorder\n" + "( BorderFactory.createLineBorder( slateBlue ) );\n", "jPanel.setBorder\n" + "( BorderFactory.createMatteBorder\n" + " ( 4, 6, 4, 5, seashell ) );\n", "jPanel.setBorder\n" + "( BorderFactory.createLoweredBevelBorder() );\n", "jPanel.setBorder\n" + "( BorderFactory.createRaisedBevelBorder() );\n", "jPanel.setBorder\n" + "( BorderFactory.createTitledBorder\n" + " ( BorderFactory.createMatteBorder( 4, 6, 4, 5, darkBlue ),\n" + " \"the title\" ) );\n", "jPanel.setBorder\n" + "( new TitledBorder( BorderFactory.createLineBorder( slateBlue ),\n" + " \" the title \",\n" + " TitledBorder.LEADING, TitledBorder.TOP,\n" + " FontFactory.build(\"Dialog\", Font.ITALIC, 16 ),\n" + " oliveDrab ) )\n;", }; /** * Names or the Border types */ private static final String[] names = { "BevelBorder LOWERED", "BevelBorder RAISED", "CompoundBorder", "EmptyBorder", "EtchedBorder LOWERED", "EtchedBorder RAISED", "LineBorder", "MatteBorder", "LoweredBevelBorder", "RaisedBevelBorder", "TitledBorder", "Fancy TitledBorder", }; private static final Color DARK_GREEN = new Color( 0x008000 ); private static final Color darkBlue = new Color( 0x00008b ); private static final Color darkKhaki = new Color( 0xbdb76b ); private static final Color darkOliveGreen = new Color( 0x556b2f ); /** * for titles */ private static final Color FOREGROUND_FOR_TITLE = new Color( 0xdc143c ); private static final Color lightSkyBlue = new Color( 0x87cefa ); private static final Color midnightBlue = new Color( 0x191970 ); private static final Color oliveDrab = new Color( 0x6b8e23 ); private static final Color paleGoldenrod = new Color( 0xeee8aa ); private static final Color seashell = new Color( 0xfff5ee ); private static final Color slateBlue = new Color( 0x6a5acd ); /** * for for titles and About buttons */ private static final Font FONT_FOR_TITLE = FontFactory.build( "Dialog", Font.BOLD, 16 ); /** * for for title second line */ private static final Font FONT_FOR_TITLE2 = FontFactory.build( "Dialog", Font.PLAIN, 14 ); /** * Border objects for the various border types. */ private static Border[] borders; static { buildBorderCollection(); } /** * which type of Border is selected. */ private Choice choice; private JEButton about; /** * how to use the program */ private JLabel instructions; private JLabel title; /** * title, second line */ private JLabel title2; /** * JPanel we decorate with borders. */ private JPanel panel; /** * Source code to create border as a string */ private JTextArea howDone; private JTextArea sampleText; /** * create an array of various Border objects. */ private static void buildBorderCollection() { borders = new Border[] { BorderFactory.createBevelBorder( BevelBorder.LOWERED, paleGoldenrod, darkKhaki ), BorderFactory.createBevelBorder( BevelBorder.RAISED, paleGoldenrod, darkKhaki ), BorderFactory.createCompoundBorder( BorderFactory .createMatteBorder( 4, 6, 4, 5, darkOliveGreen ), BorderFactory .createMatteBorder( 4, 6, 4, 5, oliveDrab ) ), BorderFactory.createEmptyBorder(), BorderFactory.createEtchedBorder( EtchedBorder.LOWERED, lightSkyBlue, midnightBlue ), BorderFactory.createEtchedBorder( EtchedBorder.RAISED, lightSkyBlue, midnightBlue ), BorderFactory.createLineBorder( slateBlue ), BorderFactory.createMatteBorder( 4, 6, 4, 5, seashell ), BorderFactory.createLoweredBevelBorder(), BorderFactory.createRaisedBevelBorder(), BorderFactory.createTitledBorder( BorderFactory.createMatteBorder( 4, 6, 4, 5, darkBlue ), "the title" ), new TitledBorder( BorderFactory.createLineBorder( slateBlue ), " the title ", TitledBorder.LEADING, TitledBorder.TOP, FontFactory.build( "Dialog", Font.ITALIC, 16 ), oliveDrab ), }; } /** * allocate all the components * contentPane content pane of JApplet * * @param contentPane content pane of JApplet */ private void buildComponents( Container contentPane ) { title = new JLabel( TITLE_STRING + " " + VERSION_STRING ); title.setFont( FONT_FOR_TITLE ); title.setForeground( FOREGROUND_FOR_TITLE ); title2 = new JLabel( "released:" + RELEASE_DATE + " build:" + Build.BUILD_NUMBER ); title2.setFont( FONT_FOR_TITLE2 ); title2.setForeground( FOREGROUND_FOR_TITLE ); about = new JEButton( "About" ); about.setToolTipText( "About " + TITLE_STRING + " " + VERSION_STRING ); about.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // open aboutbox frame new CMPAboutJBox( TITLE_STRING, VERSION_STRING, "Teaches how to get various border effects", "using Swing.", "freeware", RELEASE_DATE, FIRST_COPYRIGHT_YEAR, "Roedy Green", "BORDERS", "1.8" ); } } ); choice = new Choice(); for ( final String name : names ) { choice.add( name ); } panel = new JPanel(); sampleText = new JTextArea( "The quick brown fox \njumped over the lazy dog's back" ); sampleText.setMargin( new Insets( 4, 4, 4, 4 ) ); panel.add( sampleText ); howDone = new JTextArea(); howDone.setMargin( new Insets( 4, 4, 4, 4 ) ); howDone.setEditable( false ); // attempt to stop screen from jumping around resizing this component. howDone.setMinimumSize( new Dimension( 406, 124 ) ); instructions = new JLabel( "Select type of Border for the JPanel.", JLabel.CENTER ); instructions.setFont( FontFactory.build( "Dialog", Font.PLAIN, 12 ) ); instructions.setForeground( DARK_GREEN ); instructions.setBackground( Color.white ); layoutComponents( contentPane ); choice.addItemListener( new ItemListener() { /** * User selected a new Border * @param event Tells if selected or deselected. */ public void itemStateChanged( ItemEvent event ) { Object object = event.getSource(); if ( object == choice ) { setSelectedBorder(); } // end if } // end itemStateChanged } // end anonymous class );// end addItemListener line setSelectedBorder(); } /** * layout components in a GridBag * * @param contentPane content pane of JApplet */ private void layoutComponents( Container contentPane ) { // layout // ---0--------1------2-- // 0 title title2 about 0 // 1 choice panel -------1 // 2 howdone ----------- 2 // 3 instructions--------3 // ---0--------1------2-- // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( title, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 10, 10, 5, 5 ), 0, 0 ) ); contentPane.add( title2, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 10, 5, 5, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( about, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets( 10, 5, 5, 10 ), 10, 2 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( choice, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 5, 10, 15, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( panel, new GridBagConstraints( 1, 1, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 5, 5, 5, 10 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( howDone, new GridBagConstraints( 0, 2, 3, 1, 100.0, 100.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 5, 10, 5, 10 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( instructions, new GridBagConstraints( 0, 3, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 5, 10, 10, 10 ), 0, 0 ) ); } /** * set up both the border selected and the description of how it was done. */ private void setSelectedBorder() { final int i = choice.getSelectedIndex(); panel.setBorder( borders[ i ] ); howDone.setText( hows[ i ] ); // make it easier to copy/paste. howDone.requestFocus(); } /** * Test harness * * @param args not used. */ public static void main( String[] args ) { HybridJ.fireup( new Borders(), TITLE_STRING + " " + VERSION_STRING, APPLET_WIDTH, APPLET_HEIGHT ); } /** * Called by the browser or Applet viewer to inform * this Applet that it is being reclaimed and that it should destroy * any resources that it has allocated. */ public void destroy() { about = null; choice = null; howDone = null; instructions = null; panel = null; sampleText = null; title = null; title2 = null; } /** * Called by the browser or Applet viewer to inform * this Applet that it has been loaded into the system. */ @Override public void init() { Container contentPane = this.getContentPane(); if ( !VersionCheck.isJavaVersionOK( 1, 7, 0, contentPane ) ) { // effectively abort return; } Common18.setLaf(); contentPane.setLayout( new GridBagLayout() ); buildComponents( contentPane ); } // end init /** * Applet scrolled onscreen */ public void start() { } /** * Applet scrolled offscreen */ public void stop() { } }