Skip to content

Commit

Permalink
Signaturesaver
Browse files Browse the repository at this point in the history
a control for a user to save signatures, could be useful in a number of applications
  • Loading branch information
maxbisesi committed Nov 18, 2016
1 parent 5d9c799 commit c5b6330
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Signaturesaver
@@ -0,0 +1,87 @@
class Signaturesaver extends Canvas{
//Signature Saver is node that can be put on gui for users to pen their signatures and save them to a file.
//could be useful in a number of different applications
//fields
double inx;
double iny;
private String savebuttontext;
Button saveButton = new Button(savebuttontext);
//Group root = new Group();
GraphicsContext gc;
WritableImage signature;
final FileChooser fc = new FileChooser();
private Stage stage;
private static int filecounter = 0;

public Signaturesaver(){
//if they dont provide a primary stage, the image will be saved to a default filename in the current dir;
this(null);
}
public Signaturesaver(Stage currentstage){

//the user can either call getsaveButton() and add that to this one to their container along with the Signaturesave canvas
//or they can provide their own.
//A Signaturesave will not work without a save button though.

super(800,500);
filecounter++;
gc = this.getGraphicsContext2D();
signature = new WritableImage((int)this.getWidth(), (int)this.getHeight());
stage = currentstage;


addEventHandler(MouseEvent.MOUSE_PRESSED,new EventHandler<MouseEvent>() {

public void handle(MouseEvent e) {
inx = e.getSceneX();
iny = e.getSceneY();
e.consume();

}
});

addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
gc.lineTo(e.getX(), e.getY());
gc.stroke();
e.consume();
}
});

saveButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e){
File imageFile;

snapshot(null,signature);
if(stage == null){
imageFile = new File("SavedSignature"+filecounter+".png");
}else
imageFile = fc.showSaveDialog(stage);

try{
ImageIO.write(SwingFXUtils.fromFXImage(signature,null),"png", imageFile);
}catch(IOException ioe){
System.out.println("something went wrong writing the image to disk");
}

}
});


}

public Button getSaveButton(){
return saveButton;
}
public void setSaveButtonText(String text){
savebuttontext = text;
}
public void setButton(Button savebutton){
this.saveButton = savebutton;
}




}

0 comments on commit c5b6330

Please sign in to comment.