forked from External/mage
Merge pull request #2030 from draxdyn/performance
Improve GUI performance
This commit is contained in:
commit
d220c739a8
19 changed files with 673 additions and 376 deletions
|
|
@ -8,6 +8,8 @@ import java.util.Timer;
|
|||
import java.util.TimerTask;
|
||||
|
||||
public abstract class Animation {
|
||||
private static boolean ENABLED = true;
|
||||
|
||||
private static final long TARGET_MILLIS_PER_FRAME = 30;
|
||||
|
||||
private static Timer timer = new Timer("Animation", true);
|
||||
|
|
@ -25,6 +27,18 @@ public abstract class Animation {
|
|||
}
|
||||
|
||||
public Animation (final long duration, long delay) {
|
||||
if(!ENABLED) {
|
||||
UI.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run () {
|
||||
start();
|
||||
//update(1.0f);
|
||||
end();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
timerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run () {
|
||||
|
|
@ -171,6 +185,12 @@ public abstract class Animation {
|
|||
|
||||
@Override
|
||||
protected void end () {
|
||||
if (!state) {
|
||||
parent.toggleTransformed();
|
||||
}
|
||||
state = true;
|
||||
panel.transformAngle = 0;
|
||||
|
||||
parent.onEndAnimation();
|
||||
parent.repaint();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package org.mage.card.arcane;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
|
|
@ -23,6 +25,7 @@ import java.awt.image.BufferedImage;
|
|||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.UUID;
|
||||
import javax.swing.BorderFactory;
|
||||
|
|
@ -38,6 +41,7 @@ import mage.cards.action.TransferData;
|
|||
import mage.client.dialog.PreferencesDialog;
|
||||
import mage.client.plugins.adapters.MageActionCallback;
|
||||
import mage.client.plugins.impl.Plugins;
|
||||
import mage.client.util.ImageCaches;
|
||||
import mage.client.util.ImageHelper;
|
||||
import mage.client.util.audio.AudioManager;
|
||||
import mage.components.ImagePanel;
|
||||
|
|
@ -52,8 +56,7 @@ import mage.view.PermanentView;
|
|||
import mage.view.StackAbilityView;
|
||||
import net.java.truevfs.access.TFile;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.mage.card.arcane.ScaledImagePanel.MultipassType;
|
||||
import org.mage.card.arcane.ScaledImagePanel.ScalingType;
|
||||
import org.jdesktop.swingx.graphics.GraphicsUtilities;
|
||||
import static org.mage.plugins.card.constants.Constants.THUMBNAIL_SIZE_FULL;
|
||||
import org.mage.plugins.card.dl.sources.DirectLinksForDownload;
|
||||
import org.mage.plugins.card.images.ImageCache;
|
||||
|
|
@ -160,6 +163,111 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
// if this is set, it's opened if the user right clicks on the card panel
|
||||
private JPopupMenu popupMenu;
|
||||
|
||||
private static Map<Key, BufferedImage> IMAGE_CACHE;
|
||||
|
||||
private final static class Key
|
||||
{
|
||||
final int width;
|
||||
final int height;
|
||||
final int cardWidth;
|
||||
final int cardHeight;
|
||||
final int cardXOffset;
|
||||
final int cardYOffset;
|
||||
final boolean hasImage;
|
||||
final boolean isSelected;
|
||||
final boolean isChoosable;
|
||||
final boolean isPlayable;
|
||||
final boolean canAttack;
|
||||
|
||||
public Key(int width, int height, int cardWidth, int cardHeight, int cardXOffset, int cardYOffset, boolean hasImage, boolean isSelected, boolean isChoosable, boolean isPlayable, boolean canAttack) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.cardWidth = cardWidth;
|
||||
this.cardHeight = cardHeight;
|
||||
this.cardXOffset = cardXOffset;
|
||||
this.cardYOffset = cardYOffset;
|
||||
this.hasImage = hasImage;
|
||||
this.isSelected = isSelected;
|
||||
this.isChoosable = isChoosable;
|
||||
this.isPlayable = isPlayable;
|
||||
this.canAttack = canAttack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 3;
|
||||
hash = 19 * hash + this.width;
|
||||
hash = 19 * hash + this.height;
|
||||
hash = 19 * hash + this.cardWidth;
|
||||
hash = 19 * hash + this.cardHeight;
|
||||
hash = 19 * hash + this.cardXOffset;
|
||||
hash = 19 * hash + this.cardYOffset;
|
||||
hash = 19 * hash + (this.hasImage ? 1 : 0);
|
||||
hash = 19 * hash + (this.isSelected ? 1 : 0);
|
||||
hash = 19 * hash + (this.isChoosable ? 1 : 0);
|
||||
hash = 19 * hash + (this.isPlayable ? 1 : 0);
|
||||
hash = 19 * hash + (this.canAttack ? 1 : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Key other = (Key) obj;
|
||||
if (this.width != other.width) {
|
||||
return false;
|
||||
}
|
||||
if (this.height != other.height) {
|
||||
return false;
|
||||
}
|
||||
if (this.cardWidth != other.cardWidth) {
|
||||
return false;
|
||||
}
|
||||
if (this.cardHeight != other.cardHeight) {
|
||||
return false;
|
||||
}
|
||||
if (this.cardXOffset != other.cardXOffset) {
|
||||
return false;
|
||||
}
|
||||
if (this.cardYOffset != other.cardYOffset) {
|
||||
return false;
|
||||
}
|
||||
if (this.hasImage != other.hasImage) {
|
||||
return false;
|
||||
}
|
||||
if (this.isSelected != other.isSelected) {
|
||||
return false;
|
||||
}
|
||||
if (this.isChoosable != other.isChoosable) {
|
||||
return false;
|
||||
}
|
||||
if (this.isPlayable != other.isPlayable) {
|
||||
return false;
|
||||
}
|
||||
if (this.canAttack != other.canAttack) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
IMAGE_CACHE = ImageCaches.register(new MapMaker().softValues().makeComputingMap(new Function<Key, BufferedImage>() {
|
||||
@Override
|
||||
public BufferedImage apply(Key key) {
|
||||
return createImage(key);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public CardPanel(CardView newGameCard, UUID gameId, final boolean loadImage, ActionCallback callback, final boolean foil, Dimension dimension) {
|
||||
this.gameCard = newGameCard;
|
||||
this.callback = callback;
|
||||
|
|
@ -304,9 +412,6 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
imagePanel = new ScaledImagePanel();
|
||||
imagePanel.setBorder(BorderFactory.createLineBorder(Color.white));
|
||||
add(imagePanel);
|
||||
imagePanel.setScaleLarger(true);
|
||||
imagePanel.setScalingType(ScalingType.nearestNeighbor);
|
||||
imagePanel.setScalingMultiPassType(MultipassType.none);
|
||||
|
||||
String cardType = getType(newGameCard);
|
||||
tooltipText.setText(getText(cardType, newGameCard));
|
||||
|
|
@ -387,7 +492,7 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
titleText.setText(!displayTitleAnyway && hasImage ? "" : card.getName());
|
||||
}
|
||||
|
||||
private void setImage(Image srcImage) {
|
||||
private void setImage(BufferedImage srcImage) {
|
||||
synchronized (imagePanel) {
|
||||
imagePanel.setImage(srcImage);
|
||||
repaint();
|
||||
|
|
@ -413,10 +518,6 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
return zone;
|
||||
}
|
||||
|
||||
public void setScalingType(ScalingType scalingType) {
|
||||
imagePanel.setScalingType(scalingType);
|
||||
}
|
||||
|
||||
public void setDisplayEnabled(boolean displayEnabled) {
|
||||
this.displayEnabled = displayEnabled;
|
||||
}
|
||||
|
|
@ -484,15 +585,28 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
Graphics2D g2d = (Graphics2D)(g.create());
|
||||
|
||||
if (alpha != 1.0f) {
|
||||
AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha);
|
||||
g2d.setComposite(composite);
|
||||
}
|
||||
|
||||
if (!hasImage) {
|
||||
g2d.drawImage(IMAGE_CACHE.get(new Key(getWidth(), getHeight(), cardWidth, cardHeight, cardXOffset, cardYOffset, hasImage, isSelected, isChoosable, isPlayable, canAttack)), 0, 0, null);
|
||||
g2d.dispose();
|
||||
}
|
||||
|
||||
private static BufferedImage createImage(Key key) {
|
||||
int cardWidth = key.cardWidth;
|
||||
int cardHeight = key.cardHeight;
|
||||
int cardXOffset = key.cardXOffset;
|
||||
int cardYOffset = key.cardYOffset;
|
||||
|
||||
BufferedImage image = GraphicsUtilities.createCompatibleTranslucentImage(key.width, key.height);
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
if (!key.hasImage) {
|
||||
g2d.setColor(new Color(30, 200, 200, 120));
|
||||
} else {
|
||||
g2d.setColor(new Color(0, 0, 0, 0));
|
||||
|
|
@ -501,19 +615,19 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
int cornerSize = Math.max(4, Math.round(cardWidth * ROUNDED_CORNER_SIZE));
|
||||
g2d.fillRoundRect(cardXOffset, cardYOffset, cardWidth, cardHeight, cornerSize, cornerSize);
|
||||
|
||||
if (isSelected) {
|
||||
if (key.isSelected) {
|
||||
g2d.setColor(Color.green);
|
||||
g2d.fillRoundRect(cardXOffset + 1, cardYOffset + 1, cardWidth - 2, cardHeight - 2, cornerSize, cornerSize);
|
||||
} else if (isChoosable) {
|
||||
} else if (key.isChoosable) {
|
||||
g2d.setColor(new Color(250, 250, 0, 230));
|
||||
g2d.fillRoundRect(cardXOffset + 1, cardYOffset + 1, cardWidth - 2, cardHeight - 2, cornerSize, cornerSize);
|
||||
} else if (isPlayable) {
|
||||
} else if (key.isPlayable) {
|
||||
g2d.setColor(new Color(153, 102, 204, 200));
|
||||
//g2d.fillRoundRect(cardXOffset + 1, cardYOffset + 1, cardWidth - 2, cardHeight - 2, cornerSize, cornerSize);
|
||||
g2d.fillRoundRect(cardXOffset, cardYOffset, cardWidth, cardHeight, cornerSize, cornerSize);
|
||||
}
|
||||
|
||||
if (canAttack) {
|
||||
if (key.canAttack) {
|
||||
g2d.setColor(new Color(0, 0, 255, 230));
|
||||
g2d.fillRoundRect(cardXOffset + 1, cardYOffset + 1, cardWidth - 2, cardHeight - 2, cornerSize, cornerSize);
|
||||
}
|
||||
|
|
@ -524,6 +638,9 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
g2d.setColor(new Color(200,10,10,200));
|
||||
g2d.fillRoundRect(cardXOffset+1, cardYOffset+1, cardWidth-2, cardHeight-2, cornerSize, cornerSize);
|
||||
}*/
|
||||
g2d.dispose();
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -619,12 +736,6 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
|
||||
ptText.setLocation(cardXOffset + ptX - TEXT_GLOW_SIZE / 2 - offsetX, cardYOffset + ptY - TEXT_GLOW_SIZE / 2);
|
||||
}
|
||||
|
||||
if (isAnimationPanel || cardWidth < 200) {
|
||||
imagePanel.setScalingType(ScalingType.nearestNeighbor);
|
||||
} else {
|
||||
imagePanel.setScalingType(ScalingType.bilinear);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -1063,7 +1174,7 @@ public class CardPanel extends MagePermanent implements MouseListener, MouseMoti
|
|||
if (gameCard.hideInfo()) {
|
||||
return;
|
||||
}
|
||||
if (getMousePosition(true) != null) {
|
||||
if (this.contains(e.getPoint())) {
|
||||
return;
|
||||
}
|
||||
if (tooltipShowing) {
|
||||
|
|
|
|||
|
|
@ -1,15 +1,30 @@
|
|||
package org.mage.card.arcane;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.font.FontRenderContext;
|
||||
import java.awt.font.LineBreakMeasurer;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.awt.font.TextLayout;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.text.AttributedCharacterIterator;
|
||||
import java.text.AttributedString;
|
||||
import java.text.BreakIterator;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import mage.client.util.ImageCaches;
|
||||
import org.jdesktop.swingx.graphics.GraphicsUtilities;
|
||||
|
||||
public class GlowText extends JLabel {
|
||||
private static final long serialVersionUID = 1827677946939348001L;
|
||||
|
|
@ -19,6 +34,109 @@ public class GlowText extends JLabel {
|
|||
private Color glowColor;
|
||||
private boolean wrap;
|
||||
private int lineCount = 0;
|
||||
private static Map<Key, BufferedImage> IMAGE_CACHE;
|
||||
|
||||
private final static class Key
|
||||
{
|
||||
final int width;
|
||||
final int height;
|
||||
final String text;
|
||||
final Map<TextAttribute,?> fontAttributes;
|
||||
final Color color;
|
||||
final int glowSize;
|
||||
final float glowIntensity;
|
||||
final Color glowColor;
|
||||
final boolean wrap;
|
||||
|
||||
// used to pass the native font to the create function so we don't waste performance recreating it, but without holding onto the native object
|
||||
final transient WeakReference<Font> originalFont;
|
||||
|
||||
Font getFont() {
|
||||
Font res = this.originalFont.get();
|
||||
if(res == null)
|
||||
res = Font.getFont(this.fontAttributes);
|
||||
return res;
|
||||
}
|
||||
|
||||
public Key(int width, int height, String text, Font font, Color color, int glowSize, float glowIntensity, Color glowColor, boolean wrap) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.text = text;
|
||||
this.originalFont = new WeakReference<>(font);
|
||||
this.fontAttributes = font.getAttributes();
|
||||
this.color = color;
|
||||
this.glowSize = glowSize;
|
||||
this.glowIntensity = glowIntensity;
|
||||
this.glowColor = glowColor;
|
||||
this.wrap = wrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 3;
|
||||
hash = 23 * hash + this.width;
|
||||
hash = 23 * hash + this.height;
|
||||
hash = 23 * hash + Objects.hashCode(this.text);
|
||||
hash = 23 * hash + Objects.hashCode(this.fontAttributes);
|
||||
hash = 23 * hash + Objects.hashCode(this.color);
|
||||
hash = 23 * hash + this.glowSize;
|
||||
hash = 23 * hash + Float.floatToIntBits(this.glowIntensity);
|
||||
hash = 23 * hash + Objects.hashCode(this.glowColor);
|
||||
hash = 23 * hash + (this.wrap ? 1 : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Key other = (Key) obj;
|
||||
if (this.width != other.width) {
|
||||
return false;
|
||||
}
|
||||
if (this.height != other.height) {
|
||||
return false;
|
||||
}
|
||||
if (this.glowSize != other.glowSize) {
|
||||
return false;
|
||||
}
|
||||
if (Float.floatToIntBits(this.glowIntensity) != Float.floatToIntBits(other.glowIntensity)) {
|
||||
return false;
|
||||
}
|
||||
if (this.wrap != other.wrap) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(this.text, other.text)) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(this.fontAttributes, other.fontAttributes)) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(this.color, other.color)) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(this.glowColor, other.glowColor)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
IMAGE_CACHE = ImageCaches.register(new MapMaker().softValues().makeComputingMap(new Function<Key, BufferedImage>() {
|
||||
@Override
|
||||
public BufferedImage apply(Key key) {
|
||||
return createImage(key);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public void setGlow (Color glowColor, int size, float intensity) {
|
||||
this.glowColor = glowColor;
|
||||
|
|
@ -38,32 +156,32 @@ public class GlowText extends JLabel {
|
|||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText (String text) {
|
||||
super.setText(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint (Graphics g) {
|
||||
if (getText().length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
g.drawImage(IMAGE_CACHE.get(new Key(getWidth(), getHeight(), getText(), getFont(), getForeground(), glowSize, glowIntensity, glowColor, wrap)), 0, 0, null);
|
||||
}
|
||||
|
||||
private static BufferedImage createImage (Key key) {
|
||||
Dimension size = new Dimension(key.width, key.height);
|
||||
BufferedImage image = GraphicsUtilities.createCompatibleTranslucentImage(size.width, size.height);
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
|
||||
Dimension size = getSize();
|
||||
int textX = 0, textY = 0;
|
||||
int wrapWidth = Math.max(0, wrap ? size.width - glowSize : Integer.MAX_VALUE);
|
||||
int wrapWidth = Math.max(0, key.wrap ? size.width - key.glowSize : Integer.MAX_VALUE);
|
||||
|
||||
AttributedString attributedString = new AttributedString(getText());
|
||||
attributedString.addAttribute(TextAttribute.FONT, getFont());
|
||||
AttributedString attributedString = new AttributedString(key.text);
|
||||
attributedString.addAttribute(TextAttribute.FONT, key.getFont());
|
||||
AttributedCharacterIterator charIterator = attributedString.getIterator();
|
||||
FontRenderContext fontContext = g2d.getFontRenderContext();
|
||||
|
||||
LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, BreakIterator.getWordInstance(Locale.ENGLISH), fontContext);
|
||||
lineCount = 0;
|
||||
int lineCount = 0;
|
||||
while (measurer.getPosition() < charIterator.getEndIndex()) {
|
||||
//TextLayout textLayout = measurer.nextLayout(wrapWidth);
|
||||
lineCount++;
|
||||
|
|
@ -83,23 +201,21 @@ public class GlowText extends JLabel {
|
|||
float ascent = textLayout.getAscent();
|
||||
textY += ascent; // Move down to baseline.
|
||||
|
||||
g2d.setColor(glowColor);
|
||||
g2d.setColor(key.glowColor);
|
||||
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
|
||||
int glowSize = key.glowSize;
|
||||
textLayout.draw(g2d, textX + glowSize / 2 + 1, textY + glowSize / 2 - 1);
|
||||
textLayout.draw(g2d, textX + glowSize / 2 + 1, textY + glowSize / 2 + 1);
|
||||
textLayout.draw(g2d, textX + glowSize / 2 - 1, textY + glowSize / 2 - 1);
|
||||
textLayout.draw(g2d, textX + glowSize / 2 - 1, textY + glowSize / 2 + 1);
|
||||
|
||||
g2d.setColor(getForeground());
|
||||
g2d.setColor(key.color);
|
||||
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
|
||||
textLayout.draw(g2d, textX + glowSize / 2, textY + glowSize / 2);
|
||||
|
||||
textY += textLayout.getDescent() + textLayout.getLeading(); // Move down to top of next line.
|
||||
}
|
||||
}
|
||||
|
||||
public int getLineCount() {
|
||||
return this.lineCount;
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setGlowColor(Color glowColor) {
|
||||
|
|
|
|||
|
|
@ -7,188 +7,35 @@ import java.awt.RenderingHints;
|
|||
import java.awt.image.BufferedImage;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import mage.client.util.TransformedImageCache;
|
||||
|
||||
public class ScaledImagePanel extends JPanel {
|
||||
private static final long serialVersionUID = -1523279873208605664L;
|
||||
private volatile Image srcImage;
|
||||
|
||||
private ScalingType scalingType = ScalingType.bilinear;
|
||||
private boolean scaleLarger;
|
||||
private MultipassType multiPassType = MultipassType.bilinear;
|
||||
private volatile BufferedImage srcImage;
|
||||
|
||||
public ScaledImagePanel () {
|
||||
super(false);
|
||||
setOpaque(false);
|
||||
}
|
||||
|
||||
public void setImage(Image srcImage) {
|
||||
public void setImage(BufferedImage srcImage) {
|
||||
this.srcImage = srcImage;
|
||||
}
|
||||
|
||||
public void clearImage () {
|
||||
srcImage = null;
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void setScalingMultiPassType (MultipassType multiPassType) {
|
||||
this.multiPassType = multiPassType;
|
||||
}
|
||||
|
||||
public void setScalingType (ScalingType scalingType) {
|
||||
this.scalingType = scalingType;
|
||||
}
|
||||
|
||||
public void setScaleLarger (boolean scaleLarger) {
|
||||
this.scaleLarger = scaleLarger;
|
||||
}
|
||||
|
||||
public boolean hasImage () {
|
||||
return srcImage != null;
|
||||
}
|
||||
|
||||
private ScalingInfo getScalingInfo () {
|
||||
int panelWidth = getWidth();
|
||||
int panelHeight = getHeight();
|
||||
int srcWidth = srcImage.getWidth(null);
|
||||
int srcHeight = srcImage.getHeight(null);
|
||||
int targetWidth = srcWidth;
|
||||
int targetHeight = srcHeight;
|
||||
if (scaleLarger || srcWidth > panelWidth || srcHeight > panelHeight) {
|
||||
targetWidth = Math.round(panelHeight * (srcWidth / (float)srcHeight));
|
||||
if (targetWidth > panelWidth) {
|
||||
targetHeight = Math.round(panelWidth * (srcHeight / (float)srcWidth));
|
||||
targetWidth = panelWidth;
|
||||
} else {
|
||||
targetHeight = panelHeight;
|
||||
}
|
||||
}
|
||||
ScalingInfo info = new ScalingInfo();
|
||||
info.targetWidth = targetWidth;
|
||||
info.targetHeight = targetHeight;
|
||||
info.srcWidth = srcWidth;
|
||||
info.srcHeight = srcHeight;
|
||||
info.x = panelWidth / 2 - targetWidth / 2;
|
||||
info.y = panelHeight / 2 - targetHeight / 2;
|
||||
return info;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint (Graphics g) {
|
||||
if (srcImage == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2 = (Graphics2D)g.create();
|
||||
ScalingInfo info = getScalingInfo();
|
||||
|
||||
switch (scalingType) {
|
||||
case nearestNeighbor:
|
||||
scaleWithDrawImage(g2, info, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
|
||||
break;
|
||||
case bilinear:
|
||||
scaleWithDrawImage(g2, info, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
break;
|
||||
case bicubic:
|
||||
scaleWithDrawImage(g2, info, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
break;
|
||||
case areaAveraging:
|
||||
scaleWithGetScaledInstance(g2, info, Image.SCALE_AREA_AVERAGING);
|
||||
break;
|
||||
case replicate:
|
||||
scaleWithGetScaledInstance(g2, info, Image.SCALE_REPLICATE);
|
||||
break;
|
||||
}
|
||||
g.drawImage(TransformedImageCache.getResizedImage(srcImage, getWidth(), getHeight()), 0, 0, null);
|
||||
}
|
||||
|
||||
private void scaleWithGetScaledInstance (Graphics2D g2, ScalingInfo info, int hints) {
|
||||
Image scaledImage = srcImage.getScaledInstance(info.targetWidth, info.targetHeight, hints);
|
||||
g2.drawImage(scaledImage, info.x, info.y, null);
|
||||
}
|
||||
|
||||
private void scaleWithDrawImage (Graphics2D g2, ScalingInfo info, Object hint) {
|
||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
|
||||
|
||||
int tempDestWidth = info.srcWidth / 2, tempDestHeight = info.srcHeight / 2;
|
||||
if (tempDestWidth < info.targetWidth) {
|
||||
tempDestWidth = info.targetWidth;
|
||||
}
|
||||
if (tempDestHeight < info.targetHeight) {
|
||||
tempDestHeight = info.targetHeight;
|
||||
}
|
||||
|
||||
// If not doing multipass or multipass only needs a single pass, just scale it once directly to the panel surface.
|
||||
if (multiPassType == MultipassType.none || (tempDestWidth == info.targetWidth && tempDestHeight == info.targetHeight)) {
|
||||
g2.drawImage(srcImage, info.x, info.y, info.targetWidth, info.targetHeight, null);
|
||||
return;
|
||||
}
|
||||
|
||||
BufferedImage tempImage = new BufferedImage(tempDestWidth, tempDestHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g2temp = tempImage.createGraphics();
|
||||
switch (multiPassType) {
|
||||
case nearestNeighbor:
|
||||
g2temp.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
|
||||
break;
|
||||
case bilinear:
|
||||
g2temp.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
break;
|
||||
case bicubic:
|
||||
g2temp.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
break;
|
||||
}
|
||||
// Render first pass from image to temp.
|
||||
g2temp.drawImage(srcImage, 0, 0, tempDestWidth, tempDestHeight, null);
|
||||
// Render passes between the first and last pass.
|
||||
int tempSrcWidth = tempDestWidth;
|
||||
int tempSrcHeight = tempDestHeight;
|
||||
while (true) {
|
||||
if (tempDestWidth > info.targetWidth) {
|
||||
tempDestWidth = tempDestWidth / 2;
|
||||
if (tempDestWidth < info.targetWidth) {
|
||||
tempDestWidth = info.targetWidth;
|
||||
}
|
||||
}
|
||||
|
||||
if (tempDestHeight > info.targetHeight) {
|
||||
tempDestHeight = tempDestHeight / 2;
|
||||
if (tempDestHeight < info.targetHeight) {
|
||||
tempDestHeight = info.targetHeight;
|
||||
}
|
||||
}
|
||||
|
||||
if (tempDestWidth == info.targetWidth && tempDestHeight == info.targetHeight) {
|
||||
break;
|
||||
}
|
||||
|
||||
g2temp.drawImage(tempImage, 0, 0, tempDestWidth, tempDestHeight, 0, 0, tempSrcWidth, tempSrcHeight, null);
|
||||
|
||||
tempSrcWidth = tempDestWidth;
|
||||
tempSrcHeight = tempDestHeight;
|
||||
}
|
||||
g2temp.dispose();
|
||||
// Render last pass from temp to panel surface.
|
||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
|
||||
g2.drawImage(tempImage, info.x, info.y, info.x + info.targetWidth, info.y + info.targetHeight, 0, 0, tempSrcWidth,
|
||||
tempSrcHeight, null);
|
||||
}
|
||||
|
||||
public Image getSrcImage() {
|
||||
public BufferedImage getSrcImage() {
|
||||
return srcImage;
|
||||
}
|
||||
|
||||
private static class ScalingInfo {
|
||||
public int targetWidth;
|
||||
public int targetHeight;
|
||||
public int srcWidth;
|
||||
public int srcHeight;
|
||||
public int x;
|
||||
public int y;
|
||||
}
|
||||
|
||||
public static enum MultipassType {
|
||||
none, nearestNeighbor, bilinear, bicubic
|
||||
}
|
||||
|
||||
public static enum ScalingType {
|
||||
nearestNeighbor, replicate, bilinear, bicubic, areaAveraging
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.mage.plugins.card.images;
|
||||
|
||||
import mage.client.util.TransformedImageCache;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ComputationException;
|
||||
import com.google.common.collect.MapMaker;
|
||||
|
|
@ -274,7 +275,7 @@ public class ImageCache {
|
|||
}
|
||||
|
||||
public static BufferedImage makeThumbnail(BufferedImage original, String path) {
|
||||
BufferedImage image = getResizedImage(original, Constants.THUMBNAIL_SIZE_FULL);
|
||||
BufferedImage image = TransformedImageCache.getResizedImage(original, Constants.THUMBNAIL_SIZE_FULL.width, Constants.THUMBNAIL_SIZE_FULL.height);
|
||||
TFile imageFile = getTFile(path);
|
||||
if (imageFile == null) {
|
||||
return null;
|
||||
|
|
@ -312,36 +313,7 @@ public class ImageCache {
|
|||
return original;
|
||||
}
|
||||
|
||||
ResampleOp resampleOp = new ResampleOp(tgtWidth, tgtHeight);
|
||||
BufferedImage image = resampleOp.filter(original, null);
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an image scaled to the size appropriate for the card picture
|
||||
* panel For future use.
|
||||
*/
|
||||
private static BufferedImage getFullSizeImage(BufferedImage original, double scale) {
|
||||
if (scale == 1) {
|
||||
return original;
|
||||
}
|
||||
ResampleOp resampleOp = new ResampleOp((int) (original.getWidth() * scale), (int) (original.getHeight() * scale));
|
||||
BufferedImage image = resampleOp.filter(original, null);
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an image scaled to the size appropriate for the card picture
|
||||
* panel
|
||||
*
|
||||
* @param original
|
||||
* @param sizeNeed
|
||||
* @return
|
||||
*/
|
||||
public static BufferedImage getResizedImage(BufferedImage original, Rectangle sizeNeed) {
|
||||
ResampleOp resampleOp = new ResampleOp(sizeNeed.width, sizeNeed.height);
|
||||
BufferedImage image = resampleOp.filter(original, null);
|
||||
return image;
|
||||
return TransformedImageCache.getResizedImage(original, tgtWidth, tgtHeight);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -364,11 +336,11 @@ public class ImageCache {
|
|||
}
|
||||
|
||||
double scale = Math.min((double) width / original.getWidth(), (double) height / original.getHeight());
|
||||
if (scale > 1) {
|
||||
scale = 1;
|
||||
if (scale >= 1) {
|
||||
return original;
|
||||
}
|
||||
|
||||
return getFullSizeImage(original, scale);
|
||||
return TransformedImageCache.getResizedImage(original, (int)(original.getWidth() * scale), (int)(original.getHeight() * scale));
|
||||
}
|
||||
|
||||
public static TFile getTFile(String path) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue