import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class FrameTest extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameTest frame = new FrameTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FrameTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBackground(Color.YELLOW);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

FrameDragListener frameDragListener = new FrameDragListener(this);
addMouseListener(frameDragListener);
addMouseMotionListener(frameDragListener);

JButton btnNewButton = new JButton("Toggle");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (getExtendedState() == MAXIMIZED_BOTH)
setExtendedState(NORMAL);
else {
setMaximizedBounds(getMaximizedBounds());
setExtendedState(MAXIMIZED_BOTH);
System.out.println("rendered bounds => " + getBounds());
}
}
});
btnNewButton.setBounds(21, 21, 74, 34);
contentPane.add(btnNewButton);

JButton btnNewButton_1 = new JButton("X");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
btnNewButton_1.setBounds(104, 21, 43, 34);
contentPane.add(btnNewButton_1);
setUndecorated(true);
}

public static class FrameDragListener extends MouseAdapter {

private final JFrame frame;
private Point mouseDownCompCoords = null;

public FrameDragListener(JFrame frame) {
this.frame = frame;
}

public void mouseReleased(MouseEvent e) {
mouseDownCompCoords = null;
}

public void mousePressed(MouseEvent e) {
mouseDownCompCoords = e.getPoint();
}

public void mouseDragged(MouseEvent e) {
Point currCoords = e.getLocationOnScreen();
frame.setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
}
}

public Rectangle getMaximizedBounds() {

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
Rectangle bounds = this.getGraphicsConfiguration().getBounds();
Rectangle maxBounds = null;
Insets screenInsets = this.getToolkit().getScreenInsets(this.getGraphicsConfiguration());

if (gs[0] == getGraphicsConfiguration().getDevice()) { // main monitor
maxBounds = new Rectangle(screenInsets.left, screenInsets.top,
bounds.width - screenInsets.right - screenInsets.left,
bounds.height - screenInsets.bottom - screenInsets.top);
System.out.println(getGraphicsConfiguration().getDevice().getIDstring() + " bounds " + bounds.width + "x"
+ bounds.height + " ScreenInsets: " + screenInsets);
} else if (gs[0] != getGraphicsConfiguration().getDevice()) { // other monitors
maxBounds = new Rectangle(screenInsets.left, screenInsets.top,
bounds.width - screenInsets.right - screenInsets.left,
bounds.height - screenInsets.bottom - screenInsets.top);
System.out.println(getGraphicsConfiguration().getDevice().getIDstring() + " bounds " + bounds.width + "x"
+ bounds.height + " ScreenInsets: " + screenInsets);
}

else {
maxBounds = null;
}
return maxBounds;
}
} 