So I am trying to make an iterative program to show the chaos game with sierpinski's triangle. You start at (0,0) and randomly go half-way to either (0, 0), (1, sqrt(3)) or (2, 0). Repeating leaves a fractal pattern. My code in java would roughly be:
public class Sierpinski {
public static void main(String[] args) {
double x = 0;
double y = 0;
int n = 0;
while(true) {
// point on at (x,y) - this is what I need help with
// generates random number from 0 to 2
n = (int)(3 * Math.random())
// x and y randomly go halfway to one point
x += ((n == 1) ? 1 : 0) + ((n == 2) ? 2: 0);
y += ((n == 1) ? Math.sqrt(3) : 0);
x /= 2;
y /= 2;
}
}
}
How would I implement a graph with bounds 0 to 2 in x and y direction that displays these points at each iteration?
Thanks
By the way, it's a nice project ;)
To make a window in Java and draw graphics, you need two things: A JFrame and a Canvas.
JFrame is the frame of your app, just like when you are opening a a Windows app.
Canvas is a surface where we can draw graphics on it. It has a width and an height
in pixels.
To simplify things, I made a Window class that manage both of them. Here is the code:
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Canvas;
import java.awt.Color;
public class Window
{
//width and height of the canvas
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
JFrame frame;
Canvas canvas;
public Window()
{
JFrame frame = new JFrame();
canvas = new Canvas();
canvas.setSize(WIDTH, HEIGHT);
//We add the canvas inside of the frame
frame.add(canvas);
//make the frame to fit the size of the canvas
frame.pack();
//click on the X button to close the app
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set the app in the middle of the screen
frame.setLocationRelativeTo(null);
frame.setTitle("Sierpinski");
frame.setVisible(true);
}
//We call this method from your code
public void paint(double x, double y)
{
/*
Since your numbers range is between 0 and 2,
We need to adapt it to the size of the canvas.
The result would be random coordinates on the canvas.
*/
int coordX = (int)(x / 2 * WIDTH);
/*
Because in Java Y axe is reversed, we need to convert into its reversed value on the screen. Ex:
pixel (0,0) => pixel (0,599)
pixel (0,10) => pixel (0,589)
*/
int coordY = HEIGHT - ((int)(y / 2 * HEIGHT) + 1);
/*
Graphics is like a paintbrush for a specific object.
We are asking the canvas to give us his paintbrush.
*/
Graphics g = canvas.getGraphics();
//Try to execute the line below!
//g.setColor(Color.BLUE);
/*
Draw a rectangle of width and height of 1 pixel.
*/
g.fillRect(coordX, coordY, 1, 1);
}
}
finally, we need to create this Window object within your code, and call the paint method:
public class Sierpinski {
public static void main(String[] args) {
Window window = new Window();
double x = 0;
double y = 0;
int n = 0;
while(true) {
// point on at (x,y) - this is what I need help with
// generates random number from 0 to 2
n = (int)(3 * Math.random());
// x and y randomly go halfway to one point
x += ((n == 1) ? 1 : 0) + ((n == 2) ? 2: 0);
y += ((n == 1) ? Math.sqrt(3) : 0);
x /= 2;
y /= 2;
window.paint(x, y);
}
}
}
You should get a result like this:
The result expected
There is much you can do with it. You can check official documentation on Oracle web site or look at some tutorials on Youtube.
Canvas documentation
Graphics documentation
JFrame documentation
Have fun!
How can one get the screen resolution (width x height) in pixels?
I am using a JFrame and the java swing methods.
You can get the screen size with the Toolkit.getScreenSize() method.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
On a multi-monitor configuration you should use this :
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
If you want to get the screen resolution in DPI you'll have to use the getScreenResolution() method on Toolkit.
Resources :
javadoc - Toolkit.getScreenSize()
Java bug 5100801- Toolkit.getScreenSize() does not return the correct dimension on multimon, linux
This code will enumerate the graphics devices on the system (if multiple monitors are installed), and you can use that information to determine monitor affinity or automatic placement (some systems use a little side monitor for real-time displays while an app is running in the background, and such a monitor can be identified by size, screen colors, etc.):
// Test if each monitor will support my app's window
// Iterate through each monitor and see what size each is
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
Dimension mySize = new Dimension(myWidth, myHeight);
Dimension maxSize = new Dimension(minRequiredWidth, minRequiredHeight);
for (int i = 0; i < gs.length; i++)
{
DisplayMode dm = gs[i].getDisplayMode();
if (dm.getWidth() > maxSize.getWidth() && dm.getHeight() > maxSize.getHeight())
{ // Update the max size found on this monitor
maxSize.setSize(dm.getWidth(), dm.getHeight());
}
// Do test if it will work here
}
This call will give you the information you want.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Here's some functional code (Java 8) which returns the x position of the right most edge of the right most screen. If no screens are found, then it returns 0.
GraphicsDevice devices[];
devices = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getScreenDevices();
return Stream.
of(devices).
map(GraphicsDevice::getDefaultConfiguration).
map(GraphicsConfiguration::getBounds).
mapToInt(bounds -> bounds.x + bounds.width).
max().
orElse(0);
Here are links to the JavaDoc.
GraphicsEnvironment.getLocalGraphicsEnvironment()
GraphicsEnvironment.getScreenDevices()
GraphicsDevice.getDefaultConfiguration()
GraphicsConfiguration.getBounds()
These three functions return the screen size in Java. This code accounts for multi-monitor setups and task bars. The included functions are: getScreenInsets(), getScreenWorkingArea(), and getScreenTotalArea().
Code:
/**
* getScreenInsets, This returns the insets of the screen, which are defined by any task bars
* that have been set up by the user. This function accounts for multi-monitor setups. If a
* window is supplied, then the the monitor that contains the window will be used. If a window
* is not supplied, then the primary monitor will be used.
*/
static public Insets getScreenInsets(Window windowOrNull) {
Insets insets;
if (windowOrNull == null) {
insets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration());
} else {
insets = windowOrNull.getToolkit().getScreenInsets(
windowOrNull.getGraphicsConfiguration());
}
return insets;
}
/**
* getScreenWorkingArea, This returns the working area of the screen. (The working area excludes
* any task bars.) This function accounts for multi-monitor setups. If a window is supplied,
* then the the monitor that contains the window will be used. If a window is not supplied, then
* the primary monitor will be used.
*/
static public Rectangle getScreenWorkingArea(Window windowOrNull) {
Insets insets;
Rectangle bounds;
if (windowOrNull == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
insets = Toolkit.getDefaultToolkit().getScreenInsets(ge.getDefaultScreenDevice()
.getDefaultConfiguration());
bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
} else {
GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
insets = windowOrNull.getToolkit().getScreenInsets(gc);
bounds = gc.getBounds();
}
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
return bounds;
}
/**
* getScreenTotalArea, This returns the total area of the screen. (The total area includes any
* task bars.) This function accounts for multi-monitor setups. If a window is supplied, then
* the the monitor that contains the window will be used. If a window is not supplied, then the
* primary monitor will be used.
*/
static public Rectangle getScreenTotalArea(Window windowOrNull) {
Rectangle bounds;
if (windowOrNull == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
} else {
GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
bounds = gc.getBounds();
}
return bounds;
}
This is the resolution of the screen that the given component is currently assigned (something like most part of the root window is visible on that screen).
public Rectangle getCurrentScreenBounds(Component component) {
return component.getGraphicsConfiguration().getBounds();
}
Usage:
Rectangle currentScreen = getCurrentScreenBounds(frameOrWhateverComponent);
int currentScreenWidth = currentScreen.width // current screen width
int currentScreenHeight = currentScreen.height // current screen height
// absolute coordinate of current screen > 0 if left of this screen are further screens
int xOfCurrentScreen = currentScreen.x
If you want to respect toolbars, etc. you'll need to calculate with this, too:
GraphicsConfiguration gc = component.getGraphicsConfiguration();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
Unfortunately Toolkit.getDefaultToolkit() does not help if you have multiple displays, and on Windows it also reports scaled values if you have changed font setting "Scale and Layout" from 100%. For example at 150% font scale my 1920x1080 screen is reported as 1280x720 which (unhelpfully) changes the resolution my app uses.
You can access all details of the screen devices under:
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
// Windows scaled sizes (eg 1280x720 for my case at 150% scaling)
Rectangle bounds = devices[nnn].getDefaultConfiguration().getBounds();
// Display sizes (same as above at 100% scale, 1920x1080 for my case)
DisplayMode dm = devices[nnn].getDefaultConfiguration().getDevice().getDisplayMode();
Rectangle orig = new Rectangle((int)bounds.getX(), (int)bounds.getY(), dm.getWidth(), dm.getHeight());
I use this method which reads the default display modes of each GraphicsDevice to access the original screen position+dimensions, and returns set of rectangles sorted in left->right X position order per screen:
/** Get actual screen display sizes, ignores Windows font scaling, sort left to right */
public static List<Rectangle> getDisplays() {
return Arrays.stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
.map(GraphicsDevice::getDefaultConfiguration)
// For scaled sizes use .map(GraphicsConfiguration::getBounds) instead of:
.map(c -> {
var dm = c.getDevice().getDisplayMode();
var bounds = c.getBounds();
return new Rectangle((int)bounds.getX(), (int)bounds.getY(), dm.getWidth(), dm.getHeight());
})
.sorted(Comparator.comparing(Rectangle::getX))
.toList();
}
The above code runs under Windows and WSL. If you wish to have a version which returns the scaled values, just switch the commented out line above.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
framemain.setSize((int)width,(int)height);
framemain.setResizable(true);
framemain.setExtendedState(JFrame.MAXIMIZED_BOTH);
Here is a snippet of code I often use. It returns the full available screen area (even on multi-monitor setups) while retaining the native monitor positions.
public static Rectangle getMaximumScreenBounds() {
int minx=0, miny=0, maxx=0, maxy=0;
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
for(GraphicsDevice device : environment.getScreenDevices()){
Rectangle bounds = device.getDefaultConfiguration().getBounds();
minx = Math.min(minx, bounds.x);
miny = Math.min(miny, bounds.y);
maxx = Math.max(maxx, bounds.x+bounds.width);
maxy = Math.max(maxy, bounds.y+bounds.height);
}
return new Rectangle(minx, miny, maxx-minx, maxy-miny);
}
On a computer with two full-HD monitors, where the left one is set as the main monitor (in Windows settings), the function returns
java.awt.Rectangle[x=0,y=0,width=3840,height=1080]
On the same setup, but with the right monitor set as the main monitor, the function returns
java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080]
int resolution =Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(resolution);
There's many answers but I still feel they're not adequate enough, my approach computes global variables related to the screen size once and also using a single loop of all the monitors:
public final class ScreenArea {
public static final Rectangle RECTANGLE;
public static final int
LEFT, RIGHT,
TOP, BOTTOM,
MIN_WIDTH, MAX_WIDTH,
MIN_HEIGHT, MAX_HEIGHT,
TOTAL_WIDTH, TOTAL_HEIGHT;
static {
// Initialise local vars
int left, right, top, bottom, minWidth, maxWidth, minHeight, maxHeight;
left = top = minWidth = minHeight = Integer.MAX_VALUE;
right = bottom = maxWidth = maxHeight = Integer.MIN_VALUE;
// In a single loop process all bounds
Rectangle bounds;
for (GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
bounds = device.getDefaultConfiguration().getBounds();
if (left > bounds.x)
left = bounds.x;
if (right < bounds.x + bounds.width)
right = bounds.x + bounds.width;
if (top > bounds.y)
top = bounds.y;
if (bottom < bounds.y + bounds.height)
bottom = bounds.y + bounds.height;
if (minWidth > bounds.width)
minWidth = bounds.width;
if (maxWidth < bounds.width)
maxWidth = bounds.width;
if (minHeight > bounds.height)
minHeight = bounds.height;
if (maxHeight < bounds.height)
maxHeight = bounds.height;
}
TOTAL_WIDTH = right - left;
TOTAL_HEIGHT = bottom - top;
RECTANGLE = new Rectangle(TOTAL_WIDTH, TOTAL_HEIGHT);
// Transfer local to immutable global vars
LEFT = left; RIGHT = right; TOP = top; BOTTOM = bottom;
MIN_WIDTH = minWidth; MAX_WIDTH = maxWidth;
MIN_HEIGHT = minHeight; MAX_HEIGHT = maxHeight;
}
}
Then you can use anytime as is just like this :
System.out.printf("LEFT=%d, ", ScreenArea.LEFT);
System.out.printf("RIGHT=%d%n", ScreenArea.RIGHT);
System.out.printf("TOP=%d, ", ScreenArea.TOP);
System.out.printf("BOTTOM=%d%n", ScreenArea.BOTTOM);
System.out.printf("MIN_WIDTH=%d, ", ScreenArea.MIN_WIDTH);
System.out.printf("MAX_WIDTH=%d%n", ScreenArea.MAX_WIDTH);
System.out.printf("MIN_HEIGHT=%d, ", ScreenArea.MIN_HEIGHT);
System.out.printf("MAX_HEIGHT=%d%n", ScreenArea.MAX_HEIGHT);
System.out.printf("SCREEN_AREA=%s%n", ScreenArea.RECTANGLE);
Which on my dual monitor setup it prints :
LEFT=0, RIGHT=3840
TOP=0, BOTTOM=1080
MIN_WIDTH=1920, MAX_WIDTH=1920
MIN_HEIGHT=1080, MAX_HEIGHT=1080
SCREEN_AREA=java.awt.Rectangle[x=0,y=0,width=3840,height=1080]
int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(""+screenResolution);
This question already has answers here:
Java Toolkit Getting Second screen size
(4 answers)
Closed 7 years ago.
I seem to struggle to get the size of more than one monitor in Java;
So I made a little window that is supposed to display a screen's dimensions, it works fine for my primary monitor, now I would like to be able to determine the size of the monitor it's on, I used getLocation() to know where my JFrame is, but I don't know how to get the size of that monitor, i can only get the primary one, or even their total size.
You need to get down into the GraphicsEnvironment which will give you access to all GraphicsDevice available on the system.
Essentially from there, you need loop through each GraphicsDevice and test to see if the window is within the bounds of the a given GraphicsDevice
The fun part, is what to do if the window spans across multiple screens...
public static GraphicsDevice getGraphicsDevice(Component comp) {
GraphicsDevice device = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
if (comp != null && comp.isVisible()) {
Rectangle parentBounds = comp.getBounds();
/*
* If the component is not a window, we need to find its location on the
* screen...
*/
if (!(comp instanceof Window)) {
Point p = new Point(0, 0);
SwingUtilities.convertPointToScreen(p, comp);
parentBounds.setLocation(p);
}
// Get all the devices which the window intersects (ie the window might expand across multiple screens)
for (GraphicsDevice gd : lstGDs) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screenBounds = gc.getBounds();
if (screenBounds.intersects(parentBounds)) {
lstDevices.add(gd);
}
}
// If there is only one device listed, return it...
// Otherwise, if there is more then one device, find the device
// which the window is "mostly" on
if (lstDevices.size() == 1) {
device = lstDevices.get(0);
} else if (lstDevices.size() > 1) {
GraphicsDevice gdMost = null;
float maxArea = 0;
for (GraphicsDevice gd : lstDevices) {
int width = 0;
int height = 0;
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
Rectangle2D intBounds = bounds.createIntersection(parentBounds);
float perArea = (float) ((intBounds.getWidth() * intBounds.getHeight()) / (parentBounds.width * parentBounds.height));
if (perArea > maxArea) {
maxArea = perArea;
gdMost = gd;
}
}
if (gdMost != null) {
device = gdMost;
}
}
}
return device;
}
I am trying to move a JFrame around in Windows using 5 buttons (North, East, South, West, and Centre) At the moment all the current code is in place and it works when using;
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==northButton)
{
setLocation(500,500);
}
} //works
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== northButton)
{
setLocation(north);
}
} //doesn't work
However as part of the task I need to use the Java Toolkit to getScreenSize width and height and using calculations to work out the boundaries of the screen and send 'north' to setLocation() (like above). However, using this method it throws an error "No suitable method found" I'm unsure of how to fix this. The calculation code is below just for north at the moment.
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = this.getWidth();
int height = this.getHeight();
int north = ((screenWidth - width)/2);
Any help will be greatly appreciated.
Thanks!
Here you are passing two parameters, X and Y:
setLocation(500,500);
Here you are passing one, but which is it? X or Y? If X, where's Y?:
int north = ((screenWidth - width)/2);
setLocation(north);
The compiler is telling you it doesn't have a setLocation() method that takes one parameter. It wants a location in 2D space: that's an X and Y. Probably what you want is:
setLocation(north, 0);
Toolkit.getDefaultToolkit().getScreenSize() returns the full size of the default screen, it does not take into consideration things like task bars or other elements which may occupy space on the desktop which windows should avoid been placed under/over.
A better solution would be to use Toolkit.getDefaultToolkit().getScreenInsets(GraphicsConfiguration) and GraphicsConfiguration#getBounds
public static Rectangle getScreenViewableBounds(Window window) {
return getScreenViewableBounds((Component) window);
}
public static Rectangle getScreenViewableBounds(Component comp) {
return getScreenViewableBounds(getGraphicsDevice(comp));
}
public static Rectangle getScreenViewableBounds(GraphicsDevice gd) {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (gd == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
}
if (gd != null) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bounds = gc.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
}
return bounds;
}
/**
* Attempts to locate the graphics device that the component most likely is
* on.
*
* This calculates the area that the window occupies on each screen deivce and
* returns the one which it occupies the most.
*
* #param comp
* #return
*/
public static GraphicsDevice getGraphicsDevice(Component comp) {
GraphicsDevice device = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice lstGDs[] = ge.getScreenDevices();
ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
if (comp != null && comp.isVisible()) {
Rectangle parentBounds = comp.getBounds();
/*
* If the component is not a window, we need to find its location on the
* screen...
*/
if (!(comp instanceof Window)) {
Point p = new Point(0, 0);
SwingUtilities.convertPointToScreen(p, comp);
parentBounds.setLocation(p);
}
for (GraphicsDevice gd : lstGDs) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screenBounds = gc.getBounds();
if (screenBounds.intersects(parentBounds)) {
lstDevices.add(gd);
}
}
if (lstDevices.size() == 1) {
device = lstDevices.get(0);
} else {
GraphicsDevice gdMost = null;
float maxArea = 0;
for (GraphicsDevice gd : lstDevices) {
int width = 0;
int height = 0;
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
Rectangle2D intBounds = bounds.createIntersection(parentBounds);
float perArea = (float) ((intBounds.getWidth() * intBounds.getHeight()) / (parentBounds.width * parentBounds.height));
if (perArea > maxArea) {
maxArea = perArea;
gdMost = gd;
}
}
if (gdMost != null) {
device = gdMost;
}
}
}
return device;
}
The main issue you're having is that there is no such method as setLocation(int)...what would the value int represent any way? x or y position?
You need to pass both the x and y position to setLocation in order for it to work.
Rectangle bounds = getScreenViewableBounds(this); // where this is a reference to your window
int x = bounds.x + ((bounds.width - getWidth()) / 2;
int y = bounds.y;
setLocation(x, y);
For example...
There is the following code:
double gridWidth=columnNumber*(IMAGE_WIDTH_NORMAL+10)+(columnNumber+2)*10;
double screenWidth=Toolkit.getDefaultToolkit().getScreenSize().getWidth()*0.8;
screenWidth=(gridWidth>=screenWidth) ? screenWidth : gridWidth;
double gridHeight=(rules.size()+2)*(IMAGE_HEIGHT_NORMAL+10)+rules.size()*20+(rules.size()+2)*10;
double screenHeight=Toolkit.getDefaultToolkit().getScreenSize().getHeight()*0.8;
screenHeight=(gridHeight>=screenHeight) ? screenHeight : gridHeight;
setSize((int)screenWidth, (int)screenHeight);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(screenSize.width/2-getWidth()/2, screenSize.height/2-getHeight()/2);
setMaximumSize(new Dimension((int)gridWeight, (int)gridHeight));
setVisible(true);
This code does the following thing: calculates preferred width and height of frame, and if the size bigger then I want (80% of width and height), then application sets another size; but I want to allow user to click by "full screen" icon on the jframe in order to maximize jframe for preferred size (setMaximumSize() method), but it code doesn't work! Always a windows maximizes to full screen of my laptop! How can I fix it?
I'm not sure if I understood your problem correctly, but I suggest using
java.awt.Window#setBounds(int, int, int, int)
to set the location and size of a JFrame.
int x = screenSize.width/2-getWidth()/2;
int y = screenSize.height/2-getHeight()/2;
int width = (int)gridWeight;
int height = (int)gridHeight);
setBounds(x, y, width, height);
To maximize a JFrame I suggest using
java.awt.Frame#setExtendedState(int)
like this
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
You need to do like this :
_mainDialog.setBounds(rectangle);
_mainDialog.setExtendedState(windowsState);
Example :
addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent e) {
int oldState = e.getOldState();
int newState = e.getNewState();
if ((oldState & JFrame.ICONIFIED) == 0 && (newState & JFrame.ICONIFIED) != 0) {
setMainDialogRectangle(minRectangle, JFrame.NORMAL);
}
else if ((oldState & JFrame.ICONIFIED) != 0 && (newState & JFrame.ICONIFIED) == 0) {
setMainDialogRectangle(minRectangle, JFrame.NORMAL);
}
else if ((e.getNewState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH) {
setMainDialogRectangle(maxRectangle, JFrame.MAXIMIZED_BOTH);
}
else if ((oldState & JFrame.MAXIMIZED_BOTH) != 0 && (newState & JFrame.MAXIMIZED_BOTH) == 0) {
setMainDialogRectangle(medRectangle, JFrame.NORMAL);
}
}
});