+ final Image img = chap.getParagraphs().get(i).getContentImage();
+
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ // Wait until size computations are correct
+ while (!scroll.isValid()) {
+ try {
+ Thread.sleep(1);
+ } catch (InterruptedException e) {
+ }
+ }
+
+ if (img == null) {
+ setText("Error: cannot render image.");
+ } else {
+ setImage(img);
+ }
+ }
+ }).start();
+ }
+
+ /**
+ * Actually set the image in the UI.
+ * <p>
+ * Do <b>NOT</b> use this method from the UI thread.
+ *
+ * @param img
+ * the image to set
+ */
+ private void setImage(Image img) {
+ try {
+ int scrollWidth = scroll.getWidth()
+ - scroll.getVerticalScrollBar().getWidth();
+
+ BufferedImage buffImg = ImageUtilsAwt.fromImage(img);
+
+ int iw = buffImg.getWidth();
+ int ih = buffImg.getHeight();
+ double ratio = ((double) ih) / iw;
+
+ int w = scrollWidth;
+ int h = (int) (ratio * scrollWidth);
+
+ BufferedImage resizedImage = new BufferedImage(w, h,
+ BufferedImage.TYPE_4BYTE_ABGR);
+
+ Graphics2D g = resizedImage.createGraphics();