Skip to content

Commit

Permalink
Make repainting more reliable and higher quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Sothatsit committed Jan 1, 2023
1 parent 3207d9e commit 13328e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
19 changes: 10 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,11 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- Used for interaction with the browser for WASM target. -->
<dependency>
<groupId>de.mirkosertic.bytecoder</groupId>
Expand All @@ -33,6 +26,14 @@
<artifactId>bytecoder.web</artifactId>
<version>2021-01-26</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public AnalysisWindow() {
frame = new JFrame(RoyalUrAnalysis.TITLE);
frame.setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new BufferedCanvas(canvasWidth, canvasHeight);
canvas = new BufferedCanvas(canvasWidth, canvasHeight, this::repaint);
canvas.addMouseListener(this);
frame.setContentPane(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

repaint();
canvas.repaint();
}

public int[] convertBoardToScreen(int x, int y) {
Expand All @@ -117,10 +117,7 @@ public int[] convertScreenToBoard(int x, int y) {
};
}

public void repaint() {
Graphics2D g = canvas.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

public void repaint(Graphics2D g) {
g.clearRect(0, 0, canvasWidth, canvasHeight);
drawBoard(g);

Expand All @@ -143,6 +140,7 @@ public void repaint() {
drawTextCentered(g, titleFont, canvasWidth / 2, 35, turn);
drawTextCentered(g, titleFont, canvasWidth / 2, canvasHeight - 35, "Analyse");

g.dispose();
canvas.repaint();
frame.repaint();
}
Expand Down Expand Up @@ -174,7 +172,7 @@ private void drawTextCentered(Graphics2D g, Font font, int x, int y, String text
}

public void drawBoard(Graphics2D g) {
// Draw all of the spaces on the board.
// Draw all spaces on the board.
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 8; ++y) {
if (!Tile.isOnBoard(x, y))
Expand Down Expand Up @@ -323,7 +321,7 @@ public void mousePressed(MouseEvent e) {
} else if (analyseRect.contains(mouse)) {
analyse();
}
repaint();
canvas.repaint();
}

@Override
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/com/sothatsit/royalur/analysis/ui/BufferedCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.function.Consumer;

/**
* A canvas that buffers the modifications you make to it
Expand All @@ -12,23 +13,25 @@
*/
public class BufferedCanvas extends JPanel {

private final BufferedImage image;
private final Graphics2D graphics;
private final Consumer<Graphics2D> repaintFn;

public BufferedCanvas(int width, int height) {
public BufferedCanvas(int width, int height, Consumer<Graphics2D> repaintFn) {
setPreferredSize(new Dimension(width, height));
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
this.graphics = image.createGraphics();
graphics.setBackground(Color.WHITE);
}

public Graphics2D getGraphics() {
return graphics;
this.repaintFn = repaintFn;
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);

Graphics2D graphics = (Graphics2D) g.create();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
try {
repaintFn.accept(graphics);
} finally {
graphics.dispose();
}
}
}

0 comments on commit 13328e3

Please sign in to comment.