-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathExample.java
More file actions
67 lines (52 loc) · 1.93 KB
/
Copy pathExample.java
File metadata and controls
67 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.github.humbleui.jwm.examples;
import io.github.humbleui.jwm.*;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.function.Consumer;
/// An example of JWM working with Java2D/Graphics2D as graphics-drawing library.
///
/// This example shows some text and an orange circle at the center of the main window.
///
public class Example implements Consumer<Event> {
private Window window;
public Example(int width, int height) {
window = App.makeWindow();
window.setEventListener(this);
window.setTitle("JWM with Java2D/Graphics2D Fixed Scale");
window.setLayer(new LayerRasterGraphics2D());
window.setWindowSize(width, height);
window.setVisible(true);
}
@Override
public void accept(Event e) {
if (e instanceof EventWindowCloseRequest) {
window.close();
App.terminate();
}
else if (e instanceof EventFrameGraphics2D eg) {
var image = eg.getBufferedImage();
var g2d = image.createGraphics();
drawJava2DScene(g2d, image.getWidth(), image.getHeight());
g2d.dispose();
}
}
private void drawJava2DScene(Graphics2D g2d, int w, int h) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Clear background
g2d.setColor(new Color(30, 30, 30));
g2d.fillRect(0, 0, w, h);
// Draw an orange circle
int circleSize = Math.min(w, h) / 2;
int x = (w - circleSize) / 2;
int y = (h - circleSize) / 2;
g2d.setColor(Color.ORANGE);
g2d.fillOval(x, y, circleSize, circleSize);
// Draw some text
g2d.setColor(Color.WHITE);
g2d.drawString("Hello from High-DPI Correlated Java2D!", 50, 50);
}
public static void main(String[] args) {
App.start(() -> new Example(800, 600));
}
}