Skip to content

Instantly share code, notes, and snippets.

@CalebWhiting
Created February 28, 2014 23:36
Show Gist options
  • Select an option

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

Select an option

Save CalebWhiting/9282234 to your computer and use it in GitHub Desktop.
package edu.revtek.javax;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.*;
import java.util.List;
public class FrameBorder extends AbstractBorder {
private JFrame frame;
private int padding;
private List<AbstractComponent> components = new LinkedList<>();
public FrameBorder(JFrame frame) {
this.frame = frame;
padding = getBorderInsets(null).left;
components.add(new Label(new Font(Font.SANS_SERIF, Font.PLAIN, 13), "ROBOTEK"));
components.add(new HorizontalStrut(this));
components.add(new SystemButton(SystemButton.TYPE_MINIMIZE));
components.add(new SystemButton(SystemButton.TYPE_MAXIMIZE));
components.add(new SystemButton(SystemButton.TYPE_CLOSE));
}
@Override
public Insets getBorderInsets(java.awt.Component c, Insets insets) {
return new Insets(40, 4, 4, 4);
}
@Override
public void paintBorder(java.awt.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
*/
int x1 = x + padding * 2, y1 = padding;
for (AbstractComponent component : components) {
component.paint(g, x1, y1, component.getWidth(), component.getHeight());
x1 += component.getWidth() + padding;
}
/**
* 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);
}
public int getPadding() {
return padding;
}
public void setPadding(int padding) {
this.padding = padding;
}
public List<AbstractComponent> getComponents() {
return components;
}
public void setComponents(List<AbstractComponent> components) {
this.components = components;
}
public JFrame getFrame() {
return frame;
}
private static class SystemButton extends AbstractComponent {
private static final int TYPE_CLOSE = 0;
private static final int TYPE_MAXIMIZE = 1;
private static final int TYPE_MINIMIZE = 2;
private final int type;
public SystemButton(int type) {
super(25, 25);
this.type = type;
}
@Override
public void paint(Graphics g, int x, int y, int width, int height) {
g.setColor(new Color(255, 255, 255, 100));
g.fillRoundRect(x, y, width, height, 10, 10);
switch (type) {
case TYPE_CLOSE:
break;
case TYPE_MAXIMIZE:
break;
case TYPE_MINIMIZE:
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment