public void addGlyph(GlyphData gv, float x, float y) { AffineTransform at = AffineTransform.getTranslateInstance(x, y); PathIterator pi = gv.gp.getPathIterator(at); float[] coords = new float[6]; while (!pi.isDone()) { int type = pi.currentSegment(coords); switch (type) { case PathIterator.SEG_MOVETO: moveTo(coords[0], coords[1]); break; case PathIterator.SEG_LINETO: lineTo(coords[0], coords[1]); break; case PathIterator.SEG_CUBICTO: curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); break; case PathIterator.SEG_CLOSE: closePath(); break; default: System.out.println("Unknown path type: " + type); break; } pi.next(); } }
public void mouseDragged(MouseEvent e) { int mods = e.getModifiersEx(); Point dragEnd = e.getPoint(); boolean shift = (mods & MouseEvent.SHIFT_DOWN_MASK) > 0; boolean ctrl = (mods & MouseEvent.CTRL_DOWN_MASK) > 0; boolean alt = shift & ctrl; ctrl = ctrl & (!alt); shift = shift & (!alt); boolean nomods = !(shift | ctrl | alt); if (dragBegin == null) dragBegin = dragEnd; nodrag = false; if ((mods & InputEvent.BUTTON3_DOWN_MASK) > 0 || true) { double dx = dragEnd.getX() - dragBegin.getX(); double dy = dragEnd.getY() - dragBegin.getY(); synchronized (JImage.this) { t.preConcatenate(AffineTransform.getTranslateInstance(dx, dy)); } dragBegin = dragEnd; repaint(); } }
void draw(Graphics2D g) { // toX/toY is tip of arrow and fx/fy is a point on the line - // fx/fy is used to determine direction & angle AffineTransform at = AffineTransform.getTranslateInstance(toX, toY); int b = 9; double theta = Math.toRadians(20); // The idea of using a GeneralPath is so we can // create the (three lines that make up the) arrow // (only) one time and then use AffineTransform to // place it anywhere we want. GeneralPath path = new GeneralPath(); // distance between line and the arrow mark <** not ** // Start a new line segment from the position of (0,0). path.moveTo(0, 0); // Create one of the two arrow head lines. int x = (int) (-b * Math.cos(theta)); int y = (int) (b * Math.sin(theta)); path.lineTo(x, y); // distance between line and the arrow mark <** not ** // Make the other arrow head line. int x2 = (int) (-b * Math.cos(-theta)); int y2 = (int) (b * Math.sin(-theta)); // path.moveTo(0,0); path.lineTo(x2, y2); path.closePath(); // theta is in radians double s, t; s = toY - fy; // calculate slopes. t = toX - fx; if (t != 0) { s = s / t; theta = Math.atan(s); if (t < 0) theta += Math.PI; } else if (s < 0) theta = -(Math.PI / 2); else theta = Math.PI / 2; at.rotate(theta); // at.rotate(theta,toX,toY); Shape shape = at.createTransformedShape(path); if (checkStatus == Status.UNCHECKED) g.setColor(Color.BLACK); else if (checkStatus == Status.COMPATIBLE) g.setColor(FOREST_GREEN); else g.setColor(ORANGE_RED); g.fill(shape); g.draw(shape); }
public synchronized void paint(Graphics gin) { Graphics2D g = (Graphics2D) gin; if (im == null) return; int height = getHeight(); int width = getWidth(); if (fit) { t = new AffineTransform(); double scale = Math.min(((double) width) / im.getWidth(), ((double) height) / im.getHeight()); // we'll re-center the transform in a moment. t.scale(scale, scale); } // if the image (in either X or Y) is smaller than the view port, then center // the image with respect to that axis. double mwidth = im.getWidth() * t.getScaleX(); double mheight = im.getHeight() * t.getScaleY(); if (mwidth < width) t.preConcatenate( AffineTransform.getTranslateInstance((width - mwidth) / 2.0 - t.getTranslateX(), 0)); if (mheight < height) t.preConcatenate( AffineTransform.getTranslateInstance(0, (height - mheight) / 2.0 - t.getTranslateY())); // if we're allowing panning (because only a portion of the image is visible), // don't allow translations that show less information that is possible. Point2D topleft = t.transform(new Point2D.Double(0, 0), null); Point2D bottomright = t.transform(new Point2D.Double(im.getWidth(), im.getHeight()), null); if (mwidth > width) { if (topleft.getX() > 0) t.preConcatenate(AffineTransform.getTranslateInstance(-topleft.getX(), 0)); if (bottomright.getX() < width) t.preConcatenate(AffineTransform.getTranslateInstance(width - bottomright.getX(), 0)); // t.translate(width-bottomright.getX(), 0); } if (mheight > height) { if (topleft.getY() > 0) t.preConcatenate(AffineTransform.getTranslateInstance(0, -topleft.getY())); if (bottomright.getY() < height) t.preConcatenate(AffineTransform.getTranslateInstance(0, height - bottomright.getY())); } g.drawImage(im, t, null); }
// 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; }
public Shape getShape() { int R = Integer.parseInt(hexcolor.substring(0, 2), 16); int G = Integer.parseInt(hexcolor.substring(2, 4), 16); int B = Integer.parseInt(hexcolor.substring(4, 6), 16); color = new Color(R, G, B); Shape geom; if (shape.equals("circle")) { geom = new Ellipse2D.Float(-0.05f * size, -0.05f * size, 1.1f * size, 1.1f * size); center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("square")) { geom = new Rectangle2D.Float(0, 0, size, size); center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("roundsquare")) { geom = new RoundRectangle2D.Float(0, 0, size, size, size / 2, size / 2); center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("triangle")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); float height = (float) (Math.sqrt(3) * size / 2); path.moveTo(size / 2, size - height); path.lineTo(size, size); path.lineTo(0, size); path.closePath(); geom = path; center = new Point2D.Float(size / 2, size - height / 2); } else if (shape.equals("star")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 2, 0); path.lineTo(7 * size / 10, 3 * size / 10); path.lineTo(size, size / 2); path.lineTo(7 * size / 10, 7 * size / 10); path.lineTo(size / 2, size); path.lineTo(3 * size / 10, 7 * size / 10); path.lineTo(0, size / 2); path.lineTo(3 * size / 10, 3 * size / 10); path.closePath(); AffineTransform trans = AffineTransform.getRotateInstance(Math.PI / 4, size / 2, size / 2); Shape shape = trans.createTransformedShape(path); Area area = new Area(path); area.add(new Area(shape)); trans = AffineTransform.getScaleInstance(1.2, 1.2); shape = trans.createTransformedShape(area); trans = AffineTransform.getTranslateInstance(-0.1 * size, -0.1 * size); shape = trans.createTransformedShape(shape); geom = shape; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("octagon")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 4, 0); path.lineTo(3 * size / 4, 0); path.lineTo(size, size / 4); path.lineTo(size, 3 * size / 4); path.lineTo(3 * size / 4, size); path.lineTo(size / 4, size); path.lineTo(0, 3 * size / 4); path.lineTo(0, size / 4); path.closePath(); geom = path; path.closePath(); geom = path; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("ellipse")) { geom = new Ellipse2D.Float(0, 0, 3 * size / 4, size); center = new Point2D.Float(3 * size / 8, size / 2); } else if (shape.equals("cross")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 4, 0); path.lineTo(3 * size / 4, 0); path.lineTo(3 * size / 4, size / 4); path.lineTo(size, size / 4); path.lineTo(size, 3 * size / 4); path.lineTo(3 * size / 4, 3 * size / 4); path.lineTo(3 * size / 4, size); path.lineTo(size / 4, size); path.lineTo(size / 4, 3 * size / 4); path.lineTo(0, 3 * size / 4); path.lineTo(0, size / 4); path.lineTo(size / 4, size / 4); path.closePath(); geom = path; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("pentagon")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 2, size); float x, y, a; for (int i = 1; i < 5; i++) { a = (float) ((2 * Math.PI / 5) * i); x = (float) (size / 2 + size / 2 * Math.sin(a)); y = (float) (size / 2 + size / 2 * Math.cos(a)); path.lineTo(x, y); } path.closePath(); AffineTransform trans = AffineTransform.getScaleInstance(1.1, 1.1); Shape shape = trans.createTransformedShape(path); trans = AffineTransform.getTranslateInstance(-0.05 * size, -0.05 * size); shape = trans.createTransformedShape(shape); geom = shape; center = new Point2D.Float(size / 2, size / 2); } else if (shape.equals("hexagon")) { GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); path.moveTo(size / 2, size); float x, y, a; for (int i = 1; i < 6; i++) { a = (float) ((2 * Math.PI / 6) * i); x = (float) (size / 2 + size / 2 * Math.sin(a)); y = (float) (size / 2 + size / 2 * Math.cos(a)); path.lineTo(x, y); } path.closePath(); geom = path; center = new Point2D.Float(size / 2, size / 2); } else { geom = new Ellipse2D.Float(0, 0, size, size); center = new Point2D.Float(size / 2, size / 2); } return geom; }