public void drawScaledImage(BufferedImage im, int x, int y, int w, int h) { float scaleX = w * 1.0f / im.getWidth(); float scaleY = h * 1.0f / im.getHeight(); AffineTransform tx = new AffineTransform(); tx.scale(scaleX, scaleY); BufferedImageOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); drawImage(im, op, x, y); }
private AffineTransform makeTransform(Object o) { AffineTransform r = (AffineTransform) ((AffineTransform) o).clone(); // System.out.println("r:"+r); if (origTransform != null) { AffineTransform r2 = (AffineTransform) origTransform.clone(); // System.out.println("r2:"+r2); r2.concatenate(r); r = r2; } return r; }
/** * synchronize frame shape and location (TextLayout only) ; this is called by syncShape(), so that * subclasser might override easily when only rectangular shapes are availables. */ protected void syncFrame() { PicText te = (PicText) element; if (!te.isFramed()) { return; } AffineTransform tr = new AffineTransform(); // maps Image coordinates to Model coordinates (see paint) tr.setToIdentity(); // reset PicPoint anchor = te.getCtrlPt(TextEditable.P_ANCHOR, ptBuf); tr.rotate(getRotation(), anchor.x, anchor.y); // rotate along P_ANCHOR ! shape = tr.createTransformedShape(te.getShapeOfFrame()); }
/** * This implementation calls <code>super.hitTest</code> and returns the result if non-null (this * should be a HitInfo.Point), then returns a HitInfo.Interior if the mouse-click occured inside * the text bound (as defined by text layout) * * @return a HitInfo corresponding to the given mouse-event */ public HitInfo hitTest(PEMouseEvent e) { // from Bitmap: if (image != null) { if (getBounds().contains(e.getPicPoint())) { return new HitInfo.Interior((PicText) element, e); } return null; } // from TextLayout: if (!getBounds().contains(e.getPicPoint())) return null; PicText te = (PicText) element; // recompute textlayout b-box, but store it in a temporary field ! Rectangle2D tb = textLayout.getBounds(); Shape text_bounds = text2ModelTr.createTransformedShape(tb); if (text_bounds.contains(e.getPicPoint())) { // [SR:pending] for the hitInfo to be reliable, getPicPoint() should first be transformed by // inverse text2ModelTr ! (especially when rotationAngle != 0) TextHitInfo thi = textLayout.hitTestChar( (float) (e.getPicPoint().x - strx), (float) (e.getPicPoint().y - stry)); // guaranteed to return a non-null thi return new HitInfo.Text((PicText) element, thi, e); } // test hit on textlayout's bounding rectangle : // else if (bounds.contains(e.getPicPoint())) return new HitInfo.Interior(element,e); return null; }
/** Synchronizes bounding box with the model ; */ protected void syncBounds() { PicText te = (PicText) element; // [pending] Il faut tenir compte de la rotation ! Rectangle2D latexBB = null; // BB relative to latex dimensions (including rotation) (without frame) Rectangle2D textLayoutBB = null; // BB relative to textLayout dimensions (including rotation) (without frame) Rectangle2D textBB = null; // BB of the text (including rotation), without frame if (areDimensionsComputed) { // compute latexBB Rectangle2D nonRotated = new Rectangle2D.Double( te.getLeftX(), te.getBottomY(), te.getWidth(), te.getHeight() + te.getDepth()); AffineTransform tr = new AffineTransform(); // maps Image coordinates to Model coordinates (see paint) tr.setToIdentity(); // reset PicPoint anchor = te.getCtrlPt(TextEditable.P_ANCHOR, ptBuf); tr.rotate(getRotation(), anchor.x, anchor.y); // rotate along P_ANCHOR ! latexBB = tr.createTransformedShape(nonRotated).getBounds2D(); } if (image == null) { // compute textLayoutBB Rectangle2D nonRotated = textLayout.getBounds(); textLayoutBB = text2ModelTr.createTransformedShape(nonRotated).getBounds2D(); } // use textLayoutBB or latexBB or their union if (image != null) textBB = latexBB; else { if (!areDimensionsComputed) textBB = textLayoutBB; else { textBB = latexBB.createUnion(textLayoutBB); } } // union with frame BB if (te.isFramed()) { super.syncBounds(); // update bounds of the frame if necessary Rectangle2D.union(super.bounds, textBB, this.bounds); } else this.bounds = textBB; }
/** * Synchronize the textLayout and the shape (=frame box, by calling syncFrame) with the model When * <code>TextLayout</code> is used, this delegates to <code>getRotation()</code> where computing * rotation angle is concerned, and updates the AffineTransform returned by <code> * getTextToModelTransform()</code>. */ protected void syncShape() { PicText te = (PicText) element; // textLayout = new TextLayout(te.getText().length()==0 ? " " : te.getText(), // textFont, // new FontRenderContext(null,false,false)); text2ModelTr.setToIdentity(); // reset PicPoint anchor = te.getCtrlPt(TextEditable.P_ANCHOR, ptBuf); text2ModelTr.rotate(getRotation(), anchor.x, anchor.y); // rotate along P_ANCHOR ! // the reference point of an image is the top-left one, but the refpoint of a text layout is on // the baseline if (image != null) { text2ModelTr.translate(te.getLeftX(), te.getTopY()); if (te.getWidth() != 0 && image.getWidth() != 0 && (te.getDepth() + te.getHeight()) != 0 && image.getHeight() != 0) text2ModelTr.scale( te.getWidth() / image.getWidth(), -(te.getHeight() + te.getDepth()) / image.getHeight()); } else { // Hack ? Just cheating a little bit ? Ou juste ruse ? // we want here to use the dimensions of the textLayout instead of latex dimensions if // areDimensionsComputed // sinon on va aligner le textlayout en fonction des parametres latex, et l'Utilisateur (qui // est bien bete) ne va rien comprendre. double latexH = 0; double latexD = 0; double latexW = 0; if (areDimensionsComputed) { // store latex dimensions, and setDimensions to textLayout ones latexH = te.getHeight(); latexD = te.getDepth(); latexW = te.getWidth(); te.setDimensions( textLayout.getBounds().getWidth(), textLayout.getAscent(), textLayout.getDescent()); } text2ModelTr.translate(te.getLeftX(), te.getBaseLineY()); if (areDimensionsComputed) { // restore latex dimensions te.setDimensions(latexW, latexH, latexD); } // Autre possibilite= comprimer le texte pour qu'il rentre dans la boite (evite le hack // ci-dessus): // text2ModelTr.scale(te.getWidth()/textLayout.getWidth(),-(te.getHeight()+te.getDepth())/textLayout.getHeight()); text2ModelTr.scale(1.0, -1.0); } syncFrame(); }
// --------------------------------------------------------- private synchronized AffineTransform bufferTransform() { AffineTransform r = new AffineTransform(); for (int i = 0; i < a1.size(); i++) { int s1 = a1.get(i); Object s2 = a2.get(i); Object s3[] = null; if (s2 instanceof Object[]) s3 = (Object[]) s2; if (s1 == setTransform) { r = makeTransform(s2); } if (s1 == shear) r.shear((Double) s3[0], (Double) s3[1]); if (s1 == rotate1) r.rotate((Double) s2); if (s1 == rotate2) r.rotate((Double) s3[0], (Double) s3[1], (Double) s3[2]); if (s1 == scale1) r.scale((Double) s3[0], (Double) s3[1]); if (s1 == translate1) r.translate((Double) s3[0], (Double) s3[1]); if (s1 == translate2) r.translate((Integer) s3[0], (Integer) s3[1]); } return r; }
// Although it presently returns a boolean, that was only needed // during my aborted attempted at animated graphics primitives. // Until those become a reality the boolean value returned by this // routine is unnecessary public boolean animate(int sAt, boolean forward) { int x; LinkedList lt = null; animation_done = true; // May be re-set in paintComponent via indirect paintImmediately call at end // Was used in aborted attempted to // introduce animated primitives. Now // it's probably excess baggage that // remains because I still have hopes // of eventually having animated // primitives if (getSize().width != 0 && getSize().height != 0) { my_width = getSize().width; // set dimensions my_height = getSize().height; } else { my_width = GaigsAV.preferred_width; // set dimensions my_height = GaigsAV.preferred_height; } // First capture the new image in a buffer called image2 SnapAt = sAt; BufferedImage image2 = new BufferedImage(my_width, my_height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) image2.getGraphics(); // need a separate object each time? g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fillRect(0, 0, my_width, my_height); // Set horizoff and vertoff to properly center the visualization in the // viewing window. This is not quite perfect because visualizations // that are not properly centered within their [0,1] localized // coordinates will not be perfectly centered, but it is much better // than it was previously. if (no_mouse_drag) { horizoff = (my_width - GaigsAV.preferred_width) / 2; vertoff = (my_height - GaigsAV.preferred_height) / 2; } list_of_snapshots.reset(); x = 0; lt = new LinkedList(); while (x < SnapAt && list_of_snapshots.hasMoreElements()) { lt = (LinkedList) list_of_snapshots.nextElement(); x++; } lt.reset(); animation_done = true; // System.out.println("before loop " + horizoff); while (lt.hasMoreElements()) { obj tempObj = (obj) lt.nextElement(); animation_done = animation_done && (tempObj.execute(g2 /*offscreen*/, zoom, vertoff, horizoff)); // System.out.println("in loop"); } // Next capture the image we are coming from in a buffer called image1 SnapAt = (forward ? sAt - 1 : sAt + 1); BufferedImage image1 = new BufferedImage(my_width, my_height, BufferedImage.TYPE_INT_RGB); Graphics2D g1 = (Graphics2D) image1.getGraphics(); // need a separate object each time? g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g1.setColor(Color.WHITE); g1.fillRect(0, 0, my_width, my_height); // Set horizoff and vertoff to properly center the visualization in the // viewing window. This is not quite perfect because visualizations // that are not properly centered within their [0,1] localized // coordinates will not be perfectly centered, but it is much better // than it was previously. if (no_mouse_drag) { horizoff = (my_width - GaigsAV.preferred_width) / 2; vertoff = (my_height - GaigsAV.preferred_height) / 2; } list_of_snapshots.reset(); x = 0; lt = new LinkedList(); while (x < SnapAt && list_of_snapshots.hasMoreElements()) { lt = (LinkedList) list_of_snapshots.nextElement(); x++; } lt.reset(); animation_done = true; // System.out.println("before loop " + horizoff); while (lt.hasMoreElements()) { obj tempObj = (obj) lt.nextElement(); animation_done = animation_done && (tempObj.execute(g1 /*offscreen*/, zoom, vertoff, horizoff)); // System.out.println("in loop"); } // Now slide from image1 to image2 // From the gaff Visualizer by Chris Gaffney // double step = 4; // Adjust this for more/less granularity between images double step = 40; // Adjust this for more/less granularity between images Image buffer = getGraphicsConfiguration().createCompatibleVolatileImage(my_width, my_height); Graphics2D g2d = (Graphics2D) buffer.getGraphics(); AffineTransform trans = AffineTransform.getTranslateInstance(step * (forward ? -1 : 1), 0); // AffineTransform orig = g2d.getTransform(); Shape mask = createMask(my_width, my_height); for (double i = 0; i < my_width; i += step) { if (i + step > my_width) // last time through loop, so adjust transform trans = AffineTransform.getTranslateInstance(((double) (my_width - i)) * (forward ? -1 : 1), 0); g2d.transform(trans); g2d.drawImage(image1, 0, 0, this); g2d.setColor(Color.BLACK); g2d.fill(mask); AffineTransform last = g2d.getTransform(); g2d.transform(AffineTransform.getTranslateInstance(my_width * (-1 * (forward ? -1 : 1)), 0)); g2d.drawImage(image2, 0, 0, this); g2d.setColor(Color.BLACK); g2d.fill(mask); g2d.setTransform(last); this.my_image = buffer; repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { } } Image b = getGraphicsConfiguration().createCompatibleImage(my_width, my_height); b.getGraphics().drawImage(buffer, 0, 0, null); this.my_image = b; return animation_done; }
// Thread's run method aimed at creating a bitmap asynchronously public void run() { Drawing drawing = new Drawing(); PicText rawPicText = new PicText(); String s = ((PicText) element).getText(); rawPicText.setText(s); drawing.add( rawPicText); // bug fix: we must add a CLONE of the PicText, otherwise it loses it former // parent... (then pb with the view ) drawing.setNotparsedCommands( "\\newlength{\\jpicwidth}\\settowidth{\\jpicwidth}{" + s + "}" + CR_LF + "\\newlength{\\jpicheight}\\settoheight{\\jpicheight}{" + s + "}" + CR_LF + "\\newlength{\\jpicdepth}\\settodepth{\\jpicdepth}{" + s + "}" + CR_LF + "\\typeout{JPICEDT INFO: \\the\\jpicwidth, \\the\\jpicheight, \\the\\jpicdepth }" + CR_LF); RunExternalCommand.Command commandToRun = RunExternalCommand.Command.BITMAP_CREATION; // RunExternalCommand command = new RunExternalCommand(drawing, contentType,commandToRun); boolean isWriteTmpTeXfile = true; String bitmapExt = "png"; // [pending] preferences String cmdLine = "{i}/unix/tetex/create_bitmap.sh {p} {f} " + bitmapExt + " " + fileDPI; // [pending] preferences ContentType contentType = getContainer().getContentType(); RunExternalCommand.isGUI = false; // System.out, no dialog box // [pending] debug RunExternalCommand command = new RunExternalCommand(drawing, contentType, cmdLine, isWriteTmpTeXfile); command .run(); // synchronous in an async. thread => it's ok (anyway, we must way until the LaTeX // process has completed) if (wantToComputeLatexDimensions) { // load size of text: try { File logFile = new File(command.getTmpPath(), command.getTmpFilePrefix() + ".log"); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(logFile)); } catch (FileNotFoundException fnfe) { System.out.println("Cannot find log file! " + fnfe.getMessage()); System.out.println(logFile); } catch (IOException ioex) { System.out.println("Log file IO exception"); ioex.printStackTrace(); } // utile ? System.out.println("Log file created! file=" + logFile); getDimensionsFromLogFile(reader, (PicText) element); syncStringLocation(); // update dimensions syncBounds(); syncFrame(); SwingUtilities.invokeLater( new Thread() { public void run() { repaint(null); } }); // repaint(null); // now that dimensions are available, we force a repaint() [pending] // smart-repaint ? } catch (Exception e) { e.printStackTrace(); } } if (wantToGetBitMap) { // load image: try { File bitmapFile = new File(command.getTmpPath(), command.getTmpFilePrefix() + "." + bitmapExt); this.image = ImageIO.read(bitmapFile); System.out.println( "Bitmap created! file=" + bitmapFile + ", width=" + image.getWidth() + "pixels, height=" + image.getHeight() + "pixels"); if (image == null) return; syncStringLocation(); // sets strx, stry, and dimensions of text syncBounds(); // update the AffineTransform that will be applied to the bitmap before displaying on screen PicText te = (PicText) element; text2ModelTr.setToIdentity(); // reset PicPoint anchor = te.getCtrlPt(TextEditable.P_ANCHOR, ptBuf); text2ModelTr.rotate(getRotation(), anchor.x, anchor.y); // rotate along P_ANCHOR ! text2ModelTr.translate(te.getLeftX(), te.getTopY()); text2ModelTr.scale( te.getWidth() / image.getWidth(), -(te.getHeight() + te.getDepth()) / image.getHeight()); // [pending] should do something special to avoid dividing by 0 or setting a rescaling // factor to 0 [non invertible matrix] (java will throw an exception) syncFrame(); SwingUtilities.invokeLater( new Thread() { public void run() { repaint(null); } }); // repaint(null); // now that bitmap is available, we force a repaint() [pending] // smart-repaint ? } catch (Exception e) { e.printStackTrace(); } } }
public void drawScaledImage(BufferedImage im, int x, int y, float scale) { AffineTransform tx = new AffineTransform(); tx.scale(scale, scale); BufferedImageOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); drawImage(im, op, x, y); }