1. Comics.java
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | package app.comics; import java.io.File; import java.util.ArrayList; import java.util.List; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; import javafx.scene.control.ToggleButton; import javafx.scene.control.TreeView; import javafx.scene.input.KeyCode; import javafx.stage.Stage; import lib.fx.FxContextMenu; import lib.fx.FxToast; import lib.fxapp.FxRunnable; import lib.fxapp.FxWindow; import lib.java.ZipUtil; public class Comics extends Application { final int fontSize = 20; final String baseFolder = "comics"; FxWindow window; Stage mainStage; TreeView<String> fileTree; ComboBox<String> pageComboBox; File folder; List<byte[]> images; int imagesIndex; ToggleButton sideMenuTobble; ToggleButton scrollBarToggle; ToggleButton fullScreenToggle; ComicsImageView comicsImageView; @Override public void start(Stage stage) { mainStage = stage; initialize(stage); window.setMainTitle(stage, "ComicsViewer V0.0.1"); window.setMainIcon(stage, "/res/32/comments.png"); Scene scene = new Scene(window.getMainWindow(), 800, 600); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } void initialize(Stage stage) { window = new FxWindow(stage); //window.hideWindowBorder(stage); window.setStatusTextSize(fontSize); images = new ArrayList<>(); imagesIndex = 0; folder = new File(window.getWorkingFolder() + "/" + baseFolder); if( folder.exists() == false ) { folder.mkdirs(); } fileTree = window.getTreeFileView(folder.getPath(), fontSize, runnableTreeView); fileTree.setShowRoot(false); fileTree.getRoot().setExpanded(true); window.setLeftWindow(fileTree); comicsImageView = new ComicsImageView(fontSize, runnableComicsImageView); comicsImageView.setContextMenu(makeContextMenu()); makeStatusBar(); window.setMainWindow(comicsImageView.getView()); } void makeStatusBar() { pageComboBox = window.getComboBox( fontSize, () -> { gotoPage(); }); sideMenuTobble = window.getToggleImageButton("/res/32/side_menu.png", () -> setSideMenu()); sideMenuTobble.setSelected(true); scrollBarToggle = window.getToggleImageButton("/res/32/statusbar.png", () -> setScrollBar()); scrollBarToggle.setSelected(true); fullScreenToggle = window.getToggleImageButton("/res/32/expand.png", () -> setFullScreen()); fullScreenToggle.setSelected(false); window.addToStatusBar( sideMenuTobble ); window.addToStatusBar( scrollBarToggle ); window.addToStatusBar( fullScreenToggle ); window.addToStatusBar( window.getImageButton("/res/32/first.png", () -> firstPage()) ); window.addToStatusBar( window.getImageButton("/res/32/back.png", () -> prevPage()) ); window.addToStatusBar( pageComboBox ); window.addToStatusBar( window.getImageButton("/res/32/forward.png", () -> nextPage()) ); window.addToStatusBar( window.getImageButton("/res/32/last.png", () -> lastPage()) ); window.addToStatusBar( new Label(" ") ); } public void setSideMenu() { if( sideMenuTobble.isSelected() ) { window.setLeftWindow(fileTree); } else { window.setLeftWindow(null); } } public void toggleSideMenu() { if( sideMenuTobble.isSelected() ) { sideMenuTobble.setSelected(false); } else { sideMenuTobble.setSelected(true); } setSideMenu(); } public void setScrollBar() { if( scrollBarToggle.isSelected() ) { comicsImageView.showScrollBar(); } else { comicsImageView.hideScrollBar(); } } public void toggleScrollBar() { if( scrollBarToggle.isSelected() ) { scrollBarToggle.setSelected(false); } else { scrollBarToggle.setSelected(true); } setScrollBar(); } public void setFullScreen() { if( fullScreenToggle.isSelected() ) { window.fullScreen(true); } else { window.fullScreen(false); } } public void toggleFullScreen() { if( fullScreenToggle.isSelected() ) { fullScreenToggle.setSelected(false); } else { fullScreenToggle.setSelected(true); } setFullScreen(); } ContextMenu makeContextMenu() { FxContextMenu menu = new FxContextMenu(fontSize); menu.addMenu("화면 제어"); { menu.addMenuItem( "전체화면 확대/복귀", new Runnable() { @Override public void run() { toggleFullScreen(); } }); menu.addMenuItem( "왼쪽 메뉴 보이기/감추기", new Runnable() { @Override public void run() { toggleSideMenu(); } }); menu.addMenuItem( "스크롤바 보이기/감추기", new Runnable() { @Override public void run() { toggleScrollBar(); } }); } menu.addRootMenuItem(null, null); menu.addRootMenuItem( "프로그램 종료", new Runnable() { @Override public void run() { System.exit(0); } }); return menu.getMenu(); } public void loadComicsFile(String filePath) throws Exception { System.out.println(filePath); if( new File(filePath).isFile() ) { images = ZipUtil.unzipToByteArray(filePath); pageComboBox.getItems().clear(); for( int i = 0; i < images.size(); i++ ) { pageComboBox.getItems().add( String.valueOf(i+1)); } firstPage(); } } public void setImage(int index) { comicsImageView.setImage(images.get(index)); window.setStatusText(fileTree.getSelectionModel().getSelectedItem().getValue() + " [ " + (index+1) + " / " + images.size() + " ] "); } public void firstPage() { imagesIndex = 0; pageComboBox.getSelectionModel().selectFirst(); } public void lastPage() { imagesIndex = images.size() - 1; pageComboBox.getSelectionModel().selectLast(); } public void nextPage() { if( imagesIndex < (images.size()-1) ) { imagesIndex++; pageComboBox.getSelectionModel().select(imagesIndex); } else { FxToast.makeText(mainStage, "마지막 페이지 입니다", 1000); } } public void prevPage() { if( imagesIndex > 0 ) { imagesIndex--; pageComboBox.getSelectionModel().select(imagesIndex); } else { FxToast.makeText(mainStage, "첫 페이지 입니다", 1000); } } public void gotoPage() { int index = pageComboBox.getSelectionModel().getSelectedIndex(); if( index >= 0 && index <= (images.size()-1) ) { imagesIndex = index; setImage(imagesIndex); } } Runnable runnableExit = new Runnable() { @Override public void run() { if( window.yesno("Exit", "Do you want exit progrma ?") ) { System.exit(0); } } }; Runnable runnableTreeView = new Runnable() { @Override public void run() { try { loadComicsFile(window.getSelectedTreePath(fileTree)); } catch (Exception e) { e.printStackTrace(); } } }; FxRunnable runnableComicsImageView = new FxRunnable() { @Override public void run(Object... object) { if( images != null ) { KeyCode e = (KeyCode)object[0]; if ( e.equals(KeyCode.UP) ) { System.out.println("Up Pressed"); } if ( e.equals(KeyCode.DOWN) ) { System.out.println("Down Pressed"); } if ( e.equals(KeyCode.RIGHT) ) { System.out.println("Right Pressed"); nextPage(); } if ( e.equals(KeyCode.LEFT) ) { System.out.println("Left Pressed"); prevPage(); } if ( e.equals(KeyCode.PAGE_UP) ) { System.out.println("PageUp Pressed"); } if ( e.equals(KeyCode.PAGE_DOWN) ) { System.out.println("PageDown Pressed"); } if ( e.equals(KeyCode.HOME) ) { System.out.println("Home Pressed"); } if ( e.equals(KeyCode.END) ) { System.out.println("End Pressed"); } if ( e.equals(KeyCode.ENTER) ) { System.out.println("Enter Pressed"); } } } }; } |
2. ComicsImageView.java
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | package app.comics; import java.io.ByteArrayInputStream; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Pos; import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.ScrollPane.ScrollBarPolicy; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.input.MouseButton; import javafx.scene.layout.StackPane; import lib.fxapp.FxRunnable; public class ComicsImageView { ImageView imageView; ScrollPane imagePane; Label glassText; StackPane glassView; StackPane layout; public ComicsImageView(int fontSize, FxRunnable runnable) { imageView = new ImageView(); //new Image(new File("D:/a.png").toURI().toString()) imageView.setPreserveRatio(true); imageView.setPickOnBounds(true); imageView.setOnMouseMoved(e -> { if ( e.getX() < imagePane.getWidth() / 4 || e.getX() > imagePane.getWidth() * 3 / 4 ) { if ( e.getX() < imagePane.getWidth() / 4 ) { Platform.runLater(() ->glassText.setText("<")); StackPane.setAlignment(glassView, Pos.TOP_LEFT); } else { Platform.runLater(() ->glassText.setText(">")); StackPane.setAlignment(glassView, Pos.TOP_RIGHT); } glassView.setMaxWidth(imagePane.getWidth() / 4); glassView.setMaxHeight(imagePane.getHeight()); glassView.setVisible(true); } else { glassView.setVisible(false); } e.consume(); }); imageView.setOnMouseClicked( e-> { if( e.getButton().equals(MouseButton.PRIMARY) ) { runnable.run(KeyCode.RIGHT); } }); imagePane = new ScrollPane(); imagePane.setPannable(true); if( fontSize > 0 ) { imagePane.setStyle("-fx-font-size: " + fontSize + " pt;"); } imageView.fitWidthProperty().bind(imagePane.widthProperty()); imagePane.setContent(imageView); imagePane.vvalueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) { if ( new_val.equals(1.0) ) { //runnable.run(KeyCode.RIGHT); } if ( new_val.equals(0.0) ) { //runnable.run(KeyCode.LEFT); } } }); imagePane.setOnKeyPressed(e -> { runnable.run(e.getCode()); }); glassText = new Label(""); glassText.setStyle("-fx-text-fill: goldenrod; -fx-font: italic 20 \"serif\"; -fx-padding: 0 0 20 0; -fx-text-alignment: center"); glassView = new StackPane(); StackPane.setAlignment(glassText, Pos.CENTER); glassView.getChildren().addAll(glassText); glassView.setStyle("-fx-background-color: rgba(0, 50, 100, 0.1); -fx-background-radius: 10;"); glassView.setLayoutX(0); glassView.setLayoutY(0); glassView.setPrefWidth(imageView.getFitWidth() / 4); glassView.setPrefHeight(imageView.getFitHeight()); glassView.setVisible(false); glassView.setOnMouseClicked( e-> { if ( glassText.getText().equals(">") ) { runnable.run(KeyCode.RIGHT); } else { runnable.run(KeyCode.LEFT); } }); glassView.focusedProperty().addListener(new ChangeListener<Boolean>() { @Override public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { if( ! newValue.booleanValue() ) { glassView.setVisible(false); } } }); layout = new StackPane(); StackPane.setAlignment(glassView, Pos.TOP_LEFT); layout.getChildren().addAll(imagePane, glassView); layout.setStyle("-fx-background-color: silver; -fx-padding: 1;"); } public StackPane getView() { return layout; } public void setImage(byte[] image) { System.out.println("ComicsImageView.setImage()"); imageView.setImage(new Image(new ByteArrayInputStream(image))); imagePane.setVvalue(0.0); } public void setContextMenu(ContextMenu menu) { System.out.println("ComicsImageView.setContextMenu()"); imagePane.setContextMenu(menu); } public void hideScrollBar() { imagePane.setHbarPolicy(ScrollBarPolicy.NEVER); imagePane.setVbarPolicy(ScrollBarPolicy.NEVER); } public void showScrollBar() { imagePane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED); imagePane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED); } public void clearGlassView() { glassView.setVisible(false); } } |
3. FxContextMenu.java
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 | package lib.fx; import javafx.scene.control.ContextMenu; import javafx.scene.control.Menu; import javafx.scene.control.MenuItem; import javafx.scene.control.SeparatorMenuItem; public class FxContextMenu { ContextMenu rootMenu; Menu currMenu; int fontSize = 12; public FxContextMenu(int fontsize) { rootMenu = new ContextMenu(); currMenu = null; fontSize = fontsize; } public FxContextMenu() { this(12); } public ContextMenu getMenu() { return rootMenu; } public void addMenu(String text) { currMenu = new Menu(text); currMenu.setStyle("-fx-font-size: " + fontSize + " pt;"); rootMenu.getItems().add(currMenu); } public void addMenuItem(Menu menu, String text, Runnable runnable) { MenuItem item; if( text != null ) { item = new MenuItem(text); if( runnable != null ) { item.setOnAction(e -> runnable.run()); } } else { item = new SeparatorMenuItem(); } if( fontSize > 0 ) { item.setStyle("-fx-font-size: " + fontSize + " pt;"); } if( menu != null ) { menu.getItems().add(item); } else { rootMenu.getItems().add(item); } } public void addMenuItem(String text, Runnable runnable) { addMenuItem( currMenu, text, runnable ); } public void addRootMenuItem(String text, Runnable runnable) { currMenu = null; addMenuItem( currMenu, text, runnable ); } } |
4. ZipUtil.java
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 | package lib.java; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class ZipUtil { public static List<byte[]> unzipToByteArray(String fileName) throws Exception { byte[] buffer = new byte[1024]; List<byte[]> files = new ArrayList<>(); File file = new File(fileName); //System.out.println(file.getPath()); ZipInputStream zis = new ZipInputStream(new FileInputStream(file)); ZipEntry zipEntry = zis.getNextEntry(); while( zipEntry != null ){ String fname = zipEntry.getName(); //System.out.println(fname); ByteArrayOutputStream fos = new ByteArrayOutputStream(); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); files.add(fos.toByteArray()); zipEntry = zis.getNextEntry(); } zis.closeEntry(); //for java.util.Zip zis.close(); return files; } public static void unzipToFoler(File file, File foler) throws IOException { byte[] buffer = new byte[1024]; ZipInputStream zis = new ZipInputStream(new FileInputStream(file)); ZipEntry zipEntry = zis.getNextEntry(); while( zipEntry != null ){ String fileName = zipEntry.getName(); File newFile = new File(foler, fileName); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); zipEntry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } } |
댓글 없음:
댓글 쓰기