Last active
April 17, 2025 21:51
-
-
Save Crydust/d0736f398f3cbed2c324718c3c4ecefa to your computer and use it in GitHub Desktop.
Initialize Graphics2D with highest visual quality and draw jts Geometry objects
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
private static void show(Geometry... geometries) throws InterruptedException, InvocationTargetException { | |
ShapeWriter shapeWriter = new ShapeWriter(createPointTransformation(5, 20)); | |
int width = 640, height = 480; | |
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D g2d = createGraphics2D(image); | |
try { | |
// Draw background | |
g2d.setColor(Color.WHITE); | |
g2d.fillRect(0, 0, width, height); | |
// Draw geometries | |
g2d.setStroke(new BasicStroke(1)); | |
for (Geometry geometry : geometries) { | |
g2d.setColor(Color.BLUE); | |
g2d.draw(shapeWriter.toShape(geometry)); | |
} | |
} finally { | |
g2d.dispose(); | |
} | |
SwingUtilities.invokeAndWait(() -> { | |
JDialog dialog = new JDialog(); | |
dialog.setTitle("Geometry"); | |
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); | |
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);K | |
JComponent imageComponent = new JComponent() { | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
g.drawImage(image, 0, 0, this); | |
} | |
@Override | |
public Dimension getPreferredSize() { | |
return new Dimension(image.getWidth(), image.getHeight()); | |
} | |
}; | |
dialog.add(imageComponent); | |
dialog.pack(); | |
dialog.setLocationRelativeTo(null); | |
dialog.setVisible(true); | |
}); | |
} | |
private static Graphics2D createGraphics2D(BufferedImage image) { | |
Graphics2D g2d = image.createGraphics(); | |
g2d.addRenderingHints(Map.of( | |
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON, | |
RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY, | |
RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY, | |
RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY, | |
RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC, | |
RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE, | |
RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE, | |
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON, | |
RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON | |
)); | |
return g2d; | |
} | |
private static PointTransformation createPointTransformation(int translate, int scale) { | |
return (src, dest) -> { | |
dest.setLocation( | |
(src.x + translate) * scale, | |
(src.y + translate) * scale | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment