Skip to content

Commit a916aeb

Browse files
committed
lib/graphics: improve API
- Add more documentation - Rename FilmStripCanvas to GuiFilmStripCanvas to match naming of other classes - Move Graphics#renderableColor to Colors#toAwtColor
1 parent bc590f3 commit a916aeb

File tree

10 files changed

+57
-21
lines changed

10 files changed

+57
-21
lines changed

lib/src/main/java/jtamaro/graphic/CircularSector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Color getColor() {
5353

5454
@Override
5555
protected void render(Graphics2D g2d, RenderOptions options) {
56-
g2d.setPaint(renderableColor(color));
56+
g2d.setPaint(Colors.toAwtColor(color));
5757
g2d.fill(getPath());
5858
}
5959

lib/src/main/java/jtamaro/graphic/Colors.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ static String htmlString(Color color) {
178178
: "#ffffff";
179179
return String.format(
180180
"<font bgcolor=\"rgba(%1$d,%2$d,%3$d,%4$.2f)\" color=\"%5$s\">&nbsp;#%1$02x%2$02x%3$02x&nbsp;</font>",
181-
color.red(), color.green(), color.blue(), color.opacity(), foregroundColor);
181+
color.red(),
182+
color.green(),
183+
color.blue(),
184+
color.opacity(),
185+
foregroundColor);
186+
}
187+
188+
/**
189+
* Convert a given jtamaro color to an equivalent awt {@link java.awt.Color}.
190+
*/
191+
public static java.awt.Color toAwtColor(Color color) {
192+
return new java.awt.Color(color.red(), color.green(), color.blue(), color.alpha());
182193
}
183194
}

lib/src/main/java/jtamaro/graphic/Ellipse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Color getColor() {
5050

5151
@Override
5252
protected void render(Graphics2D g2d, RenderOptions options) {
53-
g2d.setPaint(renderableColor(color));
53+
g2d.setPaint(Colors.toAwtColor(color));
5454
g2d.fill(getPath());
5555
}
5656

lib/src/main/java/jtamaro/graphic/Graphic.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package jtamaro.graphic;
22

3-
import java.awt.Color;
43
import java.awt.Graphics2D;
54
import java.awt.geom.Path2D;
65
import java.awt.geom.Rectangle2D;
@@ -67,10 +66,18 @@ protected Graphic(Path2D.Double path, Point pinPoint) {
6766

6867
/* **** Location **** */
6968

69+
/**
70+
* Get pinning point of the graphic.
71+
*
72+
* @see Point
73+
*/
7074
public Point getPin() {
7175
return pinPoint;
7276
}
7377

78+
/**
79+
* Get the (tight) bounding box of this graphic.
80+
*/
7481
Rectangle2D getBBox() {
7582
return bbox;
7683
}
@@ -95,6 +102,11 @@ public final double getBoundsMinY() {
95102
return getBBox().getMinY();
96103
}
97104

105+
/**
106+
* Get the relative location for the given point with respect to this graphic.
107+
*
108+
* @implNote The location is computed with respect to the origin point of this graphic.
109+
*/
98110
RelativeLocation getLocation(Point point) {
99111
/*
100112
* switch (point.x) {
@@ -136,7 +148,7 @@ RelativeLocation getLocation(Point point) {
136148
* Return the location coordinates relative to the (child) graphic corresponding to the given
137149
* absolute coordinates.
138150
*
139-
* <p>The two coordinates must be given with respect to the origin point of the graphic.
151+
* @apiNote The two coordinates must be given with respect to the origin point of the graphic.
140152
*/
141153
Option<RelativeLocation> relativeLocationOf(double x, double y) {
142154
return path.contains(x, y)
@@ -147,33 +159,46 @@ Option<RelativeLocation> relativeLocationOf(double x, double y) {
147159
/* **** Rendering **** */
148160

149161
/**
150-
* Render this Graphic into the given Graphics2D graphics context, using the given RenderOptions.
162+
* Render this Graphic into the given {@link Graphics2D} graphics context, using the given
163+
* {@link RenderOptions}.
151164
*
152165
* <p>The context can represent a GUI component (used when visualizing the graphic), or a bitmap
153166
* (used when writing the graphic into a bitmap file).
154167
*/
155168
protected abstract void render(Graphics2D g2d, RenderOptions options);
156169

170+
/**
171+
* Draw debug information for this graphic on the given {@link Graphics2D} graphics context.
172+
*/
157173
final void drawDebugInfo(Graphics2D g2d) {
158174
GraphicsDebugInfo.render(g2d, getPath(), getBBox());
159175
}
160176

177+
/**
178+
* Get the path of this graphic.
179+
*/
161180
Path2D.Double getPath() {
162181
return path;
163182
}
164183

184+
/**
185+
* Get the width of this graphic.
186+
*
187+
* @implNote Computed using the tight bounding box.
188+
*/
165189
public double getWidth() {
166190
return bbox.getWidth();
167191
}
168192

193+
/**
194+
* Get the height of this graphic.
195+
*
196+
* @implNote Computed using the tight bounding box.
197+
*/
169198
public double getHeight() {
170199
return bbox.getHeight();
171200
}
172201

173-
public static Color renderableColor(jtamaro.graphic.Color color) {
174-
return new Color(color.red(), color.green(), color.blue(), color.alpha());
175-
}
176-
177202
/**
178203
* Return an <b>ordered</b> map that contains children (and their "names" as key) of this
179204
* graphic.

lib/src/main/java/jtamaro/graphic/FilmStripCanvas.java renamed to lib/src/main/java/jtamaro/graphic/GuiFilmStripCanvas.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @apiNote Not for public use.
2020
* @hidden
2121
*/
22-
public final class FilmStripCanvas extends JComponent {
22+
public final class GuiFilmStripCanvas extends JComponent {
2323

2424
private static final double GAP_FRACTION = 0.05;
2525

@@ -55,7 +55,7 @@ public final class FilmStripCanvas extends JComponent {
5555

5656
private int position;
5757

58-
public FilmStripCanvas(Sequence<Graphic> graphics, int frameWidth, int frameHeight) {
58+
public GuiFilmStripCanvas(Sequence<Graphic> graphics, int frameWidth, int frameHeight) {
5959
super();
6060
this.indexedGraphics = graphics.zipWithIndex();
6161
this.frameWidth = frameWidth;

lib/src/main/java/jtamaro/graphic/Rectangle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public double getHeight() {
4646

4747
@Override
4848
protected void render(Graphics2D g2d, RenderOptions options) {
49-
g2d.setPaint(renderableColor(color));
49+
g2d.setPaint(Colors.toAwtColor(color));
5050
g2d.fill(getPath());
5151
}
5252

lib/src/main/java/jtamaro/graphic/Text.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Color getColor() {
6464

6565
@Override
6666
protected void render(Graphics2D g2d, RenderOptions options) {
67-
g2d.setPaint(renderableColor(color));
67+
g2d.setPaint(Colors.toAwtColor(color));
6868
g2d.fill(getPath());
6969
}
7070

lib/src/main/java/jtamaro/graphic/Triangle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Color getColor() {
6060

6161
@Override
6262
protected void render(Graphics2D g2d, RenderOptions options) {
63-
g2d.setPaint(renderableColor(color));
63+
g2d.setPaint(Colors.toAwtColor(color));
6464
g2d.fill(getPath());
6565
}
6666

lib/src/main/java/jtamaro/io/ColorFrame.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import javax.swing.JFrame;
88
import javax.swing.JLabel;
99
import jtamaro.graphic.Color;
10-
import jtamaro.graphic.Graphic;
10+
import jtamaro.graphic.Colors;
1111

1212
final class ColorFrame extends JFrame {
1313

@@ -30,7 +30,7 @@ public Dimension getPreferredSize() {
3030

3131
@Override
3232
protected void paintComponent(Graphics g) {
33-
g.setColor(Graphic.renderableColor(color));
33+
g.setColor(Colors.toAwtColor(color));
3434
g.fillRect(0, 0, getWidth(), getHeight());
3535
}
3636
};

lib/src/main/java/jtamaro/io/FilmStripFrame.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import javax.swing.JPanel;
1010
import javax.swing.JScrollBar;
1111
import jtamaro.data.Sequence;
12-
import jtamaro.graphic.FilmStripCanvas;
12+
import jtamaro.graphic.GuiFilmStripCanvas;
1313
import jtamaro.graphic.Graphic;
1414

1515
final class FilmStripFrame extends JFrame {
@@ -20,16 +20,16 @@ final class FilmStripFrame extends JFrame {
2020

2121
private final int completeFrameWidth;
2222

23-
private final FilmStripCanvas canvas;
23+
private final GuiFilmStripCanvas canvas;
2424

2525
public FilmStripFrame(Sequence<Graphic> graphics, int frameWidth, int frameHeight) {
2626
super();
2727
setTitle("Film Strip (" + length(graphics) + " frames)");
2828
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
2929
setLayout(new BorderLayout());
3030

31-
completeFrameWidth = FilmStripCanvas.computeCompleteFrameWidth(frameWidth);
32-
canvas = new FilmStripCanvas(graphics, frameWidth, frameHeight);
31+
completeFrameWidth = GuiFilmStripCanvas.computeCompleteFrameWidth(frameWidth);
32+
canvas = new GuiFilmStripCanvas(graphics, frameWidth, frameHeight);
3333
add(canvas, BorderLayout.CENTER);
3434
final JPanel bar = new JPanel(new BorderLayout());
3535
final JButton prevButton = new JButton("<");

0 commit comments

Comments
 (0)