Skip to content

Instantly share code, notes, and snippets.

@nickgrealy
Last active August 29, 2015 14:05
Show Gist options
  • Save nickgrealy/16901a6428cb79d4f179 to your computer and use it in GitHub Desktop.
Save nickgrealy/16901a6428cb79d4f179 to your computer and use it in GitHub Desktop.
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.InternalFrameEvent;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class PhoneWindow {
public static void main(String[] args) {
new PhoneWindow();
}
public PhoneWindow() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create the inner frame
final JInternalFrame frame2 = new JInternalFrame("My Telephone");
frame2.setClosable(true);
frame2.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame2.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() {
public void internalFrameClosing(InternalFrameEvent e) {
frame.dispose();
}
});
MoveMouseListener mml = new MoveMouseListener(frame);
BasicInternalFrameUI ui = (BasicInternalFrameUI) frame2.getUI();
ui.getNorthPane().addMouseListener(mml);
ui.getNorthPane().addMouseMotionListener(mml);
// add elements to the outer frame
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
JPanel pane = new TranslucentPane();
frame.setContentPane(pane);
frame.setLayout(new BorderLayout());
// add inner frame and phone picture
frame.add(frame2, BorderLayout.CENTER);
frame.add(new JLabel(new ImageIcon(ImageIO.read(getClass()
.getResource("/transparent-phone.png")))), BorderLayout.EAST);
frame.setLocationRelativeTo(null);
frame.setMinimumSize(new Dimension(400, 300));
frame.pack();
// show
frame2.setVisible(true);
frame.setVisible(true);
} catch (Throwable ex) {
ex.printStackTrace();
}
}
});
}
public class TranslucentPane extends JPanel {
public TranslucentPane() {
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0f));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
static class MoveMouseListener implements MouseListener, MouseMotionListener {
JFrame target;
Point start_drag;
Point start_loc;
public MoveMouseListener(JFrame target) {
this.target = target;
}
public static JFrame getFrame(Container target) {
if (target instanceof JFrame) {
return (JFrame) target;
}
return getFrame(target.getParent());
}
Point getScreenLocation(MouseEvent e) {
Point cursor = e.getPoint();
Point target_location = this.target.getLocationOnScreen();
return new Point((int) (target_location.getX() + cursor.getX()),
(int) (target_location.getY() + cursor.getY()));
}
public void mousePressed(MouseEvent e) {
this.start_drag = this.getScreenLocation(e);
this.start_loc = this.getFrame(this.target).getLocation();
}
public void mouseDragged(MouseEvent e) {
Point current = this.getScreenLocation(e);
Point offset = new Point((int) current.getX() - (int) start_drag.getX(),
(int) current.getY() - (int) start_drag.getY());
JFrame frame = this.getFrame(target);
Point new_location = new Point(
(int) (this.start_loc.getX() + offset.getX()), (int) (this.start_loc
.getY() + offset.getY()));
frame.setLocation(new_location);
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment