The tree list in colopedia now scales using the scaleFactor.

This commit is contained in:
Stian Grenborgen 2022-04-20 21:04:19 +02:00
parent 4346895df5
commit cf37ae1669
10 changed files with 108 additions and 50 deletions

View File

@ -534,14 +534,14 @@ public final class ImageLibrary {
: getUnscaledImage(key);
}
public static BufferedImage getColopediaCellImage(boolean expanded) {
public BufferedImage getColopediaCellImage(boolean expanded) {
final String key = "image.icon.Colopedia."
+ ((expanded) ? "open" : "closed") + "Section";
return getUnscaledImage(key);
return getScaledImage(key);
}
public static BufferedImage getColopediaConceptImage() {
return getUnscaledImage("image.icon.Colopedia.idea");
public BufferedImage getColopediaConceptImage() {
return getScaledImage("image.icon.Colopedia.idea");
}
public static BufferedImage getColorCellRendererBackground() {
@ -606,7 +606,7 @@ public final class ImageLibrary {
}
public BufferedImage getLibertyImage() {
return this.imageCache.getSizedImage(BELLS, ICON_SIZE, false);
return this.imageCache.getSizedImage(BELLS, scale(ICON_SIZE), false);
}
public static BufferedImage getListBackground() {
@ -685,7 +685,7 @@ public final class ImageLibrary {
* @return The appropriate {@code BufferedImage}.
*/
public BufferedImage getObjectImage(FreeColObject display) {
return getObjectImageInternal(display, scale(ICON_SIZE, scaleFactor));
return getObjectImageInternal(display, scale(ICON_SIZE));
}
/**

View File

@ -26,6 +26,7 @@ import static net.sf.freecol.common.util.CollectionUtils.iterable;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@ -56,6 +57,7 @@ import net.sf.freecol.common.model.BuildingType;
import net.sf.freecol.common.model.ProductionType;
import net.sf.freecol.common.model.StringTemplate;
import net.sf.freecol.common.model.UnitType;
import net.sf.freecol.common.util.ImageUtils;
/**
@ -94,12 +96,14 @@ public class BuildingDetailPanel
: getSpecification().getBuildingTypeList()) {
if (buildingType.getUpgradesFrom() == null) {
String name = Messages.getName(buildingType);
final Dimension listItemIconSize = getListItemIconSize();
final BufferedImage buildingTypeImage = getImageLibrary().getBuildingTypeImage(buildingType, new Dimension(-1, listItemIconSize.height));
final BufferedImage centeredImage = ImageUtils.createCenteredImage(buildingTypeImage, listItemIconSize);
DefaultMutableTreeNode item =
new DefaultMutableTreeNode(new ColopediaTreeItem(
this, buildingType.getId(), name,
new ImageIcon(getImageLibrary()
.getBuildingTypeImage(buildingType,
new Dimension(-1, ImageLibrary.ICON_SIZE.height)))));
new ImageIcon(centeredImage)));
buildingHash.put(buildingType, item);
parent.add(item);
} else {
@ -108,18 +112,21 @@ public class BuildingDetailPanel
}
while (!buildingTypes.isEmpty()) {
Iterator<BuildingType> iterator = buildingTypes.iterator();
final Iterator<BuildingType> iterator = buildingTypes.iterator();
while (iterator.hasNext()) {
BuildingType buildingType = iterator.next();
DefaultMutableTreeNode node = buildingHash.get(buildingType.getUpgradesFrom());
final BuildingType buildingType = iterator.next();
final DefaultMutableTreeNode node = buildingHash.get(buildingType.getUpgradesFrom());
if (node != null) {
String name = Messages.getName(buildingType);
DefaultMutableTreeNode item =
final Dimension listItemIconSize = getListItemIconSize();
final BufferedImage buildingTypeImage = getImageLibrary().getBuildingTypeImage(buildingType, new Dimension(-1, listItemIconSize.height));
final BufferedImage centeredImage = ImageUtils.createCenteredImage(buildingTypeImage, listItemIconSize);
final DefaultMutableTreeNode item =
new DefaultMutableTreeNode(new ColopediaTreeItem(
this, buildingType.getId(), name,
new ImageIcon(getImageLibrary()
.getBuildingTypeImage(buildingType,
new Dimension(-1, ImageLibrary.ICON_SIZE.height)))));
new ImageIcon(centeredImage)));
node.add(item);
buildingHash.put(buildingType, item);
iterator.remove();

View File

@ -19,9 +19,12 @@
package net.sf.freecol.client.gui.panel.colopedia;
import java.awt.Graphics2D;
import java.awt.Image;
import static net.sf.freecol.common.util.CollectionUtils.alwaysTrue;
import static net.sf.freecol.common.util.CollectionUtils.first;
import static net.sf.freecol.common.util.CollectionUtils.transform;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -36,9 +39,11 @@ import javax.swing.text.StyledDocument;
import javax.swing.tree.DefaultMutableTreeNode;
import net.sf.freecol.client.FreeColClient;
import net.sf.freecol.client.gui.*;
import net.sf.freecol.client.gui.ImageLibrary;
import net.sf.freecol.client.gui.ModifierFormat;
import net.sf.freecol.client.gui.action.ColopediaAction.PanelType;
import net.sf.freecol.client.gui.panel.*;
import net.sf.freecol.client.gui.panel.FreeColPanel;
import net.sf.freecol.client.gui.panel.Utility;
import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.model.Ability;
import net.sf.freecol.common.model.AbstractGoods;
@ -49,7 +54,7 @@ import net.sf.freecol.common.model.Modifier;
import net.sf.freecol.common.model.ResourceType;
import net.sf.freecol.common.model.Specification;
import net.sf.freecol.common.model.UnitType;
import static net.sf.freecol.common.util.CollectionUtils.*;
import net.sf.freecol.common.util.ImageUtils;
/**
@ -127,18 +132,9 @@ public abstract class ColopediaGameObjectTypePanel<T extends FreeColSpecObjectTy
String name = getName();
ColopediaTreeItem cti = new ColopediaTreeItem(this, id, name, null);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(cti);
int width = ImageLibrary.ICON_SIZE.width;
int height = ImageLibrary.ICON_SIZE.height;
for (FreeColSpecObjectType type : types) {
Image image = lib.getObjectImage(type, ImageLibrary.ICON_SIZE);
int x = (width - image.getWidth(null)) / 2;
int y = (height - image.getHeight(null)) / 2;
BufferedImage centeredImage
= new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = centeredImage.createGraphics();
g.drawImage(image, x, y, null);
g.dispose();
node.add(buildItem(type, new ImageIcon(centeredImage)));
final BufferedImage image = ImageUtils.createCenteredImage(lib.getObjectImage(type), getListItemIconSize());
node.add(buildItem(type, new ImageIcon(image)));
}
root.add(node);
}
@ -269,6 +265,10 @@ public abstract class ColopediaGameObjectTypePanel<T extends FreeColSpecObjectTy
logger.log(Level.WARNING, "Insert fail", ble);
}
}
public Dimension getListItemIconSize() {
return colopediaPanel.getListItemIconSize();
}
// Override Component

View File

@ -40,6 +40,7 @@ import javax.swing.tree.TreePath;
import net.miginfocom.swing.MigLayout;
import net.sf.freecol.client.FreeColClient;
import net.sf.freecol.client.gui.ImageLibrary;
import net.sf.freecol.client.gui.panel.FreeColPanel;
import net.sf.freecol.client.gui.panel.MigPanel;
import net.sf.freecol.client.gui.panel.Utility;
@ -154,7 +155,7 @@ public final class ColopediaPanel extends FreeColPanel
DefaultTreeModel treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.setCellRenderer(new ColopediaTreeCellRenderer());
tree.setCellRenderer(new ColopediaTreeCellRenderer(this, getImageLibrary()));
tree.setOpaque(false);
tree.addTreeSelectionListener(this);
@ -166,6 +167,16 @@ public final class ColopediaPanel extends FreeColPanel
}
return tree;
}
/**
* Gets the preferred size for the list item images in the colopedia tree.
*/
public Dimension getListItemIconSize() {
final int width = getImageLibrary().scaleInt(ImageLibrary.ICON_SIZE.width * 3 / 2);
final int height = getImageLibrary().scaleInt(ImageLibrary.ICON_SIZE.height);
return new Dimension(width, height);
}
/**
* This function analyzes a tree selection event and calls the

View File

@ -21,6 +21,8 @@ package net.sf.freecol.client.gui.panel.colopedia;
import java.awt.Color;
import java.awt.Component;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JTree;
@ -28,6 +30,7 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import net.sf.freecol.client.gui.ImageLibrary;
import net.sf.freecol.common.util.ImageUtils;
/**
@ -36,10 +39,17 @@ import net.sf.freecol.client.gui.ImageLibrary;
*/
public class ColopediaTreeCellRenderer extends DefaultTreeCellRenderer {
private final ColopediaPanel colopediaPanel;
private final ImageLibrary lib;
/**
* The constructor makes sure that the backgrounds are transparent.
*/
public ColopediaTreeCellRenderer() {
public ColopediaTreeCellRenderer(ColopediaPanel colopediaPanel, ImageLibrary lib) {
this.colopediaPanel = colopediaPanel;
this.lib = lib;
setBackgroundNonSelectionColor(new Color(0,0,0,1));
}
@ -59,8 +69,8 @@ public class ColopediaTreeCellRenderer extends DefaultTreeCellRenderer {
if (nodeItem.getIcon() != null) {
setIcon(nodeItem.getIcon());
} else if (!leaf) {
setIcon(new ImageIcon(ImageLibrary
.getColopediaCellImage(expanded)));
final BufferedImage image = ImageUtils.createCenteredImage(lib.getColopediaCellImage(expanded), colopediaPanel.getListItemIconSize());
setIcon(new ImageIcon(image));
}
return this;
}

View File

@ -22,6 +22,7 @@ package net.sf.freecol.client.gui.panel.colopedia;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -41,6 +42,7 @@ import net.sf.freecol.client.gui.action.ColopediaAction.PanelType;
import net.sf.freecol.client.gui.panel.FreeColPanel;
import net.sf.freecol.client.gui.panel.Utility;
import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.util.ImageUtils;
/**
@ -139,8 +141,9 @@ public class ConceptDetailPanel extends FreeColPanel
for (String concept : concepts) {
String nodeId = "colopedia.concepts." + concept;
String nodeName = Messages.getName(nodeId);
final BufferedImage image = ImageUtils.createCenteredImage(getImageLibrary().getColopediaConceptImage(), colopediaPanel.getListItemIconSize());
nodes.add(new DefaultMutableTreeNode(new ColopediaTreeItem(this,
nodeId, nodeName, new ImageIcon(ImageLibrary.getColopediaConceptImage()))));
nodeId, nodeName, new ImageIcon(image))));
}
Collections.sort(nodes, nodeComparator);
for (DefaultMutableTreeNode n : nodes) {

View File

@ -40,6 +40,7 @@ import net.sf.freecol.client.gui.panel.Utility;
import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.model.FoundingFather;
import net.sf.freecol.common.model.FoundingFather.FoundingFatherType;
import net.sf.freecol.common.util.ImageUtils;
import net.sf.freecol.common.model.Specification;
import net.sf.freecol.common.model.StringTemplate;
import net.sf.freecol.common.model.Turn;
@ -85,7 +86,7 @@ public class FatherDetailPanel
for (FoundingFather foundingFather : spec.getFoundingFathers()) {
fathersByType.get(foundingFather.getType()).add(foundingFather);
}
ImageIcon icon = new ImageIcon(getImageLibrary().getLibertyImage());
ImageIcon icon = new ImageIcon(ImageUtils.createCenteredImage(getImageLibrary().getLibertyImage(), getListItemIconSize()));
for (FoundingFatherType fatherType : FoundingFatherType.values()) {
String id = fatherType.getTypeKey();
String typeName = Messages.message(id);

View File

@ -50,6 +50,7 @@ import net.sf.freecol.common.model.EuropeanNationType;
import net.sf.freecol.common.model.IndianNationType;
import net.sf.freecol.common.model.NationType;
import net.sf.freecol.common.model.UnitType;
import net.sf.freecol.common.util.ImageUtils;
import net.sf.freecol.common.util.RandomChoice;
@ -86,7 +87,7 @@ public class NationTypeDetailPanel
nations.addAll(getSpecification().getEuropeanNationTypes());
nations.addAll(getSpecification().getREFNationTypes());
nations.addAll(getSpecification().getIndianNationTypes());
ImageIcon icon = new ImageIcon(getImageLibrary().getLibertyImage());
ImageIcon icon = new ImageIcon(ImageUtils.createCenteredImage(getImageLibrary().getLibertyImage(), getListItemIconSize()));
for (NationType type : nations) {
// Suppress special case. FIXME: This is a kludge
if ("model.nationType.optionOnly".equals(type.getId())) continue;

View File

@ -45,6 +45,7 @@ import net.sf.freecol.common.model.AbstractGoods;
import net.sf.freecol.common.model.Modifier;
import net.sf.freecol.common.model.ResourceType;
import net.sf.freecol.common.model.TileType;
import net.sf.freecol.common.util.ImageUtils;
/**
@ -76,17 +77,9 @@ public class TerrainDetailPanel
= new DefaultMutableTreeNode(new ColopediaTreeItem(this, getId(),
getName(), null));
for (TileType t : getSpecification().getTileTypeList()) {
BufferedImage tileImage
= getImageLibrary().getTileImageWithOverlayAndForest(t,
new Dimension(-1, ImageLibrary.ICON_SIZE.height));
BufferedImage image = new BufferedImage(tileImage.getWidth(null),
ImageLibrary.ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(tileImage, 0,
(ImageLibrary.ICON_SIZE.height - tileImage.getHeight(null)) / 2,
null);
g.dispose();
ImageIcon icon = new ImageIcon(image);
final Dimension size = getListItemIconSize();
final BufferedImage tileImage = getImageLibrary().getTileImageWithOverlayAndForest(t, new Dimension(-1, size.height));
final ImageIcon icon = new ImageIcon(ImageUtils.createCenteredImage(tileImage, size));
node.add(buildItem(t, icon));
}
root.add(node);

View File

@ -221,6 +221,38 @@ public class ImageUtils {
g2d.setPaint(paint);
g2d.fillRect(x, y, width, height);
}
/**
* Creates a new image of the given size with the provided image centered.
*
* @param image The image to be drawn in the center (both vertically and horizontally) of
* the new image.
* @param size The size of the new image.
* @return A new image.
*/
public static BufferedImage createCenteredImage(BufferedImage image, Dimension size) {
return createCenteredImage(image, size.width, size.height);
}
/**
* Creates a new image of the given size with the provided image centered.
*
* @param image The image to be drawn in the center (both vertically and horizontally) of
* the new image.
* @param width The width of the new image.
* @param height The height of the new image.
* @return A new image.
*/
public static BufferedImage createCenteredImage(BufferedImage image, int width, int height) {
final int x = (width - image.getWidth(null)) / 2;
final int y = (height - image.getHeight(null)) / 2;
final BufferedImage centeredImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = centeredImage.createGraphics();
g.drawImage(image, x, y, null);
g.dispose();
return centeredImage;
}
/**
* Given a dimension with potential wildcard (non-positive) parts,