I have created a network view diagram using zest framework, this uses SWT display/shell to display the UI. I want to export the UI to an image/pdf.
How to do it? Any ideas?
You can use the SWT GC.copyArea() method to copy the contents of a Control to an Image, then save the Image to file. For example, if you have a Zest GraphViewer, viewer, the following code will copy its contents to a PNG file named out.png.
GC gc = new GC(viewer.getControl());
Rectangle bounds = viewer.getControl().getBounds();
Image image = new Image(viewer.getControl().getDisplay(), bounds);
try {
gc.copyArea(image, 0, 0);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save("c:\\out.png", SWT.IMAGE_PNG);
} finally {
image.dispose();
gc.dispose();
}
Related
I tested this code in order to create dialog with image.
final int xSize = 400;
final int ySize = 280;
final Color backgroundColor = Color.WHITE;
final String text = "SQL Browser";
final String version = "Product Version: 1.0";
final Stage aboutDialog = new Stage();
aboutDialog.initModality(Modality.WINDOW_MODAL);
Button closeButton = new Button("Close");
closeButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
aboutDialog.close();
}
});
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Image img = new Image("logo.png");
ImageView imgView = new ImageView(img);
grid.add(imgView, 0, 0);
grid.add(new Text(text), 0, 1);
grid.add(new Text(version), 0, 2);
grid.add(closeButton, 14, 18);
Scene aboutDialogScene = new Scene(grid, xSize, ySize, backgroundColor);
aboutDialog.setScene(aboutDialogScene);
aboutDialog.show();
I placed the image file into the directory /src.
But for some reason the image is not displayed. Can you help me to correct my mistake?
Simply replace this code:
Image img = new Image("logo.png");
with this
Image img = new Image("file:logo.png");
Docu reference.
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
When you pass a String to the Image class it can be handled in four different ways (copied from docu):
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png");
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png");
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png");
// The image is located in the current working directory
Image image4 = new Image("file:flower.png");
The file: prefix is simply an URI scheme, or in other words the counterpart to the http: protocol classifier. This also works in the file browser, or in the web browser... ;)
For further reference, you can take a look at the wiki page of the file URI scheme: https://en.wikipedia.org/wiki/File_URI_scheme
Happy Coding,
Kalasch
Try this:
img = new Image("/logo.png");
If no protocol part indicating a URL (as http: or file:) is given, the file is supposed to reside in the default package. If you want it to put in a different package say com.my.images you add this information in a path like manner:
img = new Image("/com/my/images/logo.png");
Image img = new Image("file:/logo.png");
or way with path:
Image img = new Image("file:c:/logo.png");
or
File f = new File("c:\\logo.png");
Image img = new Image(f.toURI().toString());
also can use:
new Image(file:src/logo.png) //root of project
This functions:
Image image = new Image(getClass()
.getResourceAsStream("ChimpHumanHand.jpg"));
copy and paste the image into folder where source package(source packages in NetBeans IDE) is present. Then
Image image = new Image("a1.jpg");
Image image = new Image("File:a1.jpg");
both will work.
i have be trying to upload the signature captured from the codename one signature to my php server.the problem is that the image uploaded is a black image.Below is my code.how can i fix this
SignatureComponent sig = new SignatureComponent();
sig.addActionListener((evt)-> {
try{
img = sig.getSignatureImage();
}catch(Exception ex){
ex.printStackTrace();
}
// Now we can do whatever we want with the image of this signature.
});
Button sv = new Button("save");
sv.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt) {
try {
Label it = new Label();
it.setIcon(img);
orderHome.add(it);
ImageIO imgIO= ImageIO.getImageIO();
ByteArrayOutputStream out = new ByteArrayOutputStream();
imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
byte[] ba = out.toByteArray();
MultipartRequest request = new MultipartRequest();
String url = Global.url1 + "upload_photo.php";
request.setUrl(url);
request.addData("file",ba,"image/jpeg");
request.addArgument("order_id", order_id);
request.addArgument("customer_id", customer_id);
NetworkManager.getInstance().addToQueue(request);
and the php code
[![image uploaded][1]][1]
<?php
#SESSION_START();
require_once("../includes/functions.php");
$target_path="../uploads/";
$customer_id=$_REQUEST['customer_id'];
$order_id=$_REQUEST['order_id'];
$uid = uniqid();
$file =$uid.".jpg";
$sucess=move_uploaded_file($_FILES["file"]["tmp_name"], $target_path.$file);
the black img is the file which is uploaded to the server.the other shows the screenshot of the running app.i would like to upload the signature as shown in the screenshot
The signature generates a translucent image. JavaSE has some issues with saving translucent images as JPEGs and thus PNG works well. Another alternative would be to create an opaque image and save that as a JPEG e.g.:
Image myImage = Image.create(img.getWidth(), img.getHeight());
myImage.getGraphics().drawImage(img, 0, 0);
The new myImage will be opaque with the white color background.
In my JavaFX application I use a TreeView. The single TreeItems contain an image. Unfortunately, the image is shown only once.
The code for loading the image looks like the following. It is called every time the TreeView changes. The Images are cached in a Map ("icons"). Thus, a new ImageView is created every time.
public final ImageView getImage(final String imageConstant) {
final Image img;
if (icons.containsKey(imageConstant)) {
img = icons.get(imageConstant);
}
else {
if (isRunFromJAR) {
img = new Image("/path/" + imageConstant, iconSizeWidth,
iconSizeHeight, false, false);
}
else {
img = new Image(getClass().getResourceAsStream(imageConstant), iconSizeWidth,
iconSizeHeight, false, false);
}
icons.put(imageConstant, img);
}
return new ImageView(img);
In the updateItem method of my TreeCells, the ImageView returned above is stored into a field called icon. The field is used in the following code, also from updateItem:
if (icon == null) {
setGraphic(label);
}
else {
final HBox hbox = new HBox(ICON_SPACING);
final Label iconLabel = new Label("", icon);
hbox.getChildren().addAll(iconLabel, label);
setGraphic(hbox);
}
But for some reason, the problem shown in the gif occurs.
Update
Actually, the ImageViews were cached internally, which led to de-duplication. Using multiple ImageViews is the solution.
the ImageView returned above is stored into a field called icon.The field is used in the following code...
The ImageView is type of Node. A Node can only have one parent at a time!
So don't store/cache a ImageView(-Node). Create new ImageView-Nodes.
Edit:
As pointed out in the comments by James_D:
...you can use the same Image(-Object) for every ImageView...
I am working on a JavaFX application. I want to copy image from application using context menu and paste it using Windows feature of paste.
File file = new File("C:\\Users\\Admin\\Desktop\\my\\mysql.gif");
Image image = new Image(file.toURI().toString());
ImageView ive =new ImageView(image);
cm = new ContextMenu();
MenuItem copy = new MenuItem("Copy");
cm.getItems().add(copy);
copy.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
//Paste Image at location
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putImage(image); // the image you want, as javafx.scene.image.Image
clipboard.setContent(content);
}
});
For example, like shown below in images.
And want to paste at location from using of Windows features menu.
Use the Clipboard and ClipboardContent, e.g. as:
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
// for paste as image, e.g. in GIMP
content.putImage(image); // the image you want, as javafx.scene.image.Image
// for paste as file, e.g. in Windows Explorer
content.putFiles(java.util.Collections.singletonList(new File("C:\\Users\\Admin\\Desktop\\my\\mysql.gif")));
clipboard.setContent(content);
For the "Paste" operation of Windows context menus to work, the clipboard content has to be File. In the case demonstrated above, this is easy, otherwise a temporary file should be created.
I imported an image into Eclipse, in the same package as this class:
public class mainWindow extends JFrame {
public mainWindow() {
Image bg = // \mainPackage\ShittyPlane.png;
Graphics2D g2d;
this.setSize(500,500);
this.setResizable(false);
this.setTitle("GameTest");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
g2d.drawImage(bg, 0, 0, null);
}
}
How do I define the image path?
If the image is part of you source and is packed into jar later for distribution i would sucgest you get a stream to the image using getResourceAsStream.
ClassLoader cl = getClass().getClassLoader();
InputStream is = cl.getResourceAsStream("mainPackage/ShittyPlane.png");
BufferedImage image = ImageIO.read(is);
this aproache will also work in if you run the program from your IDE
If you plan to locate the image using a a File chooser then go with #Pescis's answer.
What you need to do to load an image from a specific file is:
BufferedImage img = null;
try {
img = ImageIO.read(new File("src/mainPackage/ShittyPlane.png")); //I'm guessing this is the path to your image..
} catch (IOException e) {
}
For more info you can read the javadoc on working with images.