Instructions
Objective
Write a program to create a music preview system in java language.
Requirements and Specifications
Description: I need help creating a method for JavaFX project I am working on. It is a project that prints a document displayed on a javafx pane. The pane can be long or short depending on input of user. The problem I am having is when the pane height is too long to fit in one page, the printer does not print the rest of it on a second page. Instead it cuts off after 1 page. I need a method that can print the pane on as many pages as needed depending on its height instead of just fitting to one page and cutting off. I have attached the code I have so far. Please scroll to the printHandle() method I made. That is where I need help with.
Source Code
package GUI;
import java.io.IOException;
import instruments.Guitar;
import instruments.Drumset;
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.print.PageLayout;
import javafx.print.PageOrientation;
import javafx.print.Paper;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Translate;
import javafx.stage.Window;
import javafx.stage.Stage;
import models.ScorePartwise;
public class PreviewMusic extends Application{
private MainViewController mvc;
@FXML private Pane pane;
public Window convertWindow;
@FXML
private Button printButton;
final BooleanProperty printButtonPressed = new SimpleBooleanProperty(false);
private ScorePartwise scorePartwise;
@FXML private AnchorPane anchorPane;
public PreviewMusic() {}
@FXML
public void initialize() {}
public void setMainViewController(MainViewController mvcInput) {
mvc = mvcInput;
}
// We can use this method to update the music sheet
public void update() throws IOException
{
// Get the ScorePartwise object directly
scorePartwise = mvc.converter.getScorePartwise();
/* Get the list of measures from the ScorePartwise object.
*
* We get the list of Parts, there should be only one Part in this list,
* so we get the first item, which is the Part, then we get the measures from that Part.
*/
String instrument = scorePartwise.getPartList().getScoreParts().get(0).getPartName();
if(instrument == "Guitar") {
Guitar g = new Guitar(scorePartwise, pane);
g.drawGuitar();
}
else if (instrument == "Drumset") {
Drumset d = new Drumset(scorePartwise, pane);
d.draw();
}
}
@FXML
public <printButtonPressed> void printHandle() {
printButton.setOnAction( aEvent -> {
printButtonPressed.set(true);
});
WritableImage screenshot = pane.snapshot(null, null);
Printer printer = Printer.getDefaultPrinter();
PageLayout layout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
double pagePrintableWidth = layout.getPrintableWidth();
double pagePrintableHeight = layout.getPrintableHeight();
final double scaleX = pagePrintableWidth / screenshot.getWidth();
final double scaleY = pagePrintableHeight / screenshot.getHeight();
final ImageView print_node = new ImageView(screenshot);
print_node.getTransforms().add(new Scale(scaleX, scaleX));
PrinterJob printSheet = PrinterJob.createPrinterJob();
if (printSheet != null && printSheet.showPrintDialog(pane.getScene().getWindow())) {
double numberOfPages = Math.ceil( scaleX / scaleY);
Translate gridTransform = new Translate(0, 0);
print_node.getTransforms().add(gridTransform);
for(int i = 0; i < numberOfPages; i++)
{
gridTransform.setY(-i * (pagePrintableHeight / scaleX));
printSheet.printPage(layout, print_node);
}
printSheet.endJob();
}
}
@FXML
public void playHandle() {
ScorePartwise scorePartwise = mvc.converter.getScorePartwise();
String instrument = scorePartwise.getPartList().getScoreParts().get(0).getPartName();
if(instrument == "Guitar") {
Guitar g = new Guitar(scorePartwise, pane);
g.playNote();
}
}
// private Window openNewWindow(Parent root, String windowName) {
// Stage stage = new Stage();
// stage.setTitle(windowName);
// stage.initModality(Modality.APPLICATION_MODAL);
// stage.initOwner(MainApp.STAGE);
// stage.setResizable(false);
// Scene scene = new Scene(root);
// stage.setScene(scene);
// stage.show();
// return scene.getWindow();
// }
public void handleGotoMeasure() {}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
}
}