 |
-- Main.aberrant - 24 May 2008
Demonstrates how to use Java2D? to create a custom border.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
/**
*
* CustomBorderDemo - demonstrates how to create a custom border.
*
* @author Collin Fagan
*/
public class CustomBorderDemo extends JPanel {
public CustomBorderDemo() {
setLayout(new GridLayout(0, 2, 10, 10));
JComponent display1 = new JScrollPane(new JTree());
display1.setBorder(new GradientTitleBorder("GradientTitleBorder 1",
Color.RED, Color.YELLOW, Color.WHITE, 40));
add(display1);
JComponent display2 = new JScrollPane(new JTree());
display2.setBorder(new GradientTitleBorder("GradientTitleBorder 2"));
add(display2);
JComponent display3 = new JScrollPane(new JTree());
display3.setBorder(new GradientTitleBorder("GradientTitleBorder 3",
new Color(0x418EDC), new Color(0x6B91B8), Color.WHITE, 18));
add(display3);
JComponent display4 = new JScrollPane(new JTree());
display4.setBorder(BorderFactory.createTitledBorder("TitledBorder 1"));
add(display4);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}
/**
* Start the demo on the Event Dispatch Thread.
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
// turn bold fonts off in metal
UIManager.put("swing.boldMetal", Boolean.FALSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame demoFrame = new JFrame();
demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demoFrame.setContentPane(new CustomBorderDemo());
demoFrame.setSize(400, 400);
demoFrame.setVisible(true);
}
});
}
/**
* Custom border class, paints a titled border using a gradient.
*/
public class GradientTitleBorder implements Border {
private String title;
private int titleHeight;
private Insets insets = new Insets(titleHeight, 0, 0, 0);
private Color primaryColor;
private Color secondaryColor;
private Color fontColor;
private Color shadowColor;
private int indent = 5;
private Font titleFont;
/**
* Constructtor that assumes a title height.
*
* @param title - string to display
* @param primaryColor - first color of gradient
* @param secondaryColor - second color of gradient (lower)
* @param fontColor - color for the font
*/
public GradientTitleBorder(String title, Color primaryColor,
Color secondaryColor, Color fontColor) {
this(title, primaryColor, secondaryColor, fontColor, 30);
}
/**
* Full option constructor
*
* @param title - string to display
* @param primaryColor - first color of gradient
* @param secondaryColor - second color of gradient (lower)
* @param fontColor - color for the font
* @param titleHeight - height of the title bar
*/
public GradientTitleBorder(String title, Color primaryColor,
Color secondaryColor, Color fontColor, int titleHeight) {
this.title = title;
this.titleHeight = titleHeight;
this.insets = new Insets(titleHeight, 2, 2, 2);
this.primaryColor = primaryColor;
this.shadowColor = primaryColor.darker();
this.secondaryColor = secondaryColor;
this.fontColor = fontColor;
this.titleFont = UIManager.getFont("TitledBorder.font").deriveFont(Font.BOLD);
}
/**
* Creates a GradientTitleBorder with default values.
* @param title
*/
public GradientTitleBorder(String title) {
this(title, Color.BLACK, Color.GRAY, Color.WHITE, 30);
}
/**
*
*/
public Insets getBorderInsets(Component c) {
return insets;
}
/**
*
*/
public boolean isBorderOpaque() {
return false;
}
/**
* paint the border, being carefull to stay inside the insets.
*/
public void paintBorder(Component c, Graphics g, int x, int y,
int width, int height) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(x, y, primaryColor, x, y
+ titleHeight, secondaryColor);
g2d.setPaint(gp);
g2d.fillRect(x, y, width, titleHeight);
g2d.setColor(shadowColor);
g2d.drawRect(x, y - 1, width - 1, titleHeight);
g2d.setFont(titleFont);
g2d.setColor(shadowColor);
int titleOffset = (titleHeight / 2) + (c.getFont().getSize() / 2)
- 1;
g2d.drawString(title, x + insets.left + indent + 1, y + titleOffset
+ 1);
g2d.setColor(fontColor);
g2d.drawString(title, x + insets.left + indent, y + titleOffset);
g2d.setColor(shadowColor);
g2d.drawRect(x, y - 1, width - 1, height);
}
}
}
|