Here you can find the source of printImageFromComponents(final Component _rootComponent, final int _currentColumn, final BufferedImage _bi)
Parameter | Description |
---|---|
_rootComponent | the root component |
_currentColumn | the column of the current item |
_bi | the bufferedImage which is altered and afterwards returned |
private static BufferedImage printImageFromComponents(final Component _rootComponent, final int _currentColumn, final BufferedImage _bi)
//package com.java2s; /*//from w ww. j ava 2 s . c o m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Panel; import java.awt.Window; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; public class Main { /** * The dimension of one item in the picture (only necessary if the specified * operation is the CHECK_OP_IMAGE operation). */ public static final Dimension D_ITEM = new Dimension(70, 10); /** * The distance of two items in the picture (only necessary if the specified * operation is the CHECK_OP_IMAGE operation). */ public static final Dimension D_DISTANCE = new Dimension(12, 2); /** * Colors for displaying view. */ private static final Color CLR_CORRECT = new Color(130, 131, 133), CLR_ERROR = new Color(255, 120, 120); /** * Font which is used for printing names inside the return image of * function printImageFormComponents(). */ private static final Font FONT_IMAGE = new Font("", Font.PLAIN, 10); /** * Current entry (has to be outside the function because * it is only allowed to have one return value. Thus either * the BufferedImage or the currentEntry have to be externalized. */ private static int currentEntry = 1; /** * Print image from components. * Recursive method. * * @param _rootComponent the root component * @param _currentColumn the column of the current item * @param _bi the bufferedImage which is altered and * afterwards returned * * @return the BufferedImage which displays the complete * graphical user interface landscape. */ private static BufferedImage printImageFromComponents(final Component _rootComponent, final int _currentColumn, final BufferedImage _bi) { //save given values. BufferedImage bi_ret = _bi; int adjC = 0 + _currentColumn * (D_ITEM.width + D_DISTANCE.width); int adjR = 0 + currentEntry * (D_ITEM.height + D_DISTANCE.height); Graphics g = bi_ret.getGraphics(); if (_rootComponent.getWidth() <= 0 || _rootComponent.getHeight() <= 0) { g.setColor(CLR_ERROR); } else { g.setColor(CLR_CORRECT); } g.drawRect(adjC, adjR, D_ITEM.width, D_ITEM.height); g.setFont(FONT_IMAGE); g.drawString(_rootComponent.getClass().getSimpleName(), adjC, adjR + D_ITEM.height); currentEntry++; if (_rootComponent instanceof JPanel) { for (Component x : ((JPanel) _rootComponent).getComponents()) { final int vorher = currentEntry + 1; currentEntry--; bi_ret = printImageFromComponents(x, _currentColumn + 1, _bi); currentEntry = Math.max(vorher, currentEntry); } } else if (_rootComponent instanceof JFrame) { for (Component x : ((JFrame) _rootComponent).getContentPane().getComponents()) { final int vorher = currentEntry + 1; currentEntry--; bi_ret = printImageFromComponents(x, _currentColumn + 1, _bi); currentEntry = Math.max(vorher, currentEntry); } } else if (_rootComponent instanceof Panel) { for (Component x : ((Panel) _rootComponent).getComponents()) { final int vorher = currentEntry + 1; currentEntry--; bi_ret = printImageFromComponents(x, _currentColumn + 1, _bi); currentEntry = Math.max(vorher, currentEntry); } } else if (_rootComponent instanceof Window) { for (Component x : ((Window) _rootComponent).getComponents()) { final int vorher = currentEntry + 1; currentEntry--; bi_ret = printImageFromComponents(x, _currentColumn + 1, _bi); currentEntry = Math.max(vorher, currentEntry); } } return bi_ret; } }