-
Notifications
You must be signed in to change notification settings - Fork 0
Tianpok/image arena background #46
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85dc555
perf: cache arena background as image
liang799 308ee10
docs: update class diagram for background renderer
liang799 237f064
docs: regenerate high resolution diagram images
liang799 21f4669
chore: merge main and resolve diagram conflicts
liang799 8f96ddf
test: verify arena background decodes
liang799 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/sc2002/turnbased/ui/gui/view/ArenaBackgroundRenderer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package sc2002.turnbased.ui.gui.view; | ||
|
|
||
| import java.awt.Graphics2D; | ||
| import java.awt.RenderingHints; | ||
| import java.awt.image.BufferedImage; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.Objects; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
|
|
||
| final class ArenaBackgroundRenderer { | ||
| static final String BACKGROUND_RESOURCE = "/sc2002/turnbased/ui/gui/assets/arena-background.png"; | ||
|
|
||
| private final BufferedImage source; | ||
| private BufferedImage scaledBackground; | ||
| private int scaledWidth; | ||
| private int scaledHeight; | ||
|
|
||
| ArenaBackgroundRenderer() { | ||
| this(loadResource(BACKGROUND_RESOURCE)); | ||
| } | ||
|
|
||
| ArenaBackgroundRenderer(BufferedImage source) { | ||
| this.source = Objects.requireNonNull(source, "source"); | ||
| if (source.getWidth() <= 0 || source.getHeight() <= 0) { | ||
| throw new IllegalArgumentException("Background image must have positive dimensions."); | ||
| } | ||
| } | ||
|
|
||
| void render(Graphics2D g, int width, int height) { | ||
| Objects.requireNonNull(g, "g"); | ||
| if (width <= 0 || height <= 0) { | ||
| return; | ||
| } | ||
| g.drawImage(scaledBackground(width, height), 0, 0, null); | ||
| } | ||
|
|
||
| private BufferedImage scaledBackground(int width, int height) { | ||
| if (scaledBackground == null || width != scaledWidth || height != scaledHeight) { | ||
| scaledBackground = createScaledBackground(width, height); | ||
| scaledWidth = width; | ||
| scaledHeight = height; | ||
| } | ||
| return scaledBackground; | ||
| } | ||
|
|
||
| private BufferedImage createScaledBackground(int width, int height) { | ||
| BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | ||
| Graphics2D backgroundGraphics = target.createGraphics(); | ||
| try { | ||
| backgroundGraphics.setRenderingHint( | ||
| RenderingHints.KEY_INTERPOLATION, | ||
| RenderingHints.VALUE_INTERPOLATION_BILINEAR | ||
| ); | ||
| backgroundGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | ||
| SourceCrop crop = coverCrop(width, height); | ||
| backgroundGraphics.drawImage( | ||
| source, | ||
| 0, | ||
| 0, | ||
| width, | ||
| height, | ||
| crop.x(), | ||
| crop.y(), | ||
| crop.x() + crop.width(), | ||
| crop.y() + crop.height(), | ||
| null | ||
| ); | ||
| } finally { | ||
| backgroundGraphics.dispose(); | ||
| } | ||
| return target; | ||
| } | ||
|
|
||
| private SourceCrop coverCrop(int width, int height) { | ||
| int sourceWidth = source.getWidth(); | ||
| int sourceHeight = source.getHeight(); | ||
| double sourceRatio = sourceWidth / (double) sourceHeight; | ||
| double targetRatio = width / (double) height; | ||
| if (sourceRatio > targetRatio) { | ||
| int cropWidth = Math.max(1, (int) Math.round(sourceHeight * targetRatio)); | ||
| int cropX = (sourceWidth - cropWidth) / 2; | ||
| return new SourceCrop(cropX, 0, cropWidth, sourceHeight); | ||
| } | ||
| int cropHeight = Math.max(1, (int) Math.round(sourceWidth / targetRatio)); | ||
| int cropY = (sourceHeight - cropHeight) / 2; | ||
| return new SourceCrop(0, cropY, sourceWidth, cropHeight); | ||
| } | ||
|
|
||
| private static BufferedImage loadResource(String resourceName) { | ||
| try (InputStream inputStream = ArenaBackgroundRenderer.class.getResourceAsStream(resourceName)) { | ||
| if (inputStream == null) { | ||
| throw new IllegalStateException("Missing arena background resource: " + resourceName); | ||
| } | ||
| BufferedImage image = ImageIO.read(inputStream); | ||
| if (image == null) { | ||
| throw new IllegalStateException("Unreadable arena background resource: " + resourceName); | ||
| } | ||
| return image; | ||
| } catch (IOException exception) { | ||
| throw new UncheckedIOException("Unable to load arena background resource: " + resourceName, exception); | ||
| } | ||
| } | ||
|
|
||
| private record SourceCrop(int x, int y, int width, int height) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+170 KB
src/main/resources/sc2002/turnbased/ui/gui/assets/arena-background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions
50
src/test/java/sc2002/turnbased/ui/gui/view/ArenaBackgroundRendererTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package sc2002.turnbased.ui.gui.view; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.awt.image.BufferedImage; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| import javax.imageio.ImageIO; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ArenaBackgroundRendererTest { | ||
| private static final byte[] PNG_SIGNATURE = new byte[] { | ||
| (byte) 0x89, | ||
| 'P', | ||
| 'N', | ||
| 'G', | ||
| '\r', | ||
| '\n', | ||
| 0x1A, | ||
| '\n' | ||
| }; | ||
|
|
||
| @Test | ||
| void packagesBackgroundAsPngResource() throws IOException { | ||
| try (InputStream inputStream = ArenaBackgroundRenderer.class.getResourceAsStream( | ||
| ArenaBackgroundRenderer.BACKGROUND_RESOURCE | ||
| )) { | ||
| assertNotNull(inputStream); | ||
| assertArrayEquals(PNG_SIGNATURE, inputStream.readNBytes(PNG_SIGNATURE.length)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void decodesPackagedBackgroundResource() throws IOException { | ||
| try (InputStream inputStream = ArenaBackgroundRenderer.class.getResourceAsStream( | ||
| ArenaBackgroundRenderer.BACKGROUND_RESOURCE | ||
| )) { | ||
| assertNotNull(inputStream); | ||
| BufferedImage image = ImageIO.read(inputStream); | ||
|
|
||
| assertNotNull(image); | ||
| assertTrue(image.getWidth() > 0); | ||
| assertTrue(image.getHeight() > 0); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.