I have a large image that I'm loading from a file. I need to put it on two ImageView's (ivInput,ivLayer1) and resize it to 32x32. ivInput is resizing to 32x32 succesfully, but when I take its snapshot it has a wrong size (32x25).
Image imgLetterBox = loadFromFile("m1.png");
ivInput.setImage(imgLetterBox);
ivInput.setFitWidth(32);
ivInput.setFitHeight(32);
System.out.println("ImgInput:" + ivInput.getFitWidth() + ", " + ivInput.getFitHeight());//32x32, it's ok
Image imgLayer1 = ivInput.snapshot(null,null);
System.out.println("ImgLayer1:" + imgLayer1.getWidth() + ", " + imgLayer1.getHeight());//32x25. why?
Problem was in PreserveRatio property, it was on true value. I changed it on disable value and it is working well now.
Related
I am trying to set a couple newlines in a java swing window, but for some reason everything is printed on one line.
This is my code:
private JLabel lblOutput;
guess = 79;
numberOfTries = 5;
message = guess + " is correct. " + "\n" + "Let's play again! " + "\n" + "And it only took you " + numberOfTries + " number of tries!";
lblOutput.setText(message);
The output of the above is:
79 is correct. Let's play again! And it only took you 5 number of tries!
What am I doing wrong?
A label is designed to only display a single line of text.
However, you can use simple HTML to split the text on multiple lines:
label.setText("<html>line1<br>line2</html>");
If lblOutput is a JLabel, it only accepts a single line of text.
You can try using HTML formatting to get around it.
I'm having a problem extracting the full style Attribute, as part of it still didn't reached the DOM- Its an image that sometimes takes 1-2 seconds to load in the screen, so what happens is that there IS a string with attribute, but it CHANGES when the image arrives from the server.
I'm doing a WebDriverWait for the element to be visible in the DOM, but when I try to take its "style" attribute (that has the "...;(url:"http://....")", it sometimes isn't there, and then my substring() fails.
Here is the code:
#Step("Print Image src url")
public String printImage(Integer imgNo){
WebElement imgStyle = (new WebDriverWait(driver, 15)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Consts.ACTIVE_IMG_XPATH_1000 + "/div[" + (currentImg + 1) +"]/div[1]")));
String styleString = imgStyle.getAttribute("style");
Integer idxUrl = styleString.indexOf("url");
Integer idxJpg = styleString.indexOf("jpg");
String urlImage = styleString.substring(idxUrl+5,idxJpg+3); //styleString.indexOf("http"),20
Log.info("V - Image " + imgNo + " src is: " + urlImage);
return urlImage;
}
I can simply add System.wait(3000), but I don't wan't to use it. Any ideas how to solve it nicer? Can I wait for visibility of String?
Thanks for your time.
What is the HTML like? You could use contains or starts-with and the beginning of the url before the part that is dynamic and changes. Like WebDriverWait(driver, 15).until(EC.visibility_of_element_located(By.XPATH, "//div[contains(#style, 'text']"))
I am trying to produce several reports (i.e. N PPTX files) based on different inputs/for different users on the same PPTX template I created.
I have several preformatted XSLFTextShape on the PPTX template that contains a single XSLFTextParagraph already formatted (i.e. both the shape and the text). Each shape contains a particular placeholder that I need to substitute with a dynimic value. I have this value in a Map (placeholder,newValue). I am successful in updating the placeholder with the new value using:
textShape.clearText();
XSLFTextRun run = paragraph.addNewTextRun();
run.setText(newText);
So, when I produce the PPTX in output the text is updated but font color, font formatting, font size are changed compared to those I defined in the template. How can I keep the same formatting?
Any solutions to simply change the text while keeping original formatting?
Thanks in advance!
For everybody which may be interested in this topic in the future - I post the solution (working if one TextBox has a single Paragraph). This solution loops on all text boxes and in the case one contain one of the vales specified in the Placeholder->newValue map, it will update it maintaining the formatting.
public static void updateTextBoxesWithDesiredValues(XMLSlideShow ppt, Map<String, String> placeHolderDefinedValue) {
logger.info("ElapsedTime: " + tM.getTimeElapsedReadableFormat() + " ########## Updating single text box content...");
List<XSLFSlide> allSlides = ppt.getSlides();
int updatedElements = 0;
for (XSLFSlide currentSlide : allSlides) {
for (XSLFShape shape : currentSlide.getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textBox = (XSLFTextShape) shape;
String elementTextContent = textBox.getText();
for (Object key : placeHolderDefinedValue.keySet()) {
if (elementTextContent.equals(key)) {
List<XSLFTextParagraph> textBoxParagraphs = textBox.getTextParagraphs();
List<XSLFTextRun> textBoxParagraphTextRuns = textBoxParagraphs.get(0).getTextRuns();
//System.out.println("########################## check paragraph number in textbox: " + textBoxParagraphs.size() + " - TextRuns: " + textBoxParagraphs.get(0).getTextRuns().size());
logger.info("ElapsedTime: " + tM.getTimeElapsedReadableFormat() + updatedElements + ") Updating: " + textBox.getText() + " --> " + placeHolderDefinedValue.get(key));
for (XSLFTextRun r : textBoxParagraphTextRuns) {
r.setText(placeHolderDefinedValue.get(key));
}
updatedElements++;
//break;
}
}
}
}
}
logger.info("ElapsedTime: " + tM.getTimeElapsedReadableFormat() + " Total Text Element Content Updated: " + updatedElements + " #########################");
}
It's kind of horrible - but yeah there's a reason they called it "POI".
Here's my approach to "only reset text" of an existing XSLFTextShape (that must have at least some text pre-set!):
textShape.getTextParagraphs().get(0).getTextRuns().get(0).setText(text);
for (int i = 1; i < textShape.getTextParagraphs().get(0).getTextRuns().size(); i++) {
textShape.getTextParagraphs().get(0).getTextRuns().get(i).setText("");
}
for (int i = 1; i < textShape.getTextParagraphs().size(); i++) {
textShape.getTextParagraphs().get(i).getTextRuns().stream().filter(tr -> !tr.getRawText().equals("\n")).forEach(tr -> tr.setText(""));
}
It will replace all existing text(paragraphs/runs) with "empty" text, but linebreaks can't be replaced for some reason. So this might leave you with some trailing lines - as they usually(!) are transparent this won't really hurt a lot.
.clearText / removing paragraphs either destoyed the formatting for me, or didn't work. Trying to reset the style (fontColor, fontFamily, fontSize, isBold, isItalit, ...) didn't result in satisfying results :(
I am using JavaFX to produce bitmap previews of my music documents.
At certain points, within the paint process, I need to know the dimensions of a given string.
Upto now, I have used the following :
bounds = TextBuilder.create().text(string).font(m_Font).build().getLayoutBounds();
However, eclipse informs me that this is depreciated. It is so depreciated that the bounds object is empty (all set to zero).
How do I go about getting the bounds - width and height - of a single line string now ?
Please note that there are no on screen controls being used to display this stuff. Everything is generated in memory and "dumped" out to a png bitmap.
I have scavenged around on the net and have not found the answer (or I missed it entirely).
Any expert help available ?
As I mentioned in my comment, getLayoutBounds() is not deprecated and is perfectly valid to use. It's just the builder which is deprecated.
That said, I have created the following test application which produced seemingly correct output, using builders and creating objects directly:
import javafx.geometry.Bounds;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
import javafx.scene.text.Font;
public class stack extends javafx.application.Application {
public static void main(String[] args)
{
// Builder
Bounds b = TextBuilder.create().text("hello").build().getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
b = TextBuilder.create().text("heeeeello").build().getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
// No builder
b = new Text("hello").getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
b = new Text("heeeeello").getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
// With bad font, zero sized
Font my_font = new Font("i am not a font", 0);
Text text = new Text("heeeeello");
text.setFont(my_font);
b = text.getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
// With bad font, arbitrary size
my_font = new Font("i am not a font", 20);
text = new Text("heeeeello");
text.setFont(my_font);
b = text.getLayoutBounds();
System.out.println(b.getHeight() + ", " + b.getWidth());
}
#Override
public void start(Stage primaryStage) throws Exception { }
}
Output:
15.9609375, 25.91015625
15.9609375, 51.01171875
15.9609375, 25.91015625
15.9609375, 51.01171875
0.0, 0.0
26.6015625, 85.01953125
I would hypothesise that your font is screwing things up, possibly the size is set to zero or some other error.
In this example, an image of a map is set on a ScrollPane in the FXML file.
http://hg.openjdk.java.net/openjfx/8u-dev/rt/rev/36a59c629605
The image is resizable by some other method that changes the scale of the Group - zoomGroup - containing the image.
+ private void zoom(double scaleValue) {
+// System.out.println("airportapp.Controller.zoom, scaleValue: " + scaleValue);
+ double scrollH = map_scrollpane.getHvalue();
+ double scrollV = map_scrollpane.getVvalue();
+ zoomGroup.setScaleX(scaleValue);
+ zoomGroup.setScaleY(scaleValue);
+ map_scrollpane.setHvalue(scrollH);
+ map_scrollpane.setVvalue(scrollV);
+ }
But in order to make sure the ScrollPane re-computes its scrollbars (so the scrollable area effectively changes to include the entire image at any scale) the Group that wraps the Image, zoomGroup, is then wrapped in another Group, contentGroup, which is set as the ScrollPane's content.
+ // Wrap scroll content in a Group so ScrollPane re-computes scroll bars
+ Group contentGroup = new Group();
+ zoomGroup = new Group();
+ contentGroup.getChildren().add(zoomGroup);
+ zoomGroup.getChildren().add(map_scrollpane.getContent());
+ map_scrollpane.setContent(contentGroup);
Can someone explain why this is necessary?
This is the video that accompanies the files:
https://www.youtube.com/watch?v=ij0HwRAlCmo