I am trying to take a snapshot of a scene (which is essentially a diagram maker) and store it as a .png file. However, as the top part of the scene has menu bars, I want to ensure that only the part of the scene below the menu bars is getting captured. I thought of using Snapshot Parameters to ensure this, but for whatever reason the snapshot method doesn't accept my SnapshotParameters class.
Here is the method that takes the snapshot. The issue is that scene.snapshot will not accept the SnapshotParameters param. The code below will work perfectly accept it isn't able to "crop out" the top part of the scene which contains the menubars.
public void exportDiagram() throws IOException {
TextInputDialog inputDialog = new TextInputDialog();
inputDialog.getDialogPane().setContentText("Save image as:");
inputDialog.showAndWait();
TextField userInput = inputDialog.getEditor();
Scene scene = main.getScene();
SnapshotParameters param = new SnapshotParameters();
param.setViewport(new Rectangle2D(0, 23, 600, 377));
WritableImage imgReturn = scene.snapshot(null);
File file = new File(userInput.getText() + ".png");
ImageIO.write(SwingFXUtils.fromFXImage(imgReturn, null), "png", file);
}
Related
I am collecting images from users with a form field on a PDF form. When the field is empty the user can populated the field in Acrobat and I can successfully read it from the form using iText7. If the user has previously uploaded an image, I want to present them with that image alreadyloaded into the form field and allow them to select and submit a different image. iText allows me to populate the form with the image but I distorts the image's aspect ration by resizing it to the dimensions of the form field's rectangle.
Is there a way to get iText's setImage() method to maintain the aspect ratio when loading the image?
I have also tried using the following code to modify the form field's rectangle to conform to the image aspect ratio before loading the image:
PdfReader reader = new PdfReader("TestForm.pdf");
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(os);
StampingProperties properties = new StampingProperties();
properties.useAppendMode();
PdfDocument document = new PdfDocument(reader, writer, properties);
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(document, false);
acroForm.setNeedAppearances(true);
// get button form field
String fieldName = "Image1_af_image";
PdfButtonFormField field = (PdfButtonFormField)acroForm.getField(fieldName);
// retrieve widget rectangle
PdfDictionary widgets = field.getWidgets().get(0).getPdfObject();
com.itextpdf.kernel.geom.Rectangle rect = widgets.getAsRectangle(PdfName.Rect);
// modify its width
field.setImage("/Users/sschultz/Desktop/zuni logo.jpg").setFieldName(fieldName);
document.close();
os.flush();
os.close();
FileUtils.writeByteArrayToFile(new File("TestForm_out.pdf"), os.toByteArray());
but this code fails to modify the form field's original dimensions.
Finally, I have attempted to add a second, new form field with the appropriate aspect ratio:
// add second button field to form
String fieldName2 = "Image2_af_image";
PdfButtonFormField imageField = PdfFormField.createButton(document, new Rectangle(10, 10, 200, 50),
PdfButtonFormField.FF_PUSH_BUTTON);
imageField.setImage("image2.jpg").setFieldName(fieldName2);
acroForm.addField(imageField);
but the second field never appears in the form.
The default behavior of drawing button field appearance that doesn't preserve image's aspect ratio is quite complicated to override. Instead I can suggest to generate appearance manually, making it exactly as you want it.
// get widget dictionary
List<PdfWidgetAnnotation> widgets = pushButtonField.getWidgets();
PdfDictionary widgetDict;
if (widgets.size() > 0) {
widgetDict = widgets.get(0).getPdfObject();
} else {
// widgets.size() == 0 shouldn't really happen to properly created
// existing fields, but let's do it just in case
widgetDict = pushButtonField.getPdfObject();
}
Rectangle origRect = widgetDict.getAsRectangle(PdfName.Rect);
float borderWidth = pushButtonField.getBorderWidth();
String imgPath = ... // path to image file
// draw custom appearance preserving original field sizes
PdfFormXObject pdfFormXObject = new PdfFormXObject(new Rectangle(origRect.getWidth() - borderWidth * 2, origRect.getHeight() - borderWidth * 2));
Canvas canvas = new Canvas(pdfFormXObject, pdfDoc);
// Image class preserves aspect ratio by default
Image image = new Image(ImageDataFactory.create(imgPath))
.setAutoScale(true)
.setHorizontalAlignment(HorizontalAlignment.CENTER);
Div container = new Div()
.setMargin(borderWidth).add(image)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setFillAvailableArea(true);
canvas.add(container);
canvas.close();
// override original appearance with new one
PdfDictionary apDict = new PdfDictionary();
widgetDict.put(PdfName.AP, apDict);
apDict.put(PdfName.N, pdfFormXObject.getPdfObject());
// mark widgetDict as modified in order to save its changes in append mode
widgetDict.setModified();
The last line (calling setModified()) is required to be done for all low level PdfObjects modifications if you are using properties.useAppendMode();. Otherwise the change will be not saved.
I'm trying to fit my Content to my scrollPane. However, I cannot scroll until the end of the content. It work if resize the whole pane to my PC screen (1920,1080), however it can't fit for my laptop resolution (1366,768).
This is what happen
///////////////////////////////////////////////////CENTER SIDE OF THE PANE///////////////////////////////////////////////////
//Scroll Pane for insert centerbox in it
ScrollPane scroll = new ScrollPane();
//Grid pane for center box
GridPane centerbox= new GridPane();
centerbox.setHgap(15);
centerbox.setVgap(30);
//Input picture
javafx.scene.image.Image img = new
javafx.scene.image.Image(getClass().getResource("arcade.jpg").toExternalForm());
ImageView imgv1 = new ImageView(img);
imgv1.setFitHeight(150);
imgv1.setFitWidth(150);
imgv1.setVisible(true);
Image img2 = new Image(getClass().getResource("Bowling.jpg").toExternalForm());
ImageView imgv2 = new ImageView(img2);
imgv2.setFitHeight(150);
imgv2.setFitWidth(150);
imgv2.setVisible(true);
Image img3 = new Image(getClass().getResource("cc.jpg").toExternalForm());
ImageView imgv3 = new ImageView(img3);
imgv3.setFitHeight(150);
imgv3.setFitWidth(150);
imgv3.setVisible(true);
Image img4 = new Image(getClass().getResource("circus.jpg").toExternalForm());
ImageView imgv4 = new ImageView(img4);
imgv4.setFitHeight(150);
imgv4.setFitWidth(150);
imgv4.setVisible(true);
Image img5= new Image(getClass().getResource("karaoke.jpg").toExternalForm());
ImageView imgv5 = new ImageView(img5);
imgv5.setFitHeight(150);
imgv5.setFitWidth(150);
imgv5.setVisible(true);
Image img6= new Image(getClass().getResource("kidzania.jpg").toExternalForm());
ImageView imgv6 = new ImageView(img6);
imgv6.setFitHeight(150);
imgv6.setFitWidth(150);
imgv6.setVisible(true);
Image img7= new Image(getClass().getResource("skatepark.jpg").toExternalForm());
ImageView imgv7 = new ImageView(img7);
imgv7.setFitHeight(150);
imgv7.setFitWidth(150);
imgv7.setVisible(true);
//////////////////////////////////////////////COMBINING ALL THE PANE TOGETHER////////////////////////////////////////////////////////
//Place Node on Pane(1)
centerbox.add(imgv1,0,0);
Button buy = new Button("Reserve");
Label l1 = new Label();
l1.setText("'Arcade-ium'\n\n\nAt Arcade-ium, you can enjoy a lot of classic and modern arcade game.\nWe assure you your satisfaction");
l1.setFont(Font.font("Helvetica",FontWeight.BOLD,20));
centerbox.add(buy,2,0);
centerbox.add(l1,1,0);
//Event Handler to change scene to reservepage
buy.setOnAction(e->{
Stage stg = (Stage)buy.getScene().getWindow();
stg.setScene(rp.getScene1());
});
///Repeat until 7 node
//Place Node on Pane(7)
centerbox.add(imgv7,0,6);
Button buy6 = new Button("Reserve");
Label l7 = new Label();
l7.setText("'xTremePark'\n\nxTremePark was built in 2011. We have a lot of facilities here to facilitate our youngster who\nlove to take the risk and go beyond.");
l7.setFont((Font.font("helvetica",FontWeight.BOLD,20)));
centerbox.add(l7,1,6);
centerbox.add(buy6,2,6);
//Event Handler to change scene to reservepage
buy6.setOnAction(e->{
Stage stg = (Stage)buy6.getScene().getWindow();
stg.setScene(rp.getScene7());
});
//Formatting the pane
scroll.setContent(centerbox);
//Im using borderpane for the whole scene. And Im going to insert the scrollpane into the center.
I cannot seem to scroll until the end my content when i run the program. I tried to use .setMinHeight() and .setPrefHeight().
but nothings work. So is there any way for me to set how much the scroller would scroll in my scrollpane?
Image 1
In the code it is pretty easy:
// Clip
Rectangle rect = new Rectangle(168, 168);
rect.setArcHeight(30);
rect.setArcWidth(30);
rect.setEffect(new Reflection());
imageView.setClip(rect); //where imageView is an ImageView
I am trying to do it using SceneBuilder but i don't know
how..Any help will be appreciated.
If i well understood you are trying to populate a node (like a box or box or a pane or whatever you want)with your object rectangle, i think it's not possible doing it from the Scene Builder, you must do it from the code.
If you want to do it you have add it to a subScene i think!
PerspectiveCamera camera = new PerspectiveCamera();
ImageView imageview = new ImageView();
Rectangle rect = new Rectangle(168, 168);
rect.setArcHeight(30);
rect.setArcWidth(30);
rect.setEffect(new Reflection());
imageview.setClip(rect);
Group g = new Group(imageview);
SubScene subSceneInsertion = new SubScene(g, 919, 600, true, SceneAntialiasing.BALANCED);
subSceneInsertion.setFill(Color.DARKSLATEGREY);//yourcolour
subSceneInsertion.setCamera(camera);
Group groupForSubScene = new Group(subSceneInsertion);
groupForSubScene.autoSizeChildrenProperty().setValue(Boolean.TRUE);
groupForSubScene.setAutoSizeChildren(true);
hBoxForRectangle.getChildren().add(groupForSubScene);
I have a JavaFX tableview in which I'm listing files and their properties, including the ICON of the file. My problem is that a part of the icon is missing.
I don't know how to describe it completely so I included an image below.
My tableview is to the right and windows explorer is to the left.
Here is the code:
#Override
public void updateItem(ImageIcon imageIcon, boolean empty){
if (imageIcon != null){
// I have tried adding minimum width and height too
HBox box= new HBox();
// JavaFX scene imageview
ImageView imageView = new ImageView();
// Have tried with and without setFitHeight
imageView.setFitHeight(16);
imageView.setFitWidth(16);
// This line prints: 16 16
System.out.println(imageIcon.getIconWidth() + " " + imageIcon.getIconHeight());
// Create BufferedImage of the imageicon
BufferedImage bi = new BufferedImage(
imageIcon.getIconWidth(),
imageIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
imageIcon.paintIcon(null, g, 0, 0);
g.dispose();
// BufferedImage to fxImage
Image fxImage = SwingFXUtils.toFXImage(bi, null);
imageView.setImage(fxImage);
box.getChildren().addAll(imageView);
setGraphic(box);
}
}
Thanks.
UPDATE
I have google around some more and i copied an example directly and pasted into a new project, and the same thing happened with the icons. In the example, the user was probably using windows 7 by judging the look of the icons, so maybe this is just the way it looks in windows 8. It was the code in the answer at the bottom with a picture: www.stackoverflow.com/questions/28034432/
I am trying to make GUIs in SWT using Jigloo and when I want to set images to Labels or Buttons, I can see them in the preview pane. However, when i compile and run it, it throws
SWTResourceManager.getImage: Error getting image img/game/front/test.png, java.lang.IllegalArgumentException: Argument cannot be null
The code it generates looks like this:
opIcon = new Label(this, SWT.NONE);
FormData opIconLData = new FormData();
opIconLData.width = 64;
opIconLData.height = 64;
opIconLData.left = new FormAttachment(0, 1000, 12);
opIconLData.top = new FormAttachment(0, 1000, 12);
opIcon.setLayoutData(opIconLData);
opIcon.setImage(SWTResourceManager.getImage("img/game/front/test.png"));
The SWTResourceManager class seems to be largely undocumented so I'm kinda lost here. Any help?
This method requires an absolute path,
SWTResourceManager.getImage("/home/img/game/front/test.png");