Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a control for a user to save signatures, could be useful in a number of applications
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
||
|
||
|
||
} |