// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/Swing/magercises/Borders/Solution/Borders.java#2 $ /* * Demonstrates Borders. Change the default button * border and create your own. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class Borders extends JFrame { // The initial width and height of the frame private static int WIDTH = 400; private static int HEIGHT = 300; public Borders (String title) { super(title); Container content = getContentPane(); content.setLayout (new GridLayout (2, 3, 5, 5)); JButton b; b = new JButton("One"); b.setBorder (LineBorder.createGrayLineBorder()); content.add (b); b = new JButton("Two"); b.setBorder(new EtchedBorder()); content.add (b); b = new JButton("Three"); b.setBorder(new EmptyBorder(2,2,2,2)); content.add (b); b = new JButton("Four"); b.setBorder(new TitledBorder( LineBorder.createBlackLineBorder(), "Press Me", TitledBorder.CENTER, TitledBorder.TOP, new Font ("Serif", Font.ITALIC, 10), Color.blue)); content.add (b); b = new DoubleBorderedButton( new BevelBorder (BevelBorder.RAISED, Color.blue, Color.yellow), new BevelBorder (BevelBorder.LOWERED, Color.blue, Color.yellow)); b.setText ("Five"); content.add (b); b = new DoubleBorderedButton( new DashedBorder (Color.blue, 10, 6), new DashedBorder (Color.magenta, 3, 3)); b.setText ("Six"); content.add (b); } public static void main (String args[]) { Frame frame = new Borders ("Border Buttons"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); frame.setSize(WIDTH, HEIGHT); frame.setVisible(true); } class DoubleBorderedButton extends JButton { public DoubleBorderedButton (final Border normal, final Border selected) { setBorder (normal); addMouseListener (new MouseAdapter() { public void mousePressed (MouseEvent e) { setBorder (selected); } public void mouseReleased (MouseEvent e) { setBorder (normal); } }); } } class DashedBorder implements Border { int THICKNESS = 2; Color color; int dashWidth; int dashHeight; public DashedBorder () { this (Color.black, 2, 2); } public DashedBorder (Color c, int width, int height) { if (width < 1) { throw new IllegalArgumentException ("Invalid width: " + width); } if (height < 1) { throw new IllegalArgumentException ("Invalid height: " + height); } color = c; dashWidth = width; dashHeight = height; } public void paintBorder (Component c, Graphics g, int x, int y, int width, int height) { Insets insets = getBorderInsets(c); g.setColor (color); int numWide = (int)Math.round(width / dashWidth); int numHigh = (int)Math.round(height / dashHeight); int startPoint; for (int i=0;i<=numWide;i+=2) { startPoint = x + dashWidth * i; g.fillRect (startPoint, y, dashWidth, THICKNESS); g.fillRect (startPoint, y+height-insets.bottom, dashWidth, THICKNESS); } for (int i=0;i<=numHigh;i+=2) { startPoint = x + dashHeight * i; g.fillRect (x, startPoint, THICKNESS, dashHeight); g.fillRect (x+width-insets.right, startPoint, THICKNESS, dashHeight); } } public Insets getBorderInsets(Component c) { return new Insets (THICKNESS, THICKNESS, THICKNESS, THICKNESS); } public boolean isBorderOpaque() { return false; } } }