Skip to content

Instantly share code, notes, and snippets.

@CalebWhiting
Created February 28, 2014 22:33
Show Gist options
  • Select an option

  • Save CalebWhiting/9281439 to your computer and use it in GitHub Desktop.

Select an option

Save CalebWhiting/9281439 to your computer and use it in GitHub Desktop.
package edu.revtek.javax;
import javax.swing.border.*;
import java.awt.*;
import java.awt.font.*;
import java.util.*;
public class FrameBorder extends AbstractBorder {
@Override
public Insets getBorderInsets(Component c, Insets insets) {
return new Insets(40, 4, 4, 4);
}
@Override
public void paintBorder(Component c, Graphics g1, int x, int y, int width, int height) {
Graphics2D g = (Graphics2D) g1;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Rectangle border = new Rectangle(0, 0, width, height);
g.clip(border);
/**
* Fill Border
*/
g.setColor(new Color(20, 20, 20));
g.fill(border);
/**
* Draw TitleBar
*/
Insets insets = getBorderInsets(c);
int titleHeight = insets.top - insets.bottom;
Rectangle title = new Rectangle(x, y, width, titleHeight);
g.setPaint(new GradientPaint(x, y, new Color(60, 60, 60), x, titleHeight, new Color(40, 40, 40)));
g.fill(title);
/**
* Draw TitleBar Outline
*/
g.setColor(Color.BLACK);
title.setBounds(0, 0, width - 1, titleHeight - 1);
g.draw(title);
/**
* Draw Outline
*/
g.setColor(Color.BLACK);
border.setBounds(0, 0, width - 1, height - 1);
g.draw(border);
/**
* Draw TitleBar Components
*/
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.TRACKING, 0.25f);
g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 13).deriveFont(attributes));
drawString(g, title, "ROBOTEK");
/**
* Dispose
*/
g.dispose();
}
private void drawString(Graphics2D g, Rectangle bounds, String s) {
FontMetrics fm = g.getFontMetrics();
int x = bounds.x;
int y = bounds.y;
x += (bounds.width / 2);
y += (bounds.height / 2);
x -= (fm.stringWidth(s) / 2);
y += (fm.getAscent() / 2);
g.drawString(s, x, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment