A lot of view performance optimizations

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@162 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-05-06 01:35:21 +02:00
parent 193e9247fa
commit 794e3573f6
6 changed files with 205 additions and 132 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 KiB

View file

@ -52,7 +52,7 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
* the grid scale * the grid scale
*/ */
public AbstractStonePanel(float scale) { public AbstractStonePanel(float scale) {
super(true); // Set double buffered super(false); // Unset double buffered
stonePainter = new StonePainter(scale); stonePainter = new StonePainter(scale);

View file

@ -39,6 +39,8 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
} }
private BufferedImage scaledBackground = BACKGROUND; private BufferedImage scaledBackground = BACKGROUND;
private boolean repaintAll = true;
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
/** /**
@ -58,6 +60,8 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
setSize(new Dimension(HAND_WIDTH * getStonePainter().getStoneWidth() setSize(new Dimension(HAND_WIDTH * getStonePainter().getStoneWidth()
+ insets.left + insets.right, getHeight())); + insets.left + insets.right, getHeight()));
repaintAll = true;
} }
}); });
} }
@ -80,8 +84,10 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
int x = insets.left, y = insets.top, width = getWidth() - insets.left int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom; - insets.right, height = getHeight() - insets.top - insets.bottom;
Graphics2D g = (Graphics2D) g1.create(x, y, width, height); Graphics2D g = (Graphics2D) g1.create(x, y, width, height);
int size = height / HAND_HEIGHT; int size = height / HAND_HEIGHT;
if (repaintAll) {
if (scaledBackground.getHeight() != size) if (scaledBackground.getHeight() != size)
scaledBackground = getScaledBackground(size); scaledBackground = getScaledBackground(size);
@ -90,11 +96,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
g.drawImage(scaledBackground, xpos, size * i, null); g.drawImage(scaledBackground, xpos, size * i, null);
} }
} }
}
getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Pair<Stone, Position> entry : getStones()) { for (Pair<Stone, Position> entry : getStones()) {
getStonePainter().paintStone(g, entry.getFirst(), entry.getSecond(), getStonePainter().paintStone(g, entry.getFirst(), entry.getSecond(),
@ -105,6 +107,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
@Override @Override
public void setStones(Iterable<Pair<Stone, Position>> stones) { public void setStones(Iterable<Pair<Stone, Position>> stones) {
super.setStones(stones); super.setStones(stones);
repaintAll = true;
repaint(); repaint();
} }

View file

@ -1,14 +1,16 @@
package jrummikub.view.impl; package jrummikub.view.impl;
import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import jrummikub.model.Position; import jrummikub.model.Position;
@ -22,7 +24,12 @@ import jrummikub.view.IStoneCollectionPanel;
@SuppressWarnings("serial") @SuppressWarnings("serial")
class StoneCollectionPanel extends AbstractStonePanel implements class StoneCollectionPanel extends AbstractStonePanel implements
IStoneCollectionPanel { IStoneCollectionPanel {
private final static int INSET = 7; private final static ImageIcon BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/dark_felt.png"));
/**
* The width of the border of the collection panel
*/
public final static int INSET = 7;
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
@ -31,13 +38,27 @@ class StoneCollectionPanel extends AbstractStonePanel implements
*/ */
StoneCollectionPanel() { StoneCollectionPanel() {
setOpaque(false); setOpaque(false);
setVisible(false);
setBorder(new EmptyBorder(INSET, INSET, INSET, INSET)); setBorder(new EmptyBorder(INSET, INSET, INSET, INSET));
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
});
} }
private void rescale() { private void rescale() {
setSize(getStonePainter().getStoneWidth() * selectedStones.size() + 2 Insets insets = getInsets();
int height = getHeight() - insets.top - insets.bottom;
getStonePainter().setScale(height * StonePainter.HEIGHT_SCALE);
repaint();
/*
* setSize(getStonePainter().getStoneWidth() * selectedStones.size() + 2
* INSET, getStonePainter().getStoneHeight() + 2 * INSET); * INSET, getStonePainter().getStoneHeight() + 2 * INSET);
*/
} }
/** /**
@ -46,12 +67,12 @@ class StoneCollectionPanel extends AbstractStonePanel implements
* @param height * @param height
* the height in pixels * the height in pixels
*/ */
void setStoneHeight(int height) { /*
getStonePainter().setScale(height * StonePainter.HEIGHT_SCALE); * void setStoneHeight(int height) { getStonePainter().setScale(height *
* StonePainter.HEIGHT_SCALE);
rescale(); *
repaint(); * rescale(); repaint(); //}
} */
/** /**
* Sets the stones to be shown in the collection * Sets the stones to be shown in the collection
@ -71,27 +92,34 @@ class StoneCollectionPanel extends AbstractStonePanel implements
} }
setStones(stones); setStones(stones);
if (selectedStones.isEmpty()) {
setVisible(false);
} else {
rescale();
setVisible(true);
repaint(); repaint();
} }
@Override
protected Pair<Integer, Integer> getTranslation() {
int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
* INSET;
int x = (getWidth() - width) / 2;
return new Pair<Integer, Integer>(x + INSET, INSET);
} }
@Override @Override
public void paintComponent(Graphics g1) { public void paintComponent(Graphics g1) {
Insets insets = getInsets(); int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
int x = insets.left, y = insets.top, width = getWidth() - insets.left * INSET, height = getHeight();
- insets.right, height = getHeight() - insets.top - insets.bottom; int x = (getWidth() - width) / 2;
Graphics2D g = (Graphics2D) g1.create(x, y, width, height); Graphics2D g = (Graphics2D) g1.create(x, 0, width, height);
if (!selectedStones.isEmpty()) { if (!selectedStones.isEmpty()) {
g1.setColor(new Color(0, 0, 0, 0.25f)); for (int xpos = 0; xpos < getWidth(); xpos += BACKGROUND.getIconWidth()) {
g1.fillRoundRect(0, 0, getWidth(), getHeight(), INSET, INSET); for (int ypos = 0; ypos < getHeight(); ypos += BACKGROUND
.getIconHeight()) {
BACKGROUND.paintIcon(this, g, xpos, ypos);
}
}
g.translate(INSET, INSET);
float xpos = 0; float xpos = 0;

View file

@ -6,9 +6,13 @@ import java.awt.Font;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke; import java.awt.Stroke;
import java.awt.geom.GeneralPath; import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import jrummikub.model.Position; import jrummikub.model.Position;
import jrummikub.model.Stone; import jrummikub.model.Stone;
@ -31,6 +35,9 @@ class StonePainter {
private static final float BRIGHTER_SCALE = 1.15f; private static final float BRIGHTER_SCALE = 1.15f;
private Map<StoneColor, Map<Integer, BufferedImage>> defaultStones;
private Map<StoneColor, Map<Integer, BufferedImage>> selectedStones;
/** /**
* The width of one pixel in the scale of 1.0 * The width of one pixel in the scale of 1.0
*/ */
@ -77,6 +84,7 @@ class StonePainter {
*/ */
public void setScale(float scale) { public void setScale(float scale) {
this.scale = scale; this.scale = scale;
prepaint();
} }
/** /**
@ -114,12 +122,59 @@ class StonePainter {
return (int) (DEFAULT_WIDTH * scale / ASPECT_RATIO); return (int) (DEFAULT_WIDTH * scale / ASPECT_RATIO);
} }
private BufferedImage prepaintStone(Color fg, Color bg, int value) {
Rectangle r = new Rectangle(0, 0, getStoneWidth(), getStoneHeight());
BufferedImage img = new BufferedImage(r.width, r.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
paintStoneBackground(g, r, bg);
if (value == 0) {
paintJoker(g, r, fg);
} else {
paintStoneValue(g, r, fg, value);
}
paintCircle(g, r, bg);
return img;
}
private Map<Integer, BufferedImage> prepaintColor(Color fg, Color bg) {
Map<Integer, BufferedImage> images = new HashMap<Integer, BufferedImage>();
for (int i = 0; i <= 13; ++i) {
images.put(i, prepaintStone(fg, bg, i));
}
return images;
}
private void prepaint() {
defaultStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
selectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
Color defaultBackground = BACKGROUND_COLOR;
Color selectedBackground = SELECTED_COLOR;
for (StoneColor color : StoneColor.values()) {
Color defaultFg = getColor(color);
Color selectedFg = defaultFg.darker();
defaultStones.put(color, prepaintColor(defaultFg, defaultBackground));
selectedStones.put(color, prepaintColor(selectedFg, selectedBackground));
}
}
/** /**
* @param scale * @param scale
* the scaling factor for the grid coordinates * the scaling factor for the grid coordinates
*/ */
StonePainter(float scale) { StonePainter(float scale) {
this.scale = scale; setScale(scale);
} }
private void paintStoneBackground(Graphics2D g, Rectangle r, Color background) { private void paintStoneBackground(Graphics2D g, Rectangle r, Color background) {
@ -250,25 +305,19 @@ class StonePainter {
* if selected is true the stone will be painted darker * if selected is true the stone will be painted darker
*/ */
public void paintStone(Graphics2D g, Stone stone, Position p, boolean selected) { public void paintStone(Graphics2D g, Stone stone, Position p, boolean selected) {
Color background = selected ? SELECTED_COLOR : BACKGROUND_COLOR; // Color background = selected ? SELECTED_COLOR : BACKGROUND_COLOR;
int width = getStoneWidth(); int width = getStoneWidth();
int height = getStoneHeight(); int height = getStoneHeight();
int x = (int) (p.getX() * width), y = (int) (p.getY() * height);
Rectangle rect = new Rectangle((int) (p.getX() * width),
(int) (p.getY() * height), width, height);
paintStoneBackground(g, rect, background);
Color color = getColor(stone.getColor());
if (selected)
color = color.darker();
if (stone.isJoker()) { if (stone.isJoker()) {
paintJoker(g, rect, color); g.drawImage(
(selected ? selectedStones : defaultStones).get(stone.getColor())
.get(0), x, y, null);
} else { } else {
paintStoneValue(g, rect, color, stone.getValue()); g.drawImage(
} (selected ? selectedStones : defaultStones).get(stone.getColor())
.get(stone.getValue()), x, y, null);
paintCircle(g, rect, background); }
} }
} }

View file

@ -4,10 +4,8 @@ import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,7 +29,7 @@ import jrummikub.view.ITablePanel;
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
class TablePanel extends AbstractStonePanel implements ITablePanel { class TablePanel extends AbstractStonePanel implements ITablePanel {
private final static ImageIcon background = new ImageIcon( private final static ImageIcon BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/felt.png")); HandPanel.class.getResource("/jrummikub/resource/felt.png"));
private final static float MIN_VISIBLE_WIDTH = 15; private final static float MIN_VISIBLE_WIDTH = 15;
@ -141,15 +139,12 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
stoneCollection = new StoneCollectionPanel(); stoneCollection = new StoneCollectionPanel();
add(stoneCollection); add(stoneCollection);
ComponentListener rescaleListener = new ComponentAdapter() { addComponentListener(new ComponentAdapter() {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
rescale(); rescale();
} }
}; });
addComponentListener(rescaleListener);
stoneCollection.addComponentListener(rescaleListener);
} }
private Rectangle2D calculateTableExtent() { private Rectangle2D calculateTableExtent() {
@ -187,9 +182,6 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
topPlayerLabel.setBounds(x, y, width, height); topPlayerLabel.setBounds(x, y, width, height);
rightPlayerLabel.setBounds(x, y, width, height); rightPlayerLabel.setBounds(x, y, width, height);
stoneCollection.setLocation(x + width / 2 - stoneCollection.getWidth() / 2,
y + height - stoneCollection.getHeight() - COLLECTION_GAP);
Rectangle2D extent = calculateTableExtent(); Rectangle2D extent = calculateTableExtent();
float widthScale = width / (float) extent.getWidth() float widthScale = width / (float) extent.getWidth()
@ -199,7 +191,11 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
getStonePainter().setScale(Math.min(widthScale, heightScale)); getStonePainter().setScale(Math.min(widthScale, heightScale));
stoneCollection.setStoneHeight((int) (height * COLLECTION_RATIO)); int collectionHeight = (int) (height * COLLECTION_RATIO) + 2
* StoneCollectionPanel.INSET;
stoneCollection
.setBounds(x, y + height - collectionHeight - COLLECTION_GAP, width,
collectionHeight);
repaint(); repaint();
} }
@ -267,9 +263,9 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
protected void paintComponent(Graphics g1) { protected void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1; Graphics2D g = (Graphics2D) g1;
for (int x = 0; x < getWidth(); x += background.getIconWidth()) { for (int x = 0; x < getWidth(); x += BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += background.getIconHeight()) { for (int y = 0; y < getHeight(); y += BACKGROUND.getIconHeight()) {
background.paintIcon(this, g, x, y); BACKGROUND.paintIcon(this, g, x, y);
} }
} }
@ -278,9 +274,6 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
Pair<Integer, Integer> translation = getTranslation(); Pair<Integer, Integer> translation = getTranslation();
g.translate(translation.getFirst(), translation.getSecond()); g.translate(translation.getFirst(), translation.getSecond());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Pair<StoneSet, Position> entry : stoneSets) { for (Pair<StoneSet, Position> entry : stoneSets) {
paintStoneSet(g, entry.getFirst(), entry.getSecond()); paintStoneSet(g, entry.getFirst(), entry.getSecond());
} }