Compute how much dialog can be put into the screen and returns a list with the coordinates from the dialog positions as Point objects. - Java Swing

Java examples for Swing:Screen

Description

Compute how much dialog can be put into the screen and returns a list with the coordinates from the dialog positions as Point objects.

Demo Code

/**//from  www  . j av  a2s.  c o  m
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

public class Main{
    /**
     * Compute how much dialog can be put into the screen and returns a list with the coordinates
     * from the dialog positions as Point objects.
     *
     * @param dialogWidth
     *            the dialog width
     * @param dialogHeight
     *            the dialog height
     * @return the list with the computed Point objects.
     */
    public static List<Point> computeDialogPositions(int dialogWidth,
            int dialogHeight) {
        List<Point> dialogPosition = null;
        int windowBesides = ScreenSizeUtils.getScreenWidth() / dialogWidth;
        int windowBelow = ScreenSizeUtils.getScreenHeight() / dialogHeight;
        int listSize = windowBesides * windowBelow;
        dialogPosition = new ArrayList<>(listSize);
        int dotWidth = 0;
        int dotHeight = 0;
        for (int y = 0; y < windowBelow; y++) {
            dotWidth = 0;
            for (int x = 0; x < windowBesides; x++) {
                Point p = new Point(dotWidth, dotHeight);
                dialogPosition.add(p);
                dotWidth = dotWidth + dialogWidth;
            }
            dotHeight = dotHeight + dialogHeight;
        }
        return dialogPosition;
    }
    /**
     * Gets the width from the current screen.
     *
     * @return Returns the width from the current screen.
     */
    public static int getScreenWidth() {
        int x = (int) Toolkit.getDefaultToolkit().getScreenSize()
                .getWidth();
        return x;
    }
    /**
     * Gets the height from the current screen.
     *
     * @return Returns the height from the current screen.
     */
    public static int getScreenHeight() {
        int y = (int) Toolkit.getDefaultToolkit().getScreenSize()
                .getHeight();
        return y;
    }
}

Related Tutorials