StonePainter: Paint stones lazily
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@336 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
2e24ee3e7f
commit
c78e8e6448
1 changed files with 77 additions and 84 deletions
|
@ -60,8 +60,7 @@ class StonePainter {
|
||||||
int g = (int) (color.getGreen() * BRIGHTER_SCALE);
|
int g = (int) (color.getGreen() * BRIGHTER_SCALE);
|
||||||
int b = (int) (color.getBlue() * BRIGHTER_SCALE);
|
int b = (int) (color.getBlue() * BRIGHTER_SCALE);
|
||||||
|
|
||||||
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255
|
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
|
||||||
: b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Color hover(Color color) {
|
private static Color hover(Color color) {
|
||||||
|
@ -69,8 +68,7 @@ class StonePainter {
|
||||||
int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
|
int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
|
||||||
int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
|
int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
|
||||||
|
|
||||||
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255
|
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
|
||||||
: b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Color getColor(StoneColor color) {
|
private static Color getColor(StoneColor color) {
|
||||||
|
@ -109,7 +107,7 @@ class StonePainter {
|
||||||
this.scale = 1;
|
this.scale = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
prepaint();
|
resetPrepaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,43 +159,57 @@ class StonePainter {
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<Integer, BufferedImage> prepaintColor(Color fg, Color bg) {
|
private BufferedImage getStoneImage(StoneColor color, int value, boolean selected,
|
||||||
Map<Integer, BufferedImage> images = new HashMap<Integer, BufferedImage>();
|
boolean hovered) {
|
||||||
|
Map<StoneColor, Map<Integer, BufferedImage>> stoneMap;
|
||||||
|
|
||||||
for (int i = 0; i <= 13; ++i) {
|
if (selected) {
|
||||||
images.put(i, prepaintStone(fg, bg, i));
|
if (hovered) {
|
||||||
|
stoneMap = hoveredSelectedStones;
|
||||||
|
} else {
|
||||||
|
stoneMap = selectedStones;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (hovered) {
|
||||||
|
stoneMap = hoveredStones;
|
||||||
|
} else {
|
||||||
|
stoneMap = defaultStones;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return images;
|
BufferedImage image = stoneMap.get(color).get(value);
|
||||||
|
|
||||||
|
if (image == null) {
|
||||||
|
Color background = BACKGROUND_COLOR;
|
||||||
|
Color foreground = getColor(color);
|
||||||
|
if (selected) {
|
||||||
|
background = background.darker();
|
||||||
|
foreground = foreground.darker();
|
||||||
|
}
|
||||||
|
if (hovered) {
|
||||||
|
background = hover(background);
|
||||||
|
foreground = hover(foreground);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepaint() {
|
image = prepaintStone(foreground, background, value);
|
||||||
|
|
||||||
|
stoneMap.get(color).put(value, image);
|
||||||
|
}
|
||||||
|
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetPrepaint() {
|
||||||
defaultStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
defaultStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
||||||
selectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
selectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
||||||
hoveredStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
hoveredStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
||||||
hoveredSelectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
hoveredSelectedStones = new HashMap<StoneColor, Map<Integer, BufferedImage>>();
|
||||||
|
|
||||||
Color defaultBackground = BACKGROUND_COLOR;
|
|
||||||
Color selectedBackground = BACKGROUND_COLOR.darker();
|
|
||||||
Color hoveredBackground = hover(defaultBackground);
|
|
||||||
Color hoveredSelectedBackground = hover(selectedBackground);
|
|
||||||
|
|
||||||
for (StoneColor color : StoneColor.values()) {
|
for (StoneColor color : StoneColor.values()) {
|
||||||
Color defaultFg = getColor(color);
|
defaultStones.put(color, new HashMap<Integer, BufferedImage>());
|
||||||
Color selectedFg = defaultFg.darker();
|
selectedStones.put(color, new HashMap<Integer, BufferedImage>());
|
||||||
Color hoveredFg = hover(defaultFg);
|
hoveredStones.put(color, new HashMap<Integer, BufferedImage>());
|
||||||
Color hoveredSelectedFg = hover(selectedFg);
|
hoveredSelectedStones.put(color, new HashMap<Integer, BufferedImage>());
|
||||||
|
|
||||||
defaultStones.put(color,
|
|
||||||
prepaintColor(defaultFg, defaultBackground));
|
|
||||||
selectedStones.put(color,
|
|
||||||
prepaintColor(selectedFg, selectedBackground));
|
|
||||||
hoveredStones.put(color,
|
|
||||||
prepaintColor(hoveredFg, hoveredBackground));
|
|
||||||
hoveredSelectedStones
|
|
||||||
.put(color,
|
|
||||||
prepaintColor(hoveredSelectedFg,
|
|
||||||
hoveredSelectedBackground));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,8 +221,7 @@ class StonePainter {
|
||||||
setScale(scale);
|
setScale(scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void paintStoneBackground(Graphics2D g, Rectangle r,
|
private void paintStoneBackground(Graphics2D g, Rectangle r, Color background) {
|
||||||
Color background) {
|
|
||||||
// Paint background
|
// Paint background
|
||||||
g.setColor(background);
|
g.setColor(background);
|
||||||
g.fillRect(r.x, r.y, r.width, r.height);
|
g.fillRect(r.x, r.y, r.width, r.height);
|
||||||
|
@ -310,9 +321,8 @@ class StonePainter {
|
||||||
pos + (fm.getAscent() - fm.getDescent()) / 2 + 1);
|
pos + (fm.getAscent() - fm.getDescent()) / 2 + 1);
|
||||||
}
|
}
|
||||||
g.setColor(color);
|
g.setColor(color);
|
||||||
g.drawString(value,
|
g.drawString(value, (int) (r.x + r.width / 2 - stringRect.getWidth() / 2),
|
||||||
(int) (r.x + r.width / 2 - stringRect.getWidth() / 2), pos
|
pos + (fm.getAscent() - fm.getDescent()) / 2);
|
||||||
+ (fm.getAscent() - fm.getDescent()) / 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void paintCircle(Graphics2D g, Rectangle r, Color background) {
|
private void paintCircle(Graphics2D g, Rectangle r, Color background) {
|
||||||
|
@ -321,12 +331,11 @@ class StonePainter {
|
||||||
|
|
||||||
// Paint circle
|
// Paint circle
|
||||||
g.setColor(background.darker());
|
g.setColor(background.darker());
|
||||||
g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50,
|
g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, 170);
|
||||||
170);
|
|
||||||
|
|
||||||
g.setColor(brighter(background));
|
g.setColor(brighter(background));
|
||||||
g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size,
|
g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, size,
|
||||||
size, -130, 170);
|
-130, 170);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -349,27 +358,11 @@ class StonePainter {
|
||||||
int height = getStoneHeight();
|
int height = getStoneHeight();
|
||||||
int x = Math.round(p.getX() * width), y = Math.round(p.getY() * height);
|
int x = Math.round(p.getX() * width), y = Math.round(p.getY() * height);
|
||||||
|
|
||||||
Map<StoneColor, Map<Integer, BufferedImage>> stoneMap;
|
|
||||||
|
|
||||||
if (selected) {
|
|
||||||
if (hovered) {
|
|
||||||
stoneMap = hoveredSelectedStones;
|
|
||||||
} else {
|
|
||||||
stoneMap = selectedStones;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (hovered) {
|
|
||||||
stoneMap = hoveredStones;
|
|
||||||
} else {
|
|
||||||
stoneMap = defaultStones;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stone.isJoker()) {
|
if (stone.isJoker()) {
|
||||||
g.drawImage(stoneMap.get(stone.getColor()).get(0), x, y, null);
|
g.drawImage(getStoneImage(stone.getColor(), 0, selected, hovered), x, y, null);
|
||||||
} else {
|
} else {
|
||||||
g.drawImage(stoneMap.get(stone.getColor()).get(stone.getValue()),
|
g.drawImage(getStoneImage(stone.getColor(), stone.getValue(), selected, hovered), x, y,
|
||||||
x, y, null);
|
null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue