/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime;

import javax.microedition.lcdui.Graphics;

/**
 * The GraphicsUtil class is a graphics utility class.This class is used to clip
 * area for Graphcis.
 * 
 * @author aaa
 * 
 */
public class GraphicsUtil
{
    public static Rectangle clipRectangle = new Rectangle();

    static final Logger LOG = Logger.getLogger("minime.GraphicsUtil");

    /**
     * Gets the position and size of the current clipping area with a rectangle
     * object.
     * 
     * @param g
     *            the Graphics object
     * @param r
     *            the rectangle object
     */
    public static final void getClip(Graphics g, Rectangle r)
    {
        r.x = g.getClipX();
        r.y = g.getClipY();
        r.w = g.getClipWidth();
        r.h = g.getClipHeight();
    }

    /**
     * Intersects the current clip with the specified rectangle. The resulting
     * clipping area is the intersection of the current clipping area and the
     * specified rectangle. This method can only be used to make the current
     * clip smaller. To set the current clip larger, use the setClip method.
     * Rendering operations have no effect outside of the clipping area.
     * 
     * @param g
     *            the Graphics object
     * @param r
     *            the rectangle object
     */
    public static final void clip(Graphics g, Rectangle r)
    {
        g.clipRect(r.x, r.y, r.w, r.h);
    }

    /**
     * Sets the current clip to the rectangle specified by the given graphics
     * object and rectangle object. Rendering operations have no effect outside
     * of the clipping area.
     * 
     * @param g
     *            the Graphics object
     * @param r
     *            the specified rectangle object
     */
    public static final void setClip(Graphics g, Rectangle r)
    {
        LOG.debug("setclip " + r.x + ", " + r.y + ", " + r.w + ", " + r.h);
        g.setClip(r.x, r.y, r.w, r.h);
    }

    /**
     * Sets the current clip to the rectangle specified by the given graphics
     * object and coordinates. Rendering operations have no effect outside of
     * the clipping area.
     * 
     * @param g
     *            the Graphics object
     * @param x
     *            the x coordinate of the new clip rectangle
     * @param y
     *            the y coordinate of the new clip rectangle
     * @param w
     *            the width of the new clip rectangle
     * @param h
     *            the height of the new clip rectangle
     */
    public static final void setClip(Graphics g, int x, int y, int w, int h)
    {
        LOG.debug("setclip " + x + ", " + y + ", " + w + ", " + h);
        g.setClip(x, y, w, h);
    }

}
