Listing 7.1 A Typical Implementation of the Interface Icon (// TestIcon.java) // Shows a class that implements the Icon interface import javax.swing.Icon; import java.awt.Graphics; import java.awt.Color; import java.awt.Component; class TestIcon implements Icon { int width = 20; // set to some default size int height = 20; // set to some default size // Constructor without any arguments public TestIcon() { } // Constructor to specify the icons width and height public TestIcon(int width, int height) { this.width = width; this.height = height; } // Paint the picture public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.red); g.fillOval(x, y, width, height); g.setColor(Color.gray); g.drawOval(x, y, width, height); } // To retrieve the icon width public int getIconWidth() { return width; } // To retrieve the icon height public int getIconHeight() { return height; } } Listing 7.2 JLabel with Labels, Icons, and Constants (TJLabel.java) // Demonstrates the Swing labels, icons, and constants. /* * * */ import java.awt.*; import javax.swing.*; public class TJLabel extends JApplet { Container container = null; Icon icon = new ImageIcon("metal.gif"); public void init() { //1. Get a handle on the JApplet's content pane. container = getContentPane(); //2. Create grid layout and set it for the applet and //assign a new background color. GridLayout grid = new GridLayout(3,3,5,5); container.setLayout(grid); container.setBackground(Color.gray); //3. Invoke the setLabel() method to create and add // labels with suitable text and icon positions. setLabel(JLabel.LEFT, JLabel.TOP); setLabel(JLabel.CENTER, JLabel.TOP); setLabel(JLabel.RIGHT, JLabel.TOP); setLabel(JLabel.LEFT, JLabel.CENTER); setLabel(JLabel.CENTER, JLabel.CENTER); setLabel(JLabel.RIGHT, JLabel.CENTER); setLabel(JLabel.LEFT, JLabel.BOTTOM); setLabel(JLabel.CENTER, JLabel.BOTTOM); setLabel(JLabel.RIGHT, JLabel.BOTTOM); } //4. Create and add labels to the Swing applet. public void setLabel(int horizPos, int vertPos) { JLabel label = new JLabel("Java",JLabel.CENTER); label.setIcon(icon); // Display Java Duke on the label label.setHorizontalTextPosition(horizPos); label.setVerticalTextPosition(vertPos); // Assign new background and foreground colors label.setBackground(Color.white); label.setForeground(Color.black); label.setOpaque(true); // label should not be transparent container.add(label); } } Listing 7.3 Borders Applet Creates One of Every Type of Border (TBorder1.java) /* * * */ import javax.swing.border.*; import javax.swing.*; import java.awt.*; public class TBorder1 extends JApplet{ public void init() { // 1. Create an object of panel to contain a label // and add it to the applet's content pane. LabelPanel panel = new LabelPanel(); getContentPane().add(panel); } } // 2. Panel that contains labels with borders. class LabelPanel extends JPanel { Border border; JLabel label; public LabelPanel() { // 3. Assign grid layout (with 3 rows, 4 columns, and // 5 pixel horizontal and vertical spacing) to panel. setLayout(new GridLayout(3,4,5,5)); // 4. Create a label with an empty border. label = new JLabel("Empty", JLabel.CENTER); label.setOpaque(true); // Create an empty border object with the specified insets: // top = 1; left = 1; bottom = 1; right =1. border = new EmptyBorder(1,1,1,1); // Assign the border to the label. label.setBorder(border); add(label); // 5. Create a label with an etched border. label = new JLabel("Etched", JLabel.CENTER); label.setOpaque(true); // Create a raised and etched border. border = new EtchedBorder(EtchedBorder.RAISED); label.setBorder(border); add(label); // 6. Create a label with an etched border with color. label = new JLabel("Etched&Colored", JLabel.CENTER); label.setOpaque(true); border = new EtchedBorder(EtchedBorder.LOWERED, // Lowered and etched type Color.red, Color.blue); // Hightlight and shadow colors label.setBorder(border); add(label); // 7. Create a label with a bevel border. label = new JLabel("Bevel Up", JLabel.CENTER); label.setOpaque(true); border = new BevelBorder(BevelBorder.RAISED); // Raised bevel type label.setBorder(border); add(label); // 8. Create a label with a lowered bevel border. label = new JLabel("Bevel Down", JLabel.CENTER); label.setOpaque(true); border = new BevelBorder(BevelBorder.LOWERED); // Lowered bevel type label.setBorder(border); add(label); // 9. Create a label with a raised bevel border. label = new JLabel("ColoredBevel", JLabel.CENTER); label.setOpaque(true); border = new BevelBorder(BevelBorder.RAISED, // Raised bevel type Color.gray, Color.yellow); // Hightlight aand shadow colors label.setBorder(border); add(label); // 10. Create a label with a soft bevel border. label = new JLabel("SoftBevel", JLabel.CENTER); label.setOpaque(true); // Create a lowered bevel type border object with softened corners // without pointings border = new SoftBevelBorder(BevelBorder.LOWERED); label.setBorder(border); add(label); // 11. Create a label with a matte border. label = new JLabel("Matte", JLabel.CENTER); label.setOpaque(true); Icon icon = new ImageIcon("cube.gif"); // Create a matte border object with the specified insets of // top = 20, left = 20, bottom = 20; right = 20 and matte icon border = new MatteBorder(20,20,20,20, icon ); label.setBorder(border); add(label); // 12. Create a label with a red line border. label = new JLabel("Line", JLabel.CENTER); label.setOpaque(true); border = new LineBorder(Color.red, // Line color = red 5); // line thickness = 5. label.setBorder(border); add(label); // 13. Create a label with a gray line border. label = new JLabel("Line", JLabel.CENTER); label.setOpaque(true); // Create a line border with gray color and thickness = 1. border = LineBorder.createGrayLineBorder(); label.setBorder(border); add(label); // 14. Create a label with a compound border. label = new JLabel("Compound", JLabel.CENTER); label.setOpaque(true); // Create a compound border with a raised bevel border and a raised // etched border border = new CompoundBorder( new BevelBorder(BevelBorder.RAISED), new EtchedBorder(EtchedBorder.RAISED)); label.setBorder(border); add(label); // 15. Create a label with a titled border with specified // border. label = new JLabel("Titled", JLabel.CENTER); label.setOpaque(true); border = new TitledBorder(new LineBorder(Color.red), "Lined&Titled", // Display title. TitledBorder.DEFAULT_JUSTIFICATION, // Title justification. TitledBorder.CENTER, // Title location. new Font("Sans", Font.BOLD, 16), // Title font. Color.blue); // Title color. label.setBorder(border); add(label); } } Listing 7.4 Custom Borders (TBorder2.java) /* * * */ import javax.swing.*; import java.awt.*; import javax.swing.border.*; public class TBorder2 extends JApplet { public void init() { // 1. Get a handle on the applet's content pane. // and set the grid layout. Container container = this.getContentPane(); container.setLayout(new GridLayout(2,3)); // 2 rows and 3 columns. // 2. Add labels to the applet with decorating // borders created from the class SimpleBorder. // 2(a) Create an array of favorite colors. Color[] colors = {Color.lightGray, Color.yellow, Color.green, Color.white, Color.blue, Color.red}; // 2(b) Create labels and add them to the container. for (int i=0; i<6; i++) { // Total number of labels = 6. JLabel label = new JLabel("Label"+(i+1), JLabel.CENTER); label.setOpaque(true); container.add(label); label.setBorder(new SimpleBorder(15, // Top inset. 15, // Left inset. 15, // Right inset. 15, // Bottom inset. colors[i])); // border color } } } // 3. Define the custom border class called SimpleBorder. class SimpleBorder implements Border { int top; int left; int bottom; int right; Color color = null; public SimpleBorder(int top, int left, int bottom, int right, Color color) { this.top = top; this.left = left; this.bottom = bottom; this.right = right; this.color = color; } public void paintBorder(Component c, // Component that will contain the border. Graphics g, // Graphics context. int x, int y, // x and y coordinates of the painted border. int width, int height) { // Border width and height. // Create insets around the component to draw the border. Insets insets = getBorderInsets(c); // Set the border color. if (color != null) g.setColor(color); // Prepare the border by using 3D Rectangles returned by the method. // g.fill3Drect(). This method takes the argument values as following: g.fill3DRect(0, // x-coordinate 0, // y-coordinate width-insets.right, // width insets.top, // height. true); // Rectangle appears to be raised. // The following methods also work with the arguments as shown in the // previous method call. g.fill3DRect(0, insets.top, insets.left, height-insets.top, true); g.fill3DRect(insets.left, height-insets.bottom, width-insets.left, insets.bottom, true); g.fill3DRect(width-insets.right, 0, insets.right, height-insets.bottom, true); } public Insets getBorderInsets(Component c) { return new Insets(top, left, bottom, right); } public boolean isBorderOpaque() { return true; } }