Created
March 1, 2014 17:48
-
-
Save CalebWhiting/9293941 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package edu.revtek.javax; | |
| import org.w3c.dom.css.*; | |
| import javax.swing.*; | |
| import javax.swing.border.*; | |
| import java.awt.*; | |
| import java.awt.event.*; | |
| import java.util.*; | |
| import java.util.List; | |
| public class FrameBorder extends AbstractBorder implements MouseListener { | |
| private JFrame frame; | |
| private int padding; | |
| private final List<AbstractComponent> components = new LinkedList<>(); | |
| public FrameBorder(JFrame frame) { | |
| this.frame = frame; | |
| frame.addMouseListener(this); | |
| Insets insets = getBorderInsets(null); | |
| padding = Math.max(insets.left, insets.right); | |
| components.add(new Label(Color.GRAY, new Font(Font.SANS_SERIF, Font.BOLD, 12), "Revtek")); | |
| components.add(new HorizontalStrut(this)); | |
| components.add(new Label(Color.GRAY, new Font(Font.SANS_SERIF, Font.BOLD, 12), "v0.1")); | |
| 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, 5, 5, 5); | |
| } | |
| @Override | |
| public void paintBorder(java.awt.Component c, Graphics g1, int x, int y, int width, int height) { | |
| Graphics2D g = (Graphics2D) g1; | |
| Insets insets = getBorderInsets(c); | |
| 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 | |
| */ | |
| int titleHeight = insets.top - insets.bottom; | |
| Rectangle title = new Rectangle(x, y, width, titleHeight); | |
| g.setPaint(new GradientPaint(x, y, new Color(50, 50, 50), x, titleHeight, new Color(40, 40, 40))); | |
| g.fill(title); | |
| drawBevel(g, width, titleHeight, 2); | |
| /** | |
| * 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 | |
| */ | |
| for (AbstractComponent component : components) { | |
| Rectangle bounds = getBounds(component); | |
| component.paint(g, bounds.x, bounds.y, bounds.width, bounds.height); | |
| } | |
| /** | |
| * Dispose | |
| */ | |
| g.dispose(); | |
| } | |
| public Rectangle getBounds (AbstractComponent component) { | |
| Insets insets = getBorderInsets(null); | |
| int index = getComponents().indexOf(component); | |
| int y = ((insets.top - insets.bottom) / 2) - component.getHeight() / 2; | |
| int x = padding * 2; | |
| for (int i = index - 1; i >= 0; i--) { | |
| x += getComponents().get(i).getWidth(); | |
| } | |
| x += (index * padding); | |
| return new Rectangle(x, y, component.getWidth(), component.getHeight()); | |
| } | |
| private static void drawBevel(Graphics2D g, int width, int height, int bevelStroke) { | |
| int offset = bevelStroke / 2; | |
| int bevelWidth = width - bevelStroke; | |
| int bevelHeight = height - bevelStroke; | |
| g.setStroke(new BasicStroke(bevelStroke)); | |
| /** | |
| * Shadow | |
| */ | |
| g.setColor(new Color(25, 25, 25, 150)); | |
| g.drawLine(bevelWidth, offset, bevelWidth, bevelHeight); | |
| g.drawLine(offset, bevelHeight, bevelWidth, bevelHeight); | |
| /** | |
| * Light | |
| */ | |
| g.setColor(new Color(100, 100, 100, 150)); | |
| g.drawLine(offset, offset, offset, bevelHeight); | |
| g.drawLine(offset, offset, bevelWidth, offset); | |
| g.setStroke(new BasicStroke()); | |
| } | |
| public int getPadding() { | |
| return padding; | |
| } | |
| public void setPadding(int padding) { | |
| this.padding = padding; | |
| } | |
| public List<AbstractComponent> getComponents() { | |
| return components; | |
| } | |
| public JFrame getFrame() { | |
| return frame; | |
| } | |
| @Override | |
| public void mouseClicked(MouseEvent e) { | |
| for (AbstractComponent component : components) { | |
| Rectangle bounds = getBounds(component); | |
| if (bounds.contains(e.getPoint())) { | |
| component.clicked(e); | |
| } | |
| } | |
| } | |
| @Override | |
| public void mousePressed(MouseEvent e) { | |
| } | |
| @Override | |
| public void mouseReleased(MouseEvent e) { | |
| } | |
| @Override | |
| public void mouseEntered(MouseEvent e) { | |
| } | |
| @Override | |
| public void mouseExited(MouseEvent e) { | |
| } | |
| private class SystemButton extends AbstractComponent implements ActionListener { | |
| 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(20, 20); | |
| this.type = type; | |
| getListeners().add(this); | |
| } | |
| @Override | |
| public void paint(Graphics g, int x, int y, int width, int height) { | |
| switch (type) { | |
| case TYPE_CLOSE: | |
| g.setColor(new Color(255, 50, 50, 20)); | |
| break; | |
| case TYPE_MAXIMIZE: | |
| g.setColor(new Color(50, 255, 50, 20)); | |
| break; | |
| case TYPE_MINIMIZE: | |
| g.setColor(new Color(50, 50, 255, 20)); | |
| break; | |
| } | |
| g.fillOval(x + 5, y + 5, width - 10, height - 10); | |
| g.setColor(Color.BLACK); | |
| g.drawOval(x + 5, y + 5, width - 10, height - 10); | |
| switch (type) { | |
| case TYPE_CLOSE: | |
| break; | |
| case TYPE_MAXIMIZE: | |
| break; | |
| case TYPE_MINIMIZE: | |
| break; | |
| } | |
| } | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| switch (type) { | |
| case TYPE_CLOSE: | |
| int answer = JOptionPane.showConfirmDialog(frame, | |
| "Are you sure you want to exit?", | |
| "Are you sure?", | |
| JOptionPane.YES_NO_OPTION); | |
| if (answer == JOptionPane.YES_OPTION) { | |
| System.exit(0); | |
| } | |
| break; | |
| case TYPE_MAXIMIZE: | |
| boolean maxed = (frame.getExtendedState() & Frame.MAXIMIZED_BOTH) > 0; | |
| frame.setExtendedState(maxed ? frame.getExtendedState() & ~Frame.MAXIMIZED_BOTH : | |
| frame.getExtendedState() | Frame.MAXIMIZED_BOTH); | |
| // fix overlapping of the task bar | |
| break; | |
| case TYPE_MINIMIZE: | |
| frame.setExtendedState(frame.getExtendedState() | Frame.ICONIFIED); | |
| break; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment