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 !
Related
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")));
}
}
This is my code:
Image image1 = new Image(url);
Dialog mainDialog = new Dialog(true);
ImageView imageView = new ImageView();
imageView.setImage(image1);
imageView.setPreserveRatio(true);
imageView.setCache(true);
imageView.setSmooth(true);
imageView.setPickOnBounds(true);
imageView.setOnZoom(evt->{
imageView.setScaleX(imageView.getScaleX()*evt.getZoomFactor());
imageView.setScaleY(imageView.getScaleY()*evt.getZoomFactor());
});
mainDialog.setContent(imageView);
Platform.runLater(() -> mainDialog.showAndWait());
You don't provide much information about the image quality, and what do you mean by pixelated, at what scale it happens, or if it is only on mobile and not on desktop.
Anyway, here are a few hints to improve the results.
Add a hint to the cache, in this case CacheHint.SCALE.
Add clipping to the imageView. If the scale is too big, the image will be big on memory. This will prevent a possible memory issue. Bind the size of the rectangle to the view dimensions.
This works fine for me, even with scale factors of 20+:
Image image1 = new Image(url);
Dialog mainDialog = new Dialog(true);
ImageView imageView = new ImageView();
imageView.setImage(image1);
imageView.setPreserveRatio(true);
imageView.setCache(true);
imageView.setCacheHint(CacheHint.SCALE); // <-- hint
imageView.setSmooth(true);
imageView.setPickOnBounds(true);
imageView.setOnZoom(evt -> {
imageView.setScaleX(imageView.getScaleX() * evt.getZoomFactor());
imageView.setScaleY(imageView.getScaleY() * evt.getZoomFactor());
});
Rectangle r = new Rectangle();
imageView.setClip(r); // <-- clip
r.widthProperty().bind(view.widthProperty());
r.heightProperty().bind(view.heightProperty());
mainDialog.setContent(imageView);
mainDialog.showAndWait(); // <-- No need of runLater
I have a code which adds buttons and images dynamically to a fragment. The buttons are getting added but the images are not being added.
Here is my code :
for(String f1 : f){
// For Each File Add a View or a button or something
LogUtil.d(TAG,"ocr override" + f1);
Button b = new Button (getActivity());
b.setId (i);
b.setLayoutParams(params2);
b.setText (f1);
b.setOnClickListener(this);
ImageView img_view = new ImageView(getContext());
img_view.setImageBitmap(getBitmapFromAsset("OcrSampleImages"+System.getProperty("file.separator")+f1));
img_view.setLayoutParams(params);
linearLayout.addView(b);
linearLayout.addView(img_view,params);
i++;
}
params and params2 are defined as follows :
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Here is the output on the emulator :
Here is my assets folder :
Better to use iterative strategy..
first, new ImageView(getContext()); -> new ImageView(**getActivity()**);
Secondly, if you don't know where is the problem, then better reading default drawable image
img_view.setImageResource(R.drawable.<any_dummy_image_from_drawable>);
instead of
setImageBitmap()
if your dummy image load properly that means your View is perfect and having problem while reading from assets,
If so, then you can concentrate more on asset reading thing.
I tried this:
public void addTargetCard(MissionCard mCard) {
int card = mCard.GetID();
leftSide.getChildren().removeAll(targetCardBox);
Image image = new Image(
MainApp.class.getResourceAsStream("images/target" + card
+ ".png"));
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitHeight(81);
imageView.setFitWidth(108);
imageView.setPreserveRatio(true);
imageView.setPickOnBounds(true);
Tooltip.install(imageView, new Tooltip(intToCity(mCard.getStart())
+ " - " + intToCity(mCard.getFinish())));
targetCardBox.getChildren().add(imageView);
leftSide.getChildren().add(targetCardBox);
}
Can somebody explain me why my Tooltip doesn't work - i got no idea what i did wrong. (It's my first time that i use Tooltips's)
somebody else told me that ImageView doesnt work with tooltips and gave me this workaround - but i have again no tooltip when i move with my mouse over the label
public void addTargetCard(MissionCard mCard) {
int card = mCard.GetID();
leftSide.getChildren().removeAll(targetCardBox);
Image image = new Image(
MainApp.class.getResourceAsStream("images/target" + card
+ ".png"));
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitHeight(81);
imageView.setFitWidth(108);
imageView.setPreserveRatio(true);
imageView.setPickOnBounds(true);
Label label = new Label();
label.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
label.setGraphic(imageView);
label.setTooltip(new Tooltip(intToCity(mCard.getStart()) + " - "
+ intToCity(mCard.getFinish())));
targetCardBox.getChildren().add(label);
leftSide.getChildren().add(targetCardBox);
}
Installing a tooltip to image view is working. Try yourself with new sample JavaFX project and see it. When you doubt about some functionality of the used API (JavaFX in this case) try to isolate the doubted use case into new fresh environment/project and observe it closely.
P.S. Why are you removing the targetCardBox from leftSide and adding it again afterwards.
I want to fetch Images from my URL and add it to the radio button that is further added to a radio group. However, i could not find metod such as setText to set text for radio button. I wish to add image to my radio button in similar fashion. Is there any hint/method that can do the same for me? Any hint/code example would be really helpful for me,
RadioGroup rg = new RadioGroup (this);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton rb[]= new RadioButton[children.size()];
for (int i = 0; i < var.size(); i++)
{
Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);
MovieName[i]=movieAtt.getAttributeValue( "Title" );
MovieCover[i]=movieAtt.getAttributeValue( "cover" );
ShowTime[i]=movieAtt.getAttributeValue( "showtime" );
rb[i] = new RadioButton(this);
rb[i].setText(movieAtt.getAttributeValue("Title"));
rg.addView(rb[i]);
//Calling this func to get Images
//LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" ));
//rb[i].buildDrawingCache(LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" )));
public static Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
return null;
}
}
Is the problem with the image or the text? As for the image, I believe this should work:
Drawable d = LoadImageFromWebOperations(movieAtt.getAttributeValue("cover"));
rb[i].setButtonDrawable(d);
Make sure to set gravity to Gravity.CENTER or your image won't be centered around the radio button.
You should find information about the text in the RadioButton reference.