Java Draw Image draw(BufferedImage image, Consumer action)

Here you can find the source of draw(BufferedImage image, Consumer action)

Description

Draw onto an image

License

Apache License

Parameter

Parameter Description
image Image
action Action to draw

Declaration

public static void draw(BufferedImage image, Consumer<Graphics2D> action) 

Method Source Code

//package com.java2s;
/*//from  ww w.  ja va  2s . c  o m
 * Copyright 2016 peter.
 *
 * 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.Graphics2D;

import java.awt.image.BufferedImage;
import java.util.function.Consumer;

public class Main {
    /**
     * Draw onto an image
     *
     * @param image  Image
     * @param action Action to draw
     */
    public static void draw(BufferedImage image, Consumer<Graphics2D> action) {
        Graphics2D g = image.createGraphics();
        try {
            action.accept(g);
        } finally {
            g.dispose();
        }
    }

    /**
     * Draw using a new Graphics2D
     *
     * @param g0     Existing Graphics2d
     * @param action action to draw
     */
    public static void draw(Graphics2D g0, Consumer<Graphics2D> action) {
        Graphics2D g = (Graphics2D) g0.create();
        try {
            action.accept(g);
        } finally {
            g.dispose();
        }
    }
}

Related

  1. draw(BufferedImage image, Rectangle rectangle, BufferedImage backgroundImg)
  2. draw(final Image img, final int type)
  3. draw4on1(BufferedImage[] src, BufferedImage dst)
  4. draw9Patch(BufferedImage image, Integer[] patches, float ratio)