Java Graphics Clip mergeClip(Graphics g, Shape clip)

Here you can find the source of mergeClip(Graphics g, Shape clip)

Description

Sets the clip on a graphics object by merging a supplied clip with the existing one.

License

Open Source License

Parameter

Parameter Description
g the graphics object to update
clip a new clipping region to add to the graphics clip. This may return null if the current clip is null .

Exception

Parameter Description
NullPointerException if any parameter is null

Return

the current clipping region of the supplied graphics object

Declaration

public static Shape mergeClip(Graphics g, Shape clip) 

Method Source Code


//package com.java2s;
/*//from  w ww. j  a  va 2  s.com
 * Copyright (c) 2009 Albert Kurucz.
 *
 * This file, GraphicsUtilities.java is part of JTStand.
 *
 * JTStand is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JTStand is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JTStand.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.geom.Area;

import java.awt.Shape;

import java.awt.Graphics;

public class Main {
    /**
     * Sets the clip on a graphics object by merging a supplied clip with the
     * existing one. The new clip will be an intersection of the old clip and
     * the supplied clip. The old clip shape will be returned. This is useful
     * for resetting the old clip after an operation is performed.
     * 
     * @param g
     *            the graphics object to update
     * @param clip
     *            a new clipping region to add to the graphics clip. This may
     *            return {@code null} if the current clip is {@code null}.
     * @return the current clipping region of the supplied graphics object
     * @throws NullPointerException
     *             if any parameter is {@code null}
     */
    public static Shape mergeClip(Graphics g, Shape clip) {
        Shape oldClip = g.getClip();
        if (oldClip == null) {
            g.setClip(clip);
            return null;
        }
        Area area = new Area(oldClip);
        area.intersect(new Area(clip));//new Rectangle(0,0,width,height)));
        g.setClip(area);
        return oldClip;
    }
}

Related

  1. resetClip(Graphics g)
  2. restoreClip(final Graphics g, final Shape clip)
  3. subtractClip(final Graphics g, final Shape clip)