package free; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Insets; import java.awt.Point; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.Robot; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.event.MouseInputAdapter; import twaver.TWaverUtil; import com.sun.awt.AWTUtilities; public class ShellWindowBorderPane extends JPanel { private Color windowTitleColor = FreeUtil.DEFAULT_TEXT_COLOR; private Color inactiveRootColor = TWaverUtil.getRandomColor();// new // Color(66, // 187, // 125); private Color activeRootColor = TWaverUtil.getRandomColor();// new Color(94, // 215, 170); private Image activeTitleShadow = null; private Image inactiveTitleShadow = null; private class BorderLabel extends JLabel { protected Image image = null; protected Image inactiveImage = null; public BorderLabel(String imageURL) { this.image = getImage(imageURL, true); this.inactiveImage = getImage(imageURL, false); } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; Window window = getFrameWindow(); if (window.isActive()) { g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this); } else { g2d.drawImage(inactiveImage, 0, 0, getWidth(), getHeight(), this); } } } private static final int TITLE_HEIGHT = 30; private static final int BORDER_SIZE = 7; private Image topImage = getImage("window_border_top.png", true); private Image topImageInactive = getImage("window_border_top.png", false); private JButton btnClose = createWindowButton("window_border_close", "Close"); private JButton btnMax = createWindowButton("window_border_max", "Maximize"); private JButton btnMin = createWindowButton("window_border_min", "Minimize"); private JLabel lbTop = new JLabel() { { this.setLayout(new BorderLayout()); JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0)); buttonPane.setOpaque(false); buttonPane.add(btnMin); buttonPane.add(btnMax); buttonPane.add(btnClose); this.add(buttonPane, BorderLayout.EAST); this.addMouseListener(new MouseAdapter() { private Robot robot = null; private boolean isClickLogo(MouseEvent e) { // check whether click logo image. Rectangle imageBounds = null; int logoWidth = logo.getWidth(null); int logoHeight = logo.getHeight(null); if (isWindowMaxmized()) { imageBounds = new Rectangle(2, 4, logoWidth, logoHeight); } else { imageBounds = new Rectangle(2, 2, logoWidth, logoHeight); } return imageBounds.contains(e.getPoint()); } @Override public void mouseReleased(MouseEvent e) { // popup menu. if (isClickLogo(e)) { if (robot == null) { try { robot = new Robot(); } catch (Exception ex) { ex.printStackTrace(); } } if (robot != null) { // send a "ALT+SPACE" keystroke to popup window // menu. robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_SPACE); robot.keyRelease(KeyEvent.VK_ALT); } } } @Override public void mouseClicked(MouseEvent e) { if (isClickLogo(e)) { if (e.getClickCount() > 1) { System.exit(0); } } } }); } @Override public Dimension getPreferredSize() { return new Dimension(super.getPreferredSize().width, TITLE_HEIGHT); } @Override public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; Frame window = getFrameWindow(); int leadingX = 2; Image image = topImage; if (!window.isActive()) { image = topImageInactive; } if (isWindowMaxmized()) { leadingX = 4; g2d.drawImage(image, 0, -2, getWidth(), getHeight() + 2, this); } else { g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this); } // 1. title shadow. g2d.setFont(FreeUtil.FONT_14_BOLD); if (activeTitleShadow == null) { activeTitleShadow = FreeUtil.createWindowTitleShadowImage(g2d, "xxxx2222222柜员综合业务系统ddd", true); inactiveTitleShadow = FreeUtil.createWindowTitleShadowImage( g2d, "xxxx111111柜员综合业务系统ddd", false); } Image titleShadow = activeTitleShadow; if (!window.isActive()) { titleShadow = inactiveTitleShadow; } int shadowY = (titleShadow.getHeight(null) - TITLE_HEIGHT) / 2; g2d.drawImage(titleShadow, -10, -shadowY, null); // 2. title text. g2d.setColor(windowTitleColor); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawString("xxxx333333333柜员综合业务系统ddd", 25 + leadingX, 17); // 3. draw logo. g.drawImage(logo, leadingX, 4, null); // paint children: buttons. super.paint(g); } }; private JLabel lbBottom = new BorderLabel("window_border_bottom.png") { @Override public Dimension getPreferredSize() { return new Dimension(super.getPreferredSize().width, BORDER_SIZE); } }; private JLabel lbLeft = new BorderLabel("window_border_left.png") { @Override public Dimension getPreferredSize() { return new Dimension(BORDER_SIZE, super.getPreferredSize().height); } }; private JLabel lbRight = new BorderLabel("window_border_right.png") { @Override public Dimension getPreferredSize() { return new Dimension(BORDER_SIZE, super.getPreferredSize().height); } }; private JLabel lbLeftTop = new BorderLabel("window_border_left_top.png") { @Override public Dimension getPreferredSize() { return new Dimension(BORDER_SIZE, TITLE_HEIGHT); } }; private JLabel lbRightTop = new BorderLabel("window_border_right_top.png") { @Override public Dimension getPreferredSize() { return new Dimension(BORDER_SIZE, TITLE_HEIGHT); } }; private JLabel lbLeftBottom = new BorderLabel( "window_border_left_bottom.png") { @Override public Dimension getPreferredSize() { return new Dimension(BORDER_SIZE, BORDER_SIZE); } }; private JLabel lbRightBottom = new BorderLabel( "window_border_right_bottom.png") { @Override public Dimension getPreferredSize() { return new Dimension(BORDER_SIZE, BORDER_SIZE); } }; private Image logo = TWaverUtil.getImage("/free/test/logo.png"); private Point lastPoint = null; private MouseInputAdapter mouseHandler = new MouseInputAdapter() { @Override public void mousePressed(MouseEvent e) { lastPoint = e.getLocationOnScreen(); } @Override public void mouseClicked(MouseEvent e) { handleClick(e); } @Override public void mouseDragged(MouseEvent e) { handleDrag(e); } @Override public void mouseMoved(MouseEvent e) { if (e.getSource() == lbTop) { if (e.getPoint().y < 5 && !isWindowMaxmized()) { lbTop.setCursor(Cursor .getPredefinedCursor(Cursor.N_RESIZE_CURSOR)); } else { lbTop.setCursor(Cursor.getDefaultCursor()); } } } }; public ShellWindowBorderPane() { initSwing(); setupCursor(); } private void initSwing() { this.setLayout(new BorderLayout()); this.setOpaque(false); btnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Frame frame = getFrameWindow(); if (frame instanceof JFrame) { JFrame jframe = (JFrame) frame; int operation = jframe.getDefaultCloseOperation(); if (operation == JFrame.EXIT_ON_CLOSE) { System.exit(0); } if (operation == JFrame.HIDE_ON_CLOSE) { jframe.setVisible(false); } if (operation == JFrame.DISPOSE_ON_CLOSE) { jframe.dispose(); } } } }); btnMax.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { maxWindow(); } }); btnMin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Frame frame = getFrameWindow(); if (frame.getExtendedState() != Frame.ICONIFIED) { frame.setExtendedState(Frame.ICONIFIED); } } }); JPanel topPane = new JPanel(new BorderLayout()); topPane.setOpaque(false); topPane.add(this.lbTop, BorderLayout.CENTER); topPane.add(this.lbLeftTop, BorderLayout.WEST); topPane.add(this.lbRightTop, BorderLayout.EAST); this.add(topPane, BorderLayout.NORTH); JPanel bottomPane = new JPanel(new BorderLayout()); bottomPane.setOpaque(false); bottomPane.add(this.lbBottom, BorderLayout.CENTER); bottomPane.add(this.lbLeftBottom, BorderLayout.WEST); bottomPane.add(this.lbRightBottom, BorderLayout.EAST); this.add(bottomPane, BorderLayout.SOUTH); this.add(this.lbLeft, BorderLayout.WEST); this.add(this.lbRight, BorderLayout.EAST); // setup mouse listeners. this.lbTop.addMouseMotionListener(mouseHandler); this.lbLeftTop.addMouseMotionListener(mouseHandler); this.lbLeftBottom.addMouseMotionListener(mouseHandler); this.lbRightTop.addMouseMotionListener(mouseHandler); this.lbRightBottom.addMouseMotionListener(mouseHandler); this.lbLeft.addMouseMotionListener(mouseHandler); this.lbRight.addMouseMotionListener(mouseHandler); this.lbBottom.addMouseMotionListener(mouseHandler); this.lbTop.addMouseListener(mouseHandler); this.lbLeftTop.addMouseListener(mouseHandler); this.lbLeftBottom.addMouseListener(mouseHandler); this.lbRightTop.addMouseListener(mouseHandler); this.lbRightBottom.addMouseListener(mouseHandler); this.lbLeft.addMouseListener(mouseHandler); this.lbRight.addMouseListener(mouseHandler); this.lbBottom.addMouseListener(mouseHandler); // window maxmized changed, change update cursor. this.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { setupCursor(); } }); } private void setupCursor() { if (isWindowMaxmized()) { this.lbLeftTop.setCursor(Cursor.getDefaultCursor()); this.lbLeftBottom.setCursor(Cursor.getDefaultCursor()); this.lbRightTop.setCursor(Cursor.getDefaultCursor()); this.lbRightBottom.setCursor(Cursor.getDefaultCursor()); this.lbLeft.setCursor(Cursor.getDefaultCursor()); this.lbRight.setCursor(Cursor.getDefaultCursor()); this.lbBottom.setCursor(Cursor.getDefaultCursor()); } else { this.lbLeftTop.setCursor(Cursor .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR)); this.lbLeftBottom.setCursor(Cursor .getPredefinedCursor(Cursor.SW_RESIZE_CURSOR)); this.lbRightTop.setCursor(Cursor .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)); this.lbRightBottom.setCursor(Cursor .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)); this.lbLeft.setCursor(Cursor .getPredefinedCursor(Cursor.W_RESIZE_CURSOR)); this.lbRight.setCursor(Cursor .getPredefinedCursor(Cursor.E_RESIZE_CURSOR)); this.lbBottom.setCursor(Cursor .getPredefinedCursor(Cursor.S_RESIZE_CURSOR)); } } private void handleClick(MouseEvent e) { if (e.getClickCount() > 1) { maxWindow(); } } private void maxWindow() { boolean max = isWindowMaxmized(); this.lbLeft.setVisible(max); this.lbRight.setVisible(max); this.lbBottom.setVisible(max); this.lbLeftTop.setVisible(max); this.lbRightTop.setVisible(max); this.lbLeftBottom.setVisible(max); this.lbRightBottom.setVisible(max); if (max) { getFrameWindow().setExtendedState(Frame.NORMAL); btnMax.setToolTipText("Maximize"); } else { getFrameWindow().setExtendedState(Frame.MAXIMIZED_BOTH); btnMax.setToolTipText("Normal"); } updateMaxButtonIcon(max); } private void handleDrag(MouseEvent e) { Point point = e.getLocationOnScreen(); if (point != null && lastPoint != null) { int xOffset = point.x - lastPoint.x; int yOffset = point.y - lastPoint.y; Frame window = this.getFrameWindow(); if (window.getExtendedState() != Frame.MAXIMIZED_BOTH) { JComponent component = (JComponent) e.getSource(); // if move window. if (component == lbTop) { // if resize top. if (component.getCursor().equals( Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR))) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; y += yOffset; height -= yOffset; window.setBounds(x, y, width, height); } else { // move window. window.setLocation(window.getLocation().x + xOffset, window.getLocation().y + yOffset); } } // if resize left top. if (component == this.lbLeftTop) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; x += xOffset; width -= xOffset; y += yOffset; height -= yOffset; window.setBounds(x, y, width, height); } // if resize left bottom. if (component == this.lbLeftBottom) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; x += xOffset; width -= xOffset; height += yOffset; window.setBounds(x, y, width, height); } // if resize right top. if (component == this.lbRightTop) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; width += xOffset; height -= yOffset; y += yOffset; window.setBounds(x, y, width, height); } // if resize right bottom. if (component == this.lbRightBottom) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; width += xOffset; height += yOffset; window.setBounds(x, y, width, height); } // if resize left. if (component == this.lbLeft) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; x += xOffset; width -= xOffset; window.setBounds(x, y, width, height); } // if resize right. if (component == this.lbRight) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; width += xOffset; window.setBounds(x, y, width, height); } // if resize bottom. if (component == this.lbBottom) { int width = window.getWidth(); int height = window.getHeight(); int x = window.getLocation().x; int y = window.getLocation().y; height += yOffset; window.setBounds(x, y, width, height); } } lastPoint = point; } } private boolean isWindowMaxmized() { Window window = TWaverUtil.getWindowForComponent(this); if (window instanceof Frame) { Frame frame = (Frame) window; return frame.getExtendedState() == Frame.MAXIMIZED_BOTH; } return false; } private Frame getFrameWindow() { Window window = TWaverUtil.getWindowForComponent(this); if (window instanceof Frame) { Frame frame = (Frame) window; return frame; } return null; } private JButton createWindowButton(String imageURL, String tooltip) { ImageIcon icon = FreeUtil.getImageIcon(imageURL + ".png"); ImageIcon iconPressed = FreeUtil .getImageIcon(imageURL + "_pressed.png"); ImageIcon iconRover = FreeUtil.getImageIcon(imageURL + "_rover.png"); JButton button = new JButton(icon); button.setPressedIcon(iconPressed); button.setRolloverIcon(iconRover); button.setVerticalAlignment(SwingConstants.CENTER); button.setOpaque(false); button.setBorder(null); button.setMargin(new Insets(0, 0, 0, 0)); button.setContentAreaFilled(false); button.setFocusPainted(false); button.setRolloverEnabled(true); button.setToolTipText(tooltip); return button; } private void updateMaxButtonIcon(boolean max) { String imageURL = "window_border_reset"; if (max) { imageURL = "window_border_max"; } ImageIcon icon = FreeUtil.getImageIcon(imageURL + ".png"); ImageIcon iconPressed = FreeUtil .getImageIcon(imageURL + "_pressed.png"); ImageIcon iconRover = FreeUtil.getImageIcon(imageURL + "_rover.png"); this.btnMax.setIcon(icon); this.btnMax.setPressedIcon(iconPressed); this.btnMax.setRolloverIcon(iconRover); } private Image getImage(String imageURL, boolean active) { Image image = FreeUtil.getImage(imageURL); if (active) { image = FreeUtil.createDyedImage(image, activeRootColor, true); } else { image = FreeUtil.createDyedImage(image, inactiveRootColor, true); } return image; } public void titleChanged(String title) { activeTitleShadow = null; inactiveTitleShadow = null; } public static void main(String[] args) { try { JFrame jf = new JFrame(); jf.setTitle("xxxx综合业务系统"); jf.setUndecorated(true); com.sun.awt.AWTUtilities.setWindowOpacity(jf, 0.9f); jf.setSize(new Dimension(400, 320)); ShellWindowBorderPane win = new ShellWindowBorderPane(); win.add(new JPanel()); jf.add(win); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }