Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2014 tabletoptool.com team.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     rptools.com team - initial implementation
 *     tabletoptool.com team - further development
 */

import java.awt.Color;
import java.awt.Component;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

import java.awt.Transparency;

import java.awt.image.BufferedImage;

import javax.swing.SwingUtilities;

public class Main {
    public static BufferedImage takeScreenShot(Component component, String... watermarks) {

        Dimension size = component.getSize();

        BufferedImage screenshot = new BufferedImage(size.width, size.height, Transparency.OPAQUE);
        Graphics2D g = screenshot.createGraphics();
        g.setClip(0, 0, size.width - 1, size.height - 1);

        component.update(g);

        FontMetrics fm = g.getFontMetrics();
        int y = fm.getDescent();
        for (String watermark : watermarks)
            if (watermark != null) {
                int x = size.width - SwingUtilities.computeStringWidth(fm, watermark);

                g.setColor(Color.black);
                g.drawString(watermark, x, y);

                g.setColor(Color.white);
                g.drawString(watermark, x - 1, y - 1);

                y -= fm.getHeight();
            }

        g.dispose();

        return screenshot;
    }
}