001: /*
002: * Swing Explorer. Tool for developers exploring Java/Swing-based application internals.
003: * Copyright (C) 2008, Maxim Zakharenkov
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License along
016: * with this program; if not, write to the Free Software Foundation, Inc.,
017: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018: *
019: * $Header: /cvs/swingexplorer/src/org/swingexplorer/IconPanel.java,v 1.2 2008/02/06 08:36:09 maxz1 Exp $
020: */
021: package org.swingexplorer;
022:
023: import java.awt.Color;
024: import java.awt.Dimension;
025: import java.awt.Graphics;
026: import java.awt.Graphics2D;
027: import java.awt.Rectangle;
028:
029: import javax.swing.Icon;
030: import javax.swing.JComponent;
031:
032: /**
033: *
034: * @author Maxim Zakharenkov
035: */
036: public class IconPanel extends JComponent {
037:
038: Icon icon;
039: double scale = 1;
040: Rectangle selection;
041:
042: public IconPanel(Icon ico) {
043: icon = ico;
044: }
045:
046: public IconPanel() {
047: }
048:
049: public void setIcon(Icon icon) {
050: this .icon = icon;
051: revalidate();
052: repaint();
053: }
054:
055: public Icon getIcon() {
056: return icon;
057: }
058:
059: public void setSelection(Rectangle rect) {
060: selection = rect;
061: repaint();
062: }
063:
064: public Rectangle getSelection() {
065: return selection;
066: }
067:
068: public void setScale(double scaleP) {
069: scale = scaleP;
070: revalidate();
071: repaint();
072: }
073:
074: public double getScale() {
075: return scale;
076: }
077:
078: public void paint(Graphics g) {
079: Graphics2D g2d = (Graphics2D) g;
080: if (icon != null) {
081:
082: g2d.scale(getScale(), getScale());
083: icon.paintIcon(this , g2d, 0, 0);
084:
085: if (selection != null) {
086: g2d.setColor(Color.RED);
087: g2d.drawRect(selection.x, selection.y, selection.width,
088: selection.height);
089: }
090: } else {
091: g2d.drawString("No icon set", 0, 20);
092: }
093: }
094:
095: public Dimension getPreferredSize() {
096: if (icon == null) {
097: return new Dimension(0, 0);
098: }
099: return new Dimension((int) Math.round(getScale()
100: * icon.getIconWidth()), (int) Math.round(getScale()
101: * icon.getIconHeight()));
102: }
103: }
104:
105: /*
106: * $Log: IconPanel.java,v $
107: * Revision 1.2 2008/02/06 08:36:09 maxz1
108: * Changed license header
109: *
110: * Revision 1.1 2007/06/27 19:41:38 maxz1
111: * new
112: *
113: */
|