-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next: Support Seamless Texture & Enhance Stone Quality #366
Changes from 4 commits
c81e286
336ede0
4ba9c13
9c68cf8
1d9a2d5
c8f10f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import java.awt.font.TextAttribute; | ||
import java.awt.geom.Point2D; | ||
import java.awt.image.BufferedImage; | ||
import java.awt.image.ImageObserver; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -45,6 +46,9 @@ public class BoardRenderer { | |
private BufferedImage cachedStonesShadowImage = null; | ||
private Zobrist cachedZhash = new Zobrist(); // defaults to an empty board | ||
|
||
private BufferedImage cachedBlackStoneImage = null; | ||
private BufferedImage cachedWhiteStoneImage = null; | ||
|
||
private BufferedImage branchStonesImage = null; | ||
private BufferedImage branchStonesShadowImage = null; | ||
|
||
|
@@ -639,8 +643,10 @@ private void drawWoodenBoard(Graphics2D g) { | |
if (uiConfig.getBoolean("fancy-board")) { | ||
// fancy version | ||
int shadowRadius = (int) (boardLength * MARGIN / 6); | ||
Image boardImage = theme.getBoard(); | ||
g.drawImage(boardImage == null ? theme.getBoard() : boardImage, x - 2 * shadowRadius, y - 2 * shadowRadius, boardLength + 4 * shadowRadius, boardLength + 4 * shadowRadius, null); | ||
BufferedImage boardImage = theme.getBoard(); | ||
// Support seamless texture | ||
drawTextureImage(g, boardImage == null ? theme.getBoard() : boardImage, x - 2 * shadowRadius, y - 2 * shadowRadius, boardLength + 4 * shadowRadius, boardLength + 4 * shadowRadius); | ||
|
||
g.setStroke(new BasicStroke(shadowRadius * 2)); | ||
// draw border | ||
g.setColor(new Color(0, 0, 0, 50)); | ||
|
@@ -752,7 +758,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center | |
if (uiConfig.getBoolean("fancy-stones")) { | ||
drawShadow(gShadow, centerX, centerY, false); | ||
Image stone = theme.getBlackStone(new int[]{x, y}); | ||
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null); | ||
// Enhance draw quality | ||
int size = stoneRadius * 2 + 1; | ||
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null); | ||
} else { | ||
drawShadow(gShadow, centerX, centerY, true); | ||
g.setColor(Color.BLACK); | ||
|
@@ -765,7 +773,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center | |
if (uiConfig.getBoolean("fancy-stones")) { | ||
drawShadow(gShadow, centerX, centerY, false); | ||
Image stone = theme.getWhiteStone(new int[]{x, y}); | ||
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null); | ||
// Enhance draw quality | ||
int size = stoneRadius * 2 + 1; | ||
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null); | ||
} else { | ||
drawShadow(gShadow, centerX, centerY, true); | ||
g.setColor(Color.WHITE); | ||
|
@@ -779,7 +789,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center | |
if (uiConfig.getBoolean("fancy-stones")) { | ||
drawShadow(gShadow, centerX, centerY, true); | ||
Image stone = theme.getBlackStone(new int[]{x, y}); | ||
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null); | ||
// Enhance draw quality | ||
int size = stoneRadius * 2 + 1; | ||
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null); | ||
} else { | ||
drawShadow(gShadow, centerX, centerY, true); | ||
g.setColor(new Color(0, 0, 0));//, uiConfig.getInt("branch-stone-alpha"))); | ||
|
@@ -791,7 +803,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center | |
if (uiConfig.getBoolean("fancy-stones")) { | ||
drawShadow(gShadow, centerX, centerY, true); | ||
Image stone = theme.getWhiteStone(new int[]{x, y}); | ||
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null); | ||
// Enhance draw quality | ||
int size = stoneRadius * 2 + 1; | ||
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null); | ||
} else { | ||
drawShadow(gShadow, centerX, centerY, true); | ||
g.setColor(new Color(255, 255, 255));//, uiConfig.getInt("branch-stone-alpha"))); | ||
|
@@ -805,6 +819,94 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center | |
} | ||
} | ||
|
||
/** | ||
* Get scaled stone, if cached then return cached | ||
*/ | ||
public BufferedImage getScaleStone(Image img, Stone color, int width, int height) { | ||
BufferedImage stone = null; | ||
switch (color) { | ||
case BLACK: | ||
case BLACK_CAPTURED: | ||
case BLACK_GHOST: | ||
if (cachedBlackStoneImage == null || cachedBlackStoneImage.getWidth() != width || cachedBlackStoneImage.getHeight() != height) { | ||
stone = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This entire block is copy pasted, could you please factor it out? |
||
Graphics2D g2 = stone.createGraphics(); | ||
g2.drawImage(img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), 0, 0, null); | ||
g2.dispose(); | ||
cachedBlackStoneImage = stone; | ||
} else { | ||
return cachedBlackStoneImage; | ||
} | ||
break; | ||
case WHITE: | ||
case WHITE_CAPTURED: | ||
case WHITE_GHOST: | ||
if (cachedWhiteStoneImage == null || cachedWhiteStoneImage.getWidth() != width || cachedWhiteStoneImage.getHeight() != height) { | ||
stone = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | ||
Graphics2D g2 = stone.createGraphics(); | ||
g2.drawImage(img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), 0, 0, null); | ||
g2.dispose(); | ||
cachedWhiteStoneImage = stone; | ||
} else { | ||
return cachedWhiteStoneImage; | ||
} | ||
break; | ||
default: | ||
} | ||
return stone; | ||
} | ||
|
||
/** | ||
* Draw scale smooth image, enhanced display quality | ||
*/ | ||
public void drawScaleSmoothImage(Graphics2D g, BufferedImage img, int x, int y, int width, int height, ImageObserver observer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dead code? |
||
BufferedImage newstone = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | ||
Graphics2D g2 = newstone.createGraphics(); | ||
g2.drawImage(img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), 0, 0, observer); | ||
g2.dispose(); | ||
g.drawImage(newstone, x, y, width, height, observer); | ||
} | ||
|
||
/** | ||
* Draw scale smooth image, enhanced display quality, less and faster than drawScaleSmoothImage | ||
*/ | ||
public void drawScaleImage(Graphics2D g, BufferedImage img, int x, int y, int width, int height, ImageObserver observer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also dead code?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the both functions are dead code, but the drawScaleSmoothImage can provide high quality scale draw, the drawScaleImage can provide better quality than the default and faster than the drawScaleSmoothImage, so I think it might be useful to keep it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you think it might be useful to commit these functions even if they are unused, then please put the code inside a comment with an explanation of what it does differently that the one that's actually used... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have described & disabled the both functions. |
||
BufferedImage newstone = (BufferedImage)img; | ||
int w = img.getWidth(); | ||
int h = img.getHeight(); | ||
do { | ||
if (w > width) { | ||
w /= 2; | ||
if (w < width) { | ||
w = width; | ||
} | ||
} | ||
if (h > height) { | ||
h /= 2; | ||
if (h < height) { | ||
h = height; | ||
} | ||
} | ||
BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); | ||
Graphics2D g2 = tmp.createGraphics(); | ||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); | ||
g2.drawImage(newstone, 0, 0, w, h, null); | ||
g2.dispose(); | ||
newstone = tmp; | ||
} | ||
while (w != width || h != height); | ||
g.drawImage(newstone, x, y, width, height, observer); | ||
} | ||
|
||
/** | ||
* Draw texture image | ||
*/ | ||
public void drawTextureImage(Graphics2D g, BufferedImage img, int x, int y, int width, int height) { | ||
TexturePaint paint = new TexturePaint(img, new Rectangle(0, 0, img.getWidth(), img.getHeight())); | ||
g.setPaint(paint); | ||
g.fill(new Rectangle(x, y, width, height)); | ||
} | ||
|
||
/** | ||
* Fills in a circle centered at (centerX, centerY) with radius $radius$ | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there is also a lot of copy pasting here... For fancy-stones, I think the entire switch statement can be replaced by 6 lines:
By the way, do you know why do we keep two code paths here? Could we keep only
fancy-stones
and remove the other one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, you're right.
Because there are 'fancy-stone' settings in the configuration file, some people may like simple styles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have simplified code.