Skip to content

Instantly share code, notes, and snippets.

@kamontat
Last active March 7, 2017 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamontat/e02e436c9686d8acb216853ab6638a27 to your computer and use it in GitHub Desktop.
Save kamontat/e02e436c9686d8acb216853ab6638a27 to your computer and use it in GitHub Desktop.
deal with display location
package com.view.gui.lib;
import java.awt.*;
/**
* utilities with dimension and screen display.
*
* @author kamontat
* @version 1.3
* @since 1/30/2017 AD - 1:47 PM
*/
public class DimensionUtilities {
/**
* Arithmetic operations.
*/
public enum operation {
ADD, MINUS, MULTIPLY, DIVIDE;
}
/**
* Screen information <br>
* - get size <code>screen.getWidth</code> and <code>screen.getHeight</code>
*/
private static DisplayMode screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
/**
* get Screen size
*
* @return screen size
*/
public static Dimension getScreenSize() {
return new Dimension(screen.getWidth(), screen.getHeight());
}
/**
* test than pageSize is bigger than screen (computer monitor) or not
*
* @param pageSize
* page size (if you fixed size)
* @return true if bigger than, otherwise will return false
*/
public static boolean isBiggerThanScreen(Dimension pageSize) {
return !(pageSize.getWidth() < screen.getWidth() && pageSize.getHeight() < screen.getHeight());
}
/**
* return the minimum width and height in a and b <br>
* example: a = (200, 400), b = (500, 100) <br>
* the result will be (500, 200)
*
* @param a
* first size
* @param b
* second size
* @return the minimum size
*/
public static Dimension maximum(Dimension a, Dimension b) {
Dimension result = new Dimension();
if (a.getWidth() > b.getWidth()) result.setSize(a.getWidth(), result.getHeight());
else result.setSize(b.getWidth(), result.getHeight());
if (a.getHeight() > b.getHeight()) result.setSize(result.getWidth(), a.getHeight());
else result.setSize(result.getWidth(), b.getHeight());
return result;
}
/**
* a [{@link operation}] b = return result.
*
* @param a
* is first dimension.
* @param b
* is second dimension.
* @param op
* is arithmetic operation.
* @return result of 2 <code>a</code> and <code>b</code> do {@link operation}.
*/
public static Dimension operation(Dimension a, Dimension b, operation op) {
Dimension result = new Dimension(a);
switch (op) {
case ADD:
result.setSize(result.width + b.width, result.height + b.height);
break;
case MINUS:
result.setSize(result.width - b.width, result.height - b.height);
break;
case MULTIPLY:
result.setSize(result.width * b.width, result.height * b.height);
break;
case DIVIDE:
result.setSize(result.width / b.width, result.height / b.height);
break;
}
return result;
}
}
import java.awt.*;
/**
* get {@link Point} in the screen with some condition.
*
* @author kamontat
* @version 4.1
* @since 17/8/59 - 23:54
*/
public class Display {
/**
* get default point at x=0, y=0
*/
public static Point getDefaultPoint = new Point(0, 0);
/**
* get point that stay in the center of the screen
*
* @param pageSize
* size of page that want to show in the center
* @return point of center screen (you can set in <code>setLocation()</code> directly)
*/
public static Point getCenterLocation(Dimension pageSize) {
return new Point((int) ((DimensionUtilities.getScreenSize().getWidth() / 2) - (pageSize.getWidth() / 2)), (int) ((DimensionUtilities.getScreenSize().getHeight() / 2) - (pageSize.getHeight() / 2)));
}
/**
* get point to show page at center of the old page <br>
* if don't have old page (oldPage is <code>null</code>) this method will return center of the screen
*
* @param oldPage
* current page
* @param newPage
* new page that want to show
* @return point (you can set in <code>setLocation()</code> directly)
*/
public static Point getCenterPage(Window oldPage, Window newPage) {
if (oldPage == null) {
return getCenterLocation(newPage.getSize());
}
Point currPoint = oldPage.getLocation();
int newX = (currPoint.x + (oldPage.getWidth() / 2)) - (newPage.getWidth() / 2);
int newY = (currPoint.y + (oldPage.getHeight() / 2)) - (newPage.getHeight() / 2);
return new Point(newX, newY);
}
}
@kamontat
Copy link
Author

I separate size and location out

@kamontat
Copy link
Author

kamontat commented Mar 7, 2017

add more method in DimensionUtilities which is operation 2 dimension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment