How to change the url thats in the tag in the FXML file with my controller.
The Image is nested in pane > childeren > Imageview > image
see picture
What I want
I want to loop through the imagevies to check the coordinates and with the right coordinate change the url.
This is my loop
for (int i=0; i < 54; i++) {
if (objectsPane.getChildren().get(i).getLayoutX() == village.getX() && objectsPane.getChildren().get(i).getLayoutY() == village.getY()) {
objectsPane.getChildren().get(i). // Setting the url
}
}
How to achieve this?
First of all you need to do a type casting, because objectsPane.getChildren().get(i) gives you the ImageView as of the type Node. Then you can create a new image with the new url. As James commented, it is not possible to just change the url. Finally you replace the old image with the new one. Your code could then look something similar like this:
for (int i = 0; i < 54; i++) {
if (objectsPane.getChildren().get(i).getLayoutX() == village.getX() && objectsPane.getChildren().get(i).getLayoutY() == village.getY()) {
// Type cast from Node to ImageView:
ImageView imageView = (ImageView) objectsPane.getChildren().get(i);
// Create new image with new url:
Image image = new Image(getClass().getResourceAsStream("/assets/img/gameobjects/new-image.png"));
// Replace the old with the new image:
imageView.setImage(image);
// Or alternatively as a one-liner:
//((ImageView) objectsPane.getChildren().get(i)).setImage(new Image(getClass().getResourceAsStream("/assets/img/gameobjects/new-image.png")));
}
}
Related
I have a problem with adding a image with absolute position relative to page size in itext7.
In itext5 I used the code below to determine the image position relative to the page that I'm adding it to
for (int i = 0; i < numberOfPages;) {
page = copy.getImportedPage(reader, ++i);
if(page.getBoundingBox().getWidth() != 595.00f) {
img.setAbsolutePosition(page.getBoundingBox().getWidth() - (595-img.getAbsoluteX()),img.getAbsoluteY());
}
if(page.getBoundingBox().getHeight() != 842.00f) {
img.setAbsolutePosition(img.getAbsoluteX(), page.getBoundingBox().getHeight() - (842-img.getAbsoluteY()));
}
stamp = copy.createPageStamp(page);
stamp.getOverContent().addImage(img);
stamp.alterContents();
copy.addPage(page);
}
Now for itext7 I'm using
public static void addImageToPDF(String inputFilePath, Image img) throws IOException, DocumentException {
File inFile = new File(inputFilePath);
File outFile = new File(inputFilePath + "_image.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfReader(inFile), new PdfWriter(outFile));
Document document = new Document(pdfDoc);
int numberOfPages = pdfDoc.getNumberOfPages();
Rectangle pageSize;
// Loop over the pages of document
for (int i = 1; i <= numberOfPages; i++) {
pageSize = pdfDoc.getPage(i).getPageSize();
if(pageSize.getWidth() != 595.00f) {
img.setFixedPosition(pageSize.getWidth() - (595-img.getImageWidth()),img.getImageHeight());
}
if(pageSize.getHeight() != 842.00f) {
img.setFixedPosition(img.getImageWidth(), pageSize.getHeight() - (842-img.getImageHeight()));
}
document.add(img);
}
}
I need the image to be added in the right top corner relative to the page, but now it adds it in the middle of the screen on the right.
Is there a way to set absolute position in itext7 when adding an image? The image is not always on the same position to the exact width and height so I's a problem for me using fixed position.
I don't get why you need two cases in your for loop. If your goal is to place the image to the top right position of the page and you know the image width and height as well as page width and height, all you need to do is calculate the coordinates to pass to setFixedPosition method.
setFixedPosition accepts x and y coordinates which are image's left bottom coordinates in PDF coordinate system, i.e. left to right, top to bottom.
So you need to subtract image width from page width and do the same for height, which results in the following one-liner:
img.setFixedPosition(pageSize.getWidth() - img.getImageWidth(), pageSize.getHeight() - img.getImageHeight());
I'm using the next code to show image in label , to set nothing to label when picture column is null :
ResultSet rset2 = stmnt.executeQuery("select Picture from Pictures where Client_ID_pass =1" );
while(rset2.next()){
byte[] Passimg = rset2.getBytes("Picture");
//Resize The ImageIcon
ImageIcon Passimage = new ImageIcon(Passimg);
Image Passim = Passimage.getImage();
Image PassmyImg = Passim.getScaledInstance(PassLBL.getWidth(), PassLBL.getHeight(),Image.SCALE_SMOOTH);
ImageIcon newPassImage = new ImageIcon(PassmyImg);
PassLBL.setIcon(newPassImage);
if(Passimg.length < 0){
PassLBL.settext("No Picture");
PassLBL.setIcon(null);
}
}
I've tried the next :
if(Passimg.equals(null)
{PassLBL.settext("No Picture");}
and tried
if(Passimg == null)
{PassLBL.settext("No Picture"); }
but didn't work !
when you retrive the data from the resultSet
byte[] Passimg = rset2.getBytes("Picture");
put if statement there
if(passimg == null) {
label.setText("nothing")
lable.setIcon(null);//to remove the old picture
}else {
//show the image like you did before
lable.setText("");
icon=create your icon here
lable.setIcon(icon);
}
i didn't undertand what you need exactly hope this help you
I have an ImageView in which I load images from camera and gallery , and I do it using imgview.setimagebitmap(bitmap) ,( and I change the image again and again ) it works fine, but when I try to get the current image back for some processing (applying effect or rotate) it returns me the old image not the current one.
I think the problem is with imgview.setimagebitmap(bitmap).
can I be helped?
I have the following code:
try {
iv_image.buildDrawingCache();
Bitmap source = iv_image.getDrawingCache();
} catch(Exception ex) {
int a;
int b;
a = 11;
b = 12;
}
Can you please try to obtain the batman as follows :
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap()
I have a specific problem which has not be answered yet on stackoverflow; I have images in the assets folder numbered like 0.jpg, 1.jpg, 2.jpg etc. Using a for loop I select three images from the asssets folder and I am trying to add these images to a gridview but the images are not showing. The activity starts up okay just no images!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
gridView = (GridView) findViewById(R.id.gridview_result);
// Sets the Tag
gridView.setTag(GRIDVIEW_TAG);
/*
* Adapt the image for the GridView format
*/
imageAdapter = new ImageGridViewAdapter(getApplicationContext());
gridView.setAdapter(imageAdapter);
// Set the orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Retrieve 3 images form the database which appear
// similar
for (int i = 0; i < 3; i++) {
// System.out.println(Retrieval.distances[i][0]);
image = Retrieval.distances[i][0];
int num = (int) image;
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(num);
String imageNum = sBuilder.toString();
System.out.println(imageNum);
String file = imageNum + ".jpg";
try {
// get input stream
InputStream ims = getAssets().open(file);
Log.i("ERROR_IMS", ims + "");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, file);
// set image to ImageView
gridView.setBackground(d);
Log.i("ERROR_d", d + "");
Log.i("ERROR_gridview", gridView+"");
} catch (IOException ex) {
Log.e("I/O ERROR", "Failed when ...");
}
}
}
I believe the issue is occurring in the try/catch. Any help will be much appreciated!
You should get all images first and set it to your adapter.
// set image to ImageView
gridView.setBackground(d);
doesn't affect to your grid items view.
A good tutorial for it: guide
I have two image that is
and this:
I need to place that image (stacked) in a single ImageView. I tried to use blend mode, but doesn't work for ImageView like
Group group = new Group();
group.setBlendMode(BlendMode.SRC_OVER);
// tempImage is array of buffered Images
for(int i=0; i < tempImage.length ;i++){
if(tempImage[i] != null){
ImageView view = new ImageView();
Image im = SwingFXUtils.toFXImage(tempImage[i],
null );
view.setImage(im);
group.getChildren().add(view);
}
}
Just a little trick, instead of using BlendModeuse a HBox, add the images inside the HBox and set the HBox inside the Group
Group group = new Group();
HBox box = new HBox
// tempImage is array of buffered Images
for(int i=0; i < tempImage.length ;i++){
if(tempImage[i] != null){
ImageView view = new ImageView();
Image im = SwingFXUtils.toFXImage(tempImage[i],
null );
view.setImage(im);
box.getChildren().add(view);
}
}
group.getChildren.add(box);
But this wont help you to get the new image, guess you don't even need it !