-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewController.java
164 lines (145 loc) · 4.63 KB
/
MainViewController.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ke1_rieger_moritz_fx;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.MenuBar;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javax.imageio.ImageIO;
import javafx.stage.Stage;
import ke1_rieger_moritz_fx.generators.*;
/**
*
* @author Moritz Rieger
*/
public class MainViewController implements Initializable {
private SimpleGenerator simpleGenerator;
private Stage primaryStage;
public GraphicsContext gc;
private int canvasHeight;
private int canvasWidth;
private String working_dir;
@FXML
public Canvas canvas;
@FXML
private Text state;
public MainViewController() {
}
/**
* Handle action related to "Save Image" menu item.
*
* @param event Event on "Save Image" menu item.
*/
@FXML
private void handleSaveImageAction(final ActionEvent event)
{
saveImageWithDialog();
}
/**
* Handle action related to "Quit" menu item.
*
* @param event Event on "Quit" menu item.
*/
@FXML
private void handleQuitAction(final ActionEvent event)
{
System.exit(0);
}
/**
* Handle action related to "Simple Generator" menu item.
*
* @param event Event on "Simple Generator" menu item.
*/
@FXML
private void handleSimplegeneratorAction(final ActionEvent event)
{
if(simpleGenerator == null){
this.simpleGenerator = new SimpleGenerator(this);
}else{
simpleGenerator.show();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
setState("bereit");
gc = canvas.getGraphicsContext2D();
//set default directory for saved images
working_dir = System.getProperty("user.dir")+"/Images";
File dir = new File(working_dir);
if(!dir.exists())
dir.mkdir();
}
public void resizeCanvas(int height, int width){
canvasHeight = height;
canvasWidth = width;
canvas.setHeight(canvasHeight);
canvas.setWidth(canvasWidth);
}
/**
* Save Image as png
*/
private void saveImageWithDialog(){
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter =
new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
//Show save file dialog
File file = fileChooser.showSaveDialog(primaryStage);
saveImage2PNG(file);
}
private void saveImage2PNG(File file){
if(file != null){
try {
WritableImage writableImage = new WritableImage(canvasWidth, canvasHeight);
canvas.snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
ImageIO.write(renderedImage, "png", file);
} catch (IOException ex) {
Logger.getLogger(MainViewController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* Automatic saving for generators in default direction /Images as .png
* @param index increment for image sequences
* @param name default should be name of the generator
*/
public void saveImageWithoutDialog(int index, String name){
File file = new File(working_dir+"/"+name+index+".png");
try {
if(file.createNewFile())
saveImage2PNG(file);
} catch (IOException ex){
ex.printStackTrace();
}
}
// GETTER AND SETTER METHODS
public GraphicsContext getGraphicsContext(){
return gc;
}
public void setStage(Stage aThis) {
primaryStage = aThis;
}
public void setState(String msg){
state.setText(msg);
}
}