|
From: <cpm...@us...> - 2011-08-28 03:21:09
|
Revision: 15343
http://pcgen.svn.sourceforge.net/pcgen/?rev=15343&view=rev
Author: cpmeister
Date: 2011-08-28 03:21:02 +0000 (Sun, 28 Aug 2011)
Log Message:
-----------
PortraitPane update:
- image loading is now operational
Modified Paths:
--------------
sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/PortraitInfoPane.java
Added Paths:
-----------
sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/ImagePreviewer.java
Added: sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/ImagePreviewer.java
===================================================================
--- sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/ImagePreviewer.java (rev 0)
+++ sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/ImagePreviewer.java 2011-08-28 03:21:02 UTC (rev 15343)
@@ -0,0 +1,116 @@
+/*
+ * ImagePreviewer.java
+ * Copyright 2011 Connor Petty <cpm...@us...>
+ *
+ * This library 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 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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 this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Created on Aug 27, 2011, 5:30:02 PM
+ */
+package pcgen.gui2.tabs.bio;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import javax.imageio.ImageIO;
+import javax.swing.JComponent;
+import javax.swing.UIManager;
+import pcgen.system.LanguageBundle;
+import pcgen.util.Logging;
+
+/**
+ *
+ * @author Connor Petty <cpm...@us...>
+ */
+public class ImagePreviewer extends JComponent
+{
+
+ private static final int SIZE = 200;
+ private static String in_notAnImage = LanguageBundle.getString("in_ImagePreview_notAnImage");
+ private BufferedImage image;
+
+ public ImagePreviewer()
+ {
+ setPreferredSize(new Dimension(SIZE, SIZE));
+ }
+
+ public void setImage(File file)
+ {
+ if (null == file || !file.exists())
+ {
+ image = null;
+ return;
+ }
+ try
+ {
+ image = ImageIO.read(file);
+ }
+ catch (IOException ex)
+ {
+ Logging.errorPrint("Could not read image", ex);
+ }
+ repaint();
+ }
+
+ public BufferedImage getImage()
+ {
+ return image;
+ }
+
+ @Override
+ protected void paintComponent(Graphics g)
+ {
+ g.setColor(UIManager.getColor("Panel.background"));
+ g.fillRect(0, 0, getWidth(), getHeight());
+
+ final int textX = getFontHeightHint(g);
+ final int textY = SIZE - getFontHeightHint(g);
+
+ if (null != image)
+ {
+ final int width = image.getWidth(null);
+ final int height = image.getHeight(null);
+ final int side = Math.max(width, height);
+ final double scale = (double) SIZE / (double) side;
+
+ g.drawImage(image, 0, 0, (int) (scale * width),
+ (int) (scale * height), null);
+
+ // Annotate with original dimensions. Overlay black on white so
+ // the values are visible against most possible image backgrounds.
+ final String dim = width + " x " + height;
+
+ g.setColor(Color.black);
+ g.drawString(dim, textX, textY);
+ g.setColor(Color.white);
+ g.drawString(dim, textX - 1, textX - 1);
+ }
+ else
+ {
+ g.setColor(UIManager.getColor("Panel.foreground"));
+ // TODO: I18N
+ g.drawString(in_notAnImage,
+ textX, textY);
+ }
+ }
+
+ private static int getFontHeightHint(final Graphics g)
+ {
+ return g.getFontMetrics().getHeight();
+ }
+
+}
Modified: sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/PortraitInfoPane.java
===================================================================
--- sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/PortraitInfoPane.java 2011-08-27 23:33:54 UTC (rev 15342)
+++ sandbox/uisync/code/src/java/pcgen/gui2/tabs/bio/PortraitInfoPane.java 2011-08-28 03:21:02 UTC (rev 15343)
@@ -21,13 +21,18 @@
package pcgen.gui2.tabs.bio;
import java.awt.FlowLayout;
-import java.awt.Image;
import java.awt.Rectangle;
+import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import java.util.Hashtable;
+import javax.swing.AbstractAction;
+import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
+import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import pcgen.core.facade.CharacterFacade;
@@ -47,6 +52,8 @@
private final ThumbnailPane tnPane;
private final JButton loadButton;
private final JButton clearButton;
+ private BufferedImage portrait;
+ private Rectangle cropRect;
public PortraitInfoPane()
{
@@ -73,10 +80,8 @@
box.add(Box.createVerticalStrut(10));
{
Box hbox = Box.createHorizontalBox();
- loadButton.setText("Load Portrait");
hbox.add(loadButton);
hbox.add(Box.createHorizontalStrut(10));
- clearButton.setText("Clear Portrait");
hbox.add(clearButton);
box.add(hbox);
}
@@ -103,16 +108,28 @@
add(box);
}
- public Hashtable<Object, Object> createModels(CharacterFacade character)
+ private Rectangle createDefaultCropRectangle()
{
- Hashtable<Object, Object> state = new Hashtable<Object, Object>();
+ return new Rectangle(0, 0, 100, 100);
+ }
+
+ private BufferedImage createDefaultPortrait()
+ {
ImageIcon defaultPortrait = Icons.DefaultPortrait.getImageIcon();
BufferedImage bufImage = new BufferedImage(defaultPortrait.getIconWidth(),
defaultPortrait.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
defaultPortrait.paintIcon(this, bufImage.createGraphics(), 0, 0);
- state.put(BufferedImage.class, bufImage);
- state.put(Rectangle.class, new Rectangle(1, 1, 100, 100));
+ return bufImage;
+ }
+
+ public Hashtable<Object, Object> createModels(CharacterFacade character)
+ {
+ Hashtable<Object, Object> state = new Hashtable<Object, Object>();
+ state.put(BufferedImage.class, createDefaultPortrait());
+ state.put(Rectangle.class, createDefaultCropRectangle());
+ state.put(LoadAction.class, new LoadAction());
+ state.put(ClearAction.class, new ClearAction());
//TODO: finish
return state;
//throw new UnsupportedOperationException("Not supported yet.");
@@ -120,8 +137,23 @@
public void restoreModels(Hashtable<?, ?> state)
{
- BufferedImage portrait = (BufferedImage) state.get(BufferedImage.class);
- Rectangle cropRect = (Rectangle) state.get(Rectangle.class);
+
+ loadButton.setAction((Action) state.get(LoadAction.class));
+ clearButton.setAction((Action) state.get(ClearAction.class));
+ setPortraitImage((BufferedImage) state.get(BufferedImage.class),
+ (Rectangle) state.get(Rectangle.class));
+ }
+
+ public void storeModels(Hashtable<Object, Object> state)
+ {
+ state.put(BufferedImage.class, portrait);
+ state.put(Rectangle.class, cropRect);
+ }
+
+ private void setPortraitImage(BufferedImage portraitImage, Rectangle cropRect)
+ {
+ this.portrait = portraitImage;
+ this.cropRect = cropRect;
portraitPane.setPortraitImage(portrait);
portraitPane.setCropRectangle(cropRect);
portraitPane.revalidate();
@@ -130,13 +162,56 @@
tnPane.revalidate();
}
- public void storeModels(Hashtable<Object, Object> state)
+ public TabTitle getTabTitle()
{
+ return tabTitle;
}
- public TabTitle getTabTitle()
+ private class LoadAction extends AbstractAction implements PropertyChangeListener
{
- return tabTitle;
+
+ private ImagePreviewer previewer = new ImagePreviewer();
+ private JFileChooser chooser = new JFileChooser();
+
+ public LoadAction()
+ {
+ super("Load Portrait");
+ chooser.setAccessory(previewer);
+ }
+
+ public void actionPerformed(ActionEvent e)
+ {
+ chooser.addPropertyChangeListener(this);
+ int ret = chooser.showOpenDialog(PortraitInfoPane.this);
+ chooser.removePropertyChangeListener(this);
+ BufferedImage image = previewer.getImage();
+ if (ret != JFileChooser.APPROVE_OPTION || image == null)
+ {
+ return;
+ }
+ setPortraitImage(image, new Rectangle(1, 1, 100, 100));
+ }
+
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ previewer.setImage(chooser.getSelectedFile());
+ }
+
}
+ private class ClearAction extends AbstractAction
+ {
+
+ public ClearAction()
+ {
+ super("Clear Portrait");
+ }
+
+ public void actionPerformed(ActionEvent e)
+ {
+ setPortraitImage(createDefaultPortrait(), createDefaultCropRectangle());
+ }
+
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|