Skip to content

Commit 2fce6e6

Browse files
committed
readability, promo, clean-up
1 parent c7a7fc4 commit 2fce6e6

File tree

3 files changed

+65
-42
lines changed

3 files changed

+65
-42
lines changed

src/main/java/ee/ant/dotmatrix/Controller.java

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import javafx.event.ActionEvent;
55
import javafx.fxml.FXML;
66
import javafx.scene.Node;
7-
import javafx.scene.control.Button;
8-
import javafx.scene.control.RadioButton;
9-
import javafx.scene.control.TextField;
7+
import javafx.scene.control.*;
8+
import javafx.scene.image.Image;
9+
import javafx.scene.image.ImageView;
1010
import javafx.scene.layout.*;
1111
import javafx.scene.paint.Color;
1212

@@ -39,9 +39,13 @@ public class Controller {
3939
private TextField hexString;
4040
@FXML
4141
private Button drawButton;
42+
@FXML
43+
private MenuItem aboutDialog;
4244

4345
@FXML
4446
private void initialize() {
47+
// show AboutDialog
48+
aboutDialog.setOnAction(event -> showAbout());
4549

4650
// Handle 5x8 radio button event
4751
rbutton58.setOnAction(event -> createEmptyMatrix(8, 5));
@@ -74,6 +78,21 @@ private void initialize() {
7478
createEmptyMatrix(8, 5);
7579
}
7680

81+
/**
82+
* Show promotion
83+
*/
84+
private void showAbout() {
85+
String uri = "www.ant.ee";
86+
Alert alert = new Alert(Alert.AlertType.INFORMATION);
87+
alert.setTitle("About author");
88+
alert.setHeaderText("");
89+
alert.setGraphic(new ImageView(new Image("ant.png")));
90+
Hyperlink link = new Hyperlink(uri);
91+
link.setOnAction(event -> Main.openURL(uri + "/?dotmatrix"));
92+
alert.getDialogPane().contentProperty().set(link);
93+
alert.showAndWait();
94+
}
95+
7796
/**
7897
* Draw empty matrix and register draw events
7998
*
@@ -82,6 +101,7 @@ private void initialize() {
82101
*/
83102
private void createEmptyMatrix(int rows, int columns) {
84103
dotMatrix.getChildren().clear();
104+
hexString.clear();
85105
for (int i = 0; i < columns; i++) {
86106
for (int j = 0; j < rows; j++) {
87107
Button btn = new Button();
@@ -146,23 +166,45 @@ private void parseHexString(String text) {
146166
}
147167

148168
/**
149-
* Draw symbol from bytes array
169+
* Draw symbol from bytes array. It can be either single segment or 4 segments if number of bytes is
170+
* grater than 8
150171
*
151172
* @param bytes byte array that encodes symbol
152173
*/
153174
private void fillMatrixWithDots(byte[] bytes) {
175+
int colums = bytes.length;
154176
if (bytes.length > 8) {
155177
// there are 4 segments
156-
createEmptyMatrix(16, bytes.length / 2);
178+
colums = bytes.length / 2;
179+
createEmptyMatrix(16, colums);
157180
drawSegment(0, 0, Arrays.copyOfRange(bytes, 0, bytes.length / 4));
158181
drawSegment(0, bytes.length / 4, Arrays.copyOfRange(bytes, bytes.length / 4, bytes.length / 2));
159182
drawSegment(8, 0, Arrays.copyOfRange(bytes, bytes.length / 2, 3 * bytes.length / 4));
160183
drawSegment(8, bytes.length / 4, Arrays.copyOfRange(bytes, 3 * bytes.length / 4, bytes.length));
161184
} else {
162185
// there is only one segment
163-
createEmptyMatrix(8, bytes.length);
186+
createEmptyMatrix(8, colums);
164187
drawSegment(bytes);
165188
}
189+
190+
// select radiobutton of corresponding size
191+
switch (colums) {
192+
case 5: rbutton58.setSelected(true);
193+
break;
194+
case 6: rbutton68.setSelected(true);
195+
break;
196+
case 7: rbutton78.setSelected(true);
197+
break;
198+
case 8: rbutton88.setSelected(true);
199+
break;
200+
case 10: rbutton1016.setSelected(true);
201+
break;
202+
case 12: rbutton1216.setSelected(true);
203+
break;
204+
case 14: rbutton1416.setSelected(true);
205+
break;
206+
case 16: rbutton1616.setSelected(true);
207+
}
166208
}
167209

168210
/**
@@ -183,7 +225,8 @@ private void drawSegment(byte[] bytes) {
183225
}
184226

185227
/**
186-
* Draw segment based on offset. Top left has 0,0 and bottom right 8, number of columns
228+
* Draw segment based on offset. Top left has 0,0 and bottom right 8, number of columns. Used
229+
* for multi-segment character
187230
*
188231
* @param offsetCol - column offset
189232
* @param offsetRaw - raw offset
@@ -223,7 +266,9 @@ private void generateHexString() {
223266
}
224267

225268
/**
226-
* Create hex string for selected segment
269+
* Create hex string for segment based on offset. Used for multi-segment
270+
* hex string calculation
271+
*
227272
* @param offsetCol column offset
228273
* @param offsetRaw raw offset
229274
* @return hex string for the segment
@@ -241,7 +286,7 @@ private char[] readSegment(int offsetCol, int offsetRaw) {
241286
}
242287

243288
/**
244-
* Return hex string for single segment 8*columns
289+
* Calculates hex codes and return string for single segment 8*columns
245290
* @param segment buttons node list in a segment
246291
* @return hex string
247292
*/

src/main/java/ee/ant/dotmatrix/Main.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package ee.ant.dotmatrix;
22

33
import javafx.application.Application;
4+
import javafx.application.HostServices;
45
import javafx.fxml.FXMLLoader;
56
import javafx.scene.Parent;
67
import javafx.scene.Scene;
78
import javafx.scene.image.Image;
89
import javafx.stage.Stage;
910

11+
import java.util.Objects;
12+
1013
public class Main extends Application {
14+
private static HostServices services;
1115

1216
@Override
1317
public void start(Stage primaryStage) throws Exception{
14-
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("main.fxml"));
18+
services = getHostServices();
19+
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getClassLoader().getResource("main.fxml")));
1520
primaryStage.setTitle("Dot matrix font generator");
1621
primaryStage.getIcons().add(new Image("ant.png"));
1722
Scene main = new Scene(root, 800, 500);
@@ -23,4 +28,8 @@ public void start(Stage primaryStage) throws Exception{
2328
public static void main(String[] args) {
2429
launch(args);
2530
}
31+
32+
static void openURL(String uri) {
33+
services.showDocument(uri);
34+
}
2635
}

src/main/resources/main.fxml

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<?import javafx.scene.control.MenuBar?>
77
<?import javafx.scene.control.MenuItem?>
88
<?import javafx.scene.control.RadioButton?>
9-
<?import javafx.scene.control.SeparatorMenuItem?>
109
<?import javafx.scene.control.TextField?>
1110
<?import javafx.scene.control.ToggleGroup?>
1211
<?import javafx.scene.layout.GridPane?>
@@ -17,39 +16,9 @@
1716
<children>
1817
<MenuBar id="TopMenu" VBox.vgrow="NEVER">
1918
<menus>
20-
<Menu mnemonicParsing="false" text="File">
21-
<items>
22-
<MenuItem mnemonicParsing="false" text="New" />
23-
<MenuItem mnemonicParsing="false" text="Open…" />
24-
<Menu mnemonicParsing="false" text="Open Recent" />
25-
<SeparatorMenuItem mnemonicParsing="false" />
26-
<MenuItem mnemonicParsing="false" text="Close" />
27-
<MenuItem mnemonicParsing="false" text="Save" />
28-
<MenuItem mnemonicParsing="false" text="Save As…" />
29-
<MenuItem mnemonicParsing="false" text="Revert" />
30-
<SeparatorMenuItem mnemonicParsing="false" />
31-
<MenuItem mnemonicParsing="false" text="Preferences…" />
32-
<SeparatorMenuItem mnemonicParsing="false" />
33-
<MenuItem mnemonicParsing="false" text="Quit" />
34-
</items>
35-
</Menu>
36-
<Menu mnemonicParsing="false" text="Edit">
37-
<items>
38-
<MenuItem mnemonicParsing="false" text="Undo" />
39-
<MenuItem mnemonicParsing="false" text="Redo" />
40-
<SeparatorMenuItem mnemonicParsing="false" />
41-
<MenuItem mnemonicParsing="false" text="Cut" />
42-
<MenuItem mnemonicParsing="false" text="Copy" />
43-
<MenuItem mnemonicParsing="false" text="Paste" />
44-
<MenuItem mnemonicParsing="false" text="Delete" />
45-
<SeparatorMenuItem mnemonicParsing="false" />
46-
<MenuItem mnemonicParsing="false" text="Select All" />
47-
<MenuItem mnemonicParsing="false" text="Unselect All" />
48-
</items>
49-
</Menu>
5019
<Menu mnemonicParsing="false" text="Help">
5120
<items>
52-
<MenuItem mnemonicParsing="false" text="About MyHelloApp" />
21+
<MenuItem fx:id="aboutDialog" mnemonicParsing="false" text="About MyHelloApp" />
5322
</items>
5423
</Menu>
5524
</menus>

0 commit comments

Comments
 (0)