quarter Rectangle - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

quarter Rectangle

Demo Code


//package com.java2s;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Rectangle> quarter(Rectangle region) {
        List<Rectangle> list = new ArrayList<Rectangle>();
        for (Rectangle r : splitHorizontally(region))
            list.addAll(splitVertically(r));
        return list;
    }/*  w w  w.  j ava  2 s  .  com*/

    public static List<Rectangle> splitHorizontally(Rectangle region) {
        List<Rectangle> list = new ArrayList<Rectangle>();
        int halfWidth = region.width / 2;
        list.add(new Rectangle(region.x, region.y, halfWidth, region.height));
        list.add(new Rectangle(region.x + halfWidth, region.y, region.width
                - halfWidth, region.height));
        return list;
    }

    public static List<Rectangle> splitVertically(Rectangle region) {
        List<Rectangle> list = new ArrayList<Rectangle>();
        int halfHeight = region.height / 2;
        list.add(new Rectangle(region.x, region.y, region.width, halfHeight));
        list.add(new Rectangle(region.x, region.y + halfHeight,
                region.width, region.height - halfHeight));
        return list;
    }
}

Related Tutorials