I would like to ask for help regarding code below.
Objectives:
I should be able to enlarge the JFrame to full screen whenever I’m dragging it to the top of the screen. Similarly if the JFrame is in full screen mode, whenever I drag it down it should return to the normal mode depends on the minimum size parameters.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DraggableFrame extends JFrame {
private int posX = 0, posY = 0;
private boolean isDraggingDown = false;
public DraggableFrame() {
// Initialize the JFrame
setUndecorated(true);
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Add mouse listener for dragging
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
posX = e.getX();
posY = e.getY();
isDraggingDown = false;
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen() - posX;
int y = e.getYOnScreen() - posY;
// Handle dragging while in full screen
if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
if (y > 0 && !isDraggingDown) {
isDraggingDown = true;
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocation(x, y);
} else if (y <= 0 && !isDraggingDown) {
// If the frame is dragged to the top while maximized, do nothing
return;
}
} else {
setLocation(x, y);
// Check if dragged to the top
if (getY() <= 0) {
// Enlarge window to fullscreen without covering taskbar
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
Rectangle screenBounds = device.getDefaultConfiguration().getBounds();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());
int taskbarHeight = screenInsets.bottom;
setBounds(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height - taskbarHeight);
}
}
}
});
// Restore to normal size if dragged down while in fullscreen
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (getY() == 0 && getExtendedState() == JFrame.MAXIMIZED_BOTH) {
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocationRelativeTo(null);
}
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
DraggableFrame frame = new DraggableFrame();
frame.setVisible(true);
});
}
}
I tried to apply some code that I found all over the internet but it seems none of them worked as I expected.
What I want is the following:
I should be able to enlarge the JFrame to full screen whenever I’m dragging it to the top of the screen. Similarly if the JFrame is in full screen mode, whenever I drag it down it should return to the normal mode depends on the minimum size parameters.
1
The problem is this condition:
// Handle dragging while in full screen
if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
You never set the frames extended state to JFrame.MAXIMIZED_BOTH
, that condition is therefore never true and you never reach to code that would switch the frame back to normal.
Since setting the frame to JFrame.MAXIMIZED_BOTH
obscures the task bar (which it seems you want to avoid) you need another indication when the frame is maximized.
You could do this by adding a bolean field maximized
to your DraggableFrame
and adjust it when necessary:
public class DraggableFrame extends JFrame {
private boolean maximized = false;
// ...
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen() - posX;
int y = e.getYOnScreen() - posY;
// Handle dragging while in full screen
if (maximized) {
if (y > 0 && !isDraggingDown) {
isDraggingDown = true;
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocation(x, y);
maximized = false;
} else if (y <= 0 && !isDraggingDown) {
// If the frame is dragged to the top while maximized, do nothing
return;
}
} else {
setLocation(x, y);
// Check if dragged to the top
if (getY() <= 0) {
// Enlarge window to fullscreen without covering taskbar
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
Rectangle screenBounds = device.getDefaultConfiguration().getBounds();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());
int taskbarHeight = screenInsets.bottom;
setBounds(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height - taskbarHeight);
maximized = true;
}
}
}
});
// Restore to normal size if dragged down while in fullscreen
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (getY() == 0 && maximized) {
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocationRelativeTo(null);
maximized = false;
}
}
});
}
// ...
}
Even better would be to either not use undecorated frames (since these work together with the Window Manager of your OS) or (if you need undecorated frames because you want to make a full-screen game) make the window full screen from the beginning.
3