Java Rectangle Create Parse getRectangle(Map bounds)

Here you can find the source of getRectangle(Map bounds)

Description

Extract a rectangle which has been serialised to JSON as x,y,w,h.

License

Educational Community License

Parameter

Parameter Description
bounds map containing bounds values

Exception

Parameter Description
IOException if any of the coordinates can't be interpreted

Return

the rectangle which was serialised

Declaration

public static Rectangle getRectangle(Map<String, Object> bounds) throws IOException 

Method Source Code


//package com.java2s;
/*// w w w  . j ava  2 s  . co  m
 * Copyright 2013 Saint Louis University. Licensed under the
 *   Educational Community 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.osedu.org/licenses/ECL-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.Rectangle;
import java.io.IOException;

import java.util.Map;

public class Main {
    /**
     * Extract a rectangle which has been serialised to JSON as x,y,w,h.
     * @param bounds map containing bounds values
     * @return the rectangle which was serialised
     * @throws IOException if any of the coordinates can't be interpreted
     */
    public static Rectangle getRectangle(Map<String, Object> bounds) throws IOException {
        return new Rectangle(getInt(bounds, "x"), getInt(bounds, "y"), getInt(bounds, "width"),
                getInt(bounds, "height"));
    }

    /**
     * Extract an integer value from a JSON map.  Used for extracting coordinates from rectangles.
     * @param map map containing integers of interest
     * @param key field which contains the value
     * @return the field's value as an int.
     */
    public static int getInt(Map<String, Object> map, String key) throws IOException {
        Object o = map.get(key);
        if (o == null || !(o instanceof Number)) {
            throw new IOException(
                    String.format("Malformed JSON input: unable to extract numeric value for \"%s\"", key));
        }
        return ((Number) o).intValue();
    }
}

Related

  1. decodeBounds(String sBounds)
  2. getRectangle(Preferences prefs, String sName, int iDefaultWidth, int iDefaultHeight)
  3. getRectangle(Properties prop, final String key, final Rectangle def)
  4. getRectangle(String rectStr)
  5. newRectangle(int x1, int y1, int x2, int y2)