/** * Write the given text string in the current font, left-aligned at (x, y). * * @param x the x-coordinate of the text * @param y the y-coordinate of the text * @param s the text */ public static void textLeft(double x, double y, String s) { offscreen.setFont(font); FontMetrics metrics = offscreen.getFontMetrics(); double xs = scaleX(x); double ys = scaleY(y); int hs = metrics.getDescent(); offscreen.drawString(s, (float) (xs), (float) (ys + hs)); draw(); }
/** * Write the given text string in the current font, centered on (x, y) and rotated by the * specified number of degrees * * @param x the center x-coordinate of the text * @param y the center y-coordinate of the text * @param s the text * @param degrees is the number of degrees to rotate counterclockwise */ public static void text(double x, double y, String s, double degrees) { double xs = scaleX(x); double ys = scaleY(y); offscreen.rotate(Math.toRadians(-degrees), xs, ys); text(x, y, s); offscreen.rotate(Math.toRadians(+degrees), xs, ys); }
protected void drawImageMosaic(Graphics2D g2) { // Break the image up into tiles. Draw each // tile with its own transparency, allowing // the background to show through to varying // degrees. int side = 36; int width = mImage.getWidth(); int height = mImage.getHeight(); for (int y = 0; y < height; y += side) { for (int x = 0; x < width; x += side) { // Calculate an appropriate transparency value. float xBias = (float) x / (float) width; float yBias = (float) y / (float) height; float alpha = 1.0f - Math.abs(xBias - yBias); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); // Draw the subimage. int w = Math.min(side, width - x); int h = Math.min(side, height - y); BufferedImage tile = mImage.getSubimage(x, y, w, h); g2.drawImage(tile, x, y, null); } } // Reset the composite. g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); }
/** * Returns the specified image as icon. * * @param name name of icon * @return icon */ public static ImageIcon icon(final String name) { ImageIcon ii = ICONS.get(name); if (ii != null) return ii; Image img; if (GUIConstants.scale > 1) { // choose large image or none final URL url = GUIConstants.large() ? BaseXImages.class.getResource("/img/" + name + "_32.png") : null; if (url == null) { // resize low-res image if no hi-res image exists img = get(url(name)); final int w = (int) (img.getWidth(null) * GUIConstants.scale); final int h = (int) (img.getHeight(null) * GUIConstants.scale); final BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); final Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.drawImage(img, 0, 0, w, h, null); g2.dispose(); img = tmp; } else { img = get(url); } } else { img = get(name); } ii = new ImageIcon(img); ICONS.put(name, ii); return ii; }
// initialize default image private BufferedImage getDefaultImage(int w, int h) { BufferedImage defaultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = defaultImage.createGraphics(); graphics2D.setColor(new Color(200, 200, 200)); graphics2D.fillRect(0, 0, w, h); graphics2D.setColor(new Color(130, 130, 130)); graphics2D.drawRect(0, 0, w - 1, h - 1); return defaultImage; }
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Turn on antialiasing. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); drawBackground(g2); drawImageMosaic(g2); drawText(g2); }
public void paint(Graphics g) { gRef = (Graphics2D) g; // change size of font gRef.setFont(gRef.getFont().deriveFont(9.0f)); fmRef = g.getFontMetrics(); // Clear background if (Preferences.monochrome) { gRef.setColor(Preferences.whiteColor); } else { gRef.setColor(Preferences.backgroundColor); } gRef.fillRect(0, 0, getWidth(), getHeight()); // set colour to correct drawing colour if (Preferences.monochrome) { gRef.setColor(Preferences.blackColor); } else { gRef.setColor(Preferences.penColor); } gRef.translate(0, margin); // Call c code to draw tree gRef.scale(scale, scale); nativeDrawTree(); }
public void setPenColour(int colour) { if (Preferences.monochrome) { gRef.setColor(Preferences.blackColor); } else { if (colour == 0) { gRef.setColor(Preferences.penColor); } if (colour == 1) { gRef.setColor(Preferences.highlightColor); } } }
public int print(Graphics g,PageFormat pf,int pageIndex) { if (pageIndex == 0) { Graphics2D g2d= (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.setColor(Color.black); g2d.drawString("example string", 250, 250); g2d.fillRect(0, 0, 200, 200); return Printable.PAGE_EXISTS; } else { return Printable.NO_SUCH_PAGE; } }
/** * Gets a buffered image. * * @return the image */ public BufferedImage getBufferedImage() { if (image == null && isAnImage) { Image im = getImage(); if (im == null) { isAnImage = false; } else { image = new BufferedImage(im.getWidth(null), im.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.drawImage(im, 0, 0, null); } } return image; }
/* Draws the ball at its current position */ public void draw(Graphics2D g) { double imageWidth = img.getWidth(parent); double imageHeight = img.getHeight(parent); // translate image such that its centred on its actual position objectTransform.setToTranslation( (double) pos_x - imageWidth / 2, (double) pos_y - imageHeight / 2); // // this rotates the planet! // objectTransform.rotate(Math.PI/800,50.0,51.0); g.setTransform(new AffineTransform()); g.drawImage(img, objectTransform, parent); }
/** * Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees * * @param x the center x-coordinate of the image * @param y the center y-coordinate of the image * @param s the name of the image/picture, e.g., "ball.gif" * @param degrees is the number of degrees to rotate counterclockwise * @throws RuntimeException if the image is corrupt */ public static void picture(double x, double y, String s, double degrees) { Image image = getImage(s); double xs = scaleX(x); double ys = scaleY(y); int ws = image.getWidth(null); int hs = image.getHeight(null); if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt"); offscreen.rotate(Math.toRadians(-degrees), xs, ys); offscreen.drawImage( image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null); offscreen.rotate(Math.toRadians(+degrees), xs, ys); draw(); }
protected void drawString(Graphics2D g2, double x, double y, double theta) { // Transform to the requested location. g2.translate(x, y); // Rotate by the requested angle. g2.rotate(theta); // Draw the first part of the string. String first = mMessage.substring(0, mSplit); float width = drawBoxedString(g2, first, Color.white, Color.red, 0); // Draw the second part of the string. String second = mMessage.substring(mSplit); drawBoxedString(g2, second, Color.blue, Color.white, width); // Undo the transformations. g2.rotate(-theta); g2.translate(-x, -y); }
private void drawString(String str, int x, int y) { // text does not work correctly for very small scales. if (textEnabled && scale > 0.05) { gRef.drawString(str, x, y); } }
private void fillPolyline(int x[], int y[], int length, int symbolType) { if (!Preferences.monochrome) { setFillColor(symbolType); gRef.fillPolygon(x, y, length); restoreColor(); } }
private void fillRectangle(int x, int y, int width, int height, int symbolType) { if (!Preferences.monochrome) { setFillColor(symbolType); gRef.fillRect(x, y, width, height); restoreColor(); } }
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; AffineTransform trans = new AffineTransform(); int width = getSize().width; int height = getSize().height; g2d.setColor(Color.BLACK); g2d.fillRect(0, 0, width, height); trans.setTransform(identity); trans.scale(scale, scale); g2d.drawImage(image, trans, this); }
private void fillArc( int x, int y, int width, int height, int startAngle, int arcAngle, int symbolType) { if (!Preferences.monochrome) { setFillColor(symbolType); gRef.fillArc(x, y, width, height, startAngle, arcAngle); restoreColor(); } }
protected void drawBackground(Graphics2D g2) { // Draw circles of different colors. int side = 45; int width = getSize().width; int height = getSize().height; Color[] colors = { Color.yellow, Color.cyan, Color.orange, Color.pink, Color.magenta, Color.lightGray }; for (int y = 0; y < height; y += side) { for (int x = 0; x < width; x += side) { Ellipse2D ellipse = new Ellipse2D.Float(x, y, side, side); int index = (x + y) / side % colors.length; g2.setPaint(colors[index]); g2.fill(ellipse); } } }
/** * Set the radius of the pen to the given size. * * @param r the radius of the pen * @throws RuntimeException if r is negative */ public static void setPenRadius(double r) { if (r < 0) throw new RuntimeException("pen radius must be positive"); penRadius = r * DEFAULT_SIZE; BasicStroke stroke = new BasicStroke((float) penRadius, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); // BasicStroke stroke = new BasicStroke((float) penRadius); offscreen.setStroke(stroke); }
/** * Draw a filled polygon with the given (x[i], y[i]) coordinates. * * @param x an array of all the x-coordindates of the polygon * @param y an array of all the y-coordindates of the polygon */ public static void filledPolygon(double[] x, double[] y) { int N = x.length; GeneralPath path = new GeneralPath(); path.moveTo((float) scaleX(x[0]), (float) scaleY(y[0])); for (int i = 0; i < N; i++) path.lineTo((float) scaleX(x[i]), (float) scaleY(y[i])); path.closePath(); offscreen.fill(path); draw(); }
/** * Set the radius of the pen to the given size. * * @param r the radius of the pen * @throws IllegalArgumentException if r is negative */ public static void setPenRadius(double r) { if (r < 0) throw new IllegalArgumentException("pen radius must be nonnegative"); penRadius = r; float scaledPenRadius = (float) (r * DEFAULT_SIZE); BasicStroke stroke = new BasicStroke(scaledPenRadius, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); // BasicStroke stroke = new BasicStroke(scaledPenRadius); offscreen.setStroke(stroke); }
/** * Draw a circle of radius r, centered on (x, y). * * @param x the x-coordinate of the center of the circle * @param y the y-coordinate of the center of the circle * @param r the radius of the circle * @throws IllegalArgumentException if the radius of the circle is negative */ public static void circle(double x, double y, double r) { if (r < 0) throw new IllegalArgumentException("circle radius must be nonnegative"); double xs = scaleX(x); double ys = scaleY(y); double ws = factorX(2 * r); double hs = factorY(2 * r); if (ws <= 1 && hs <= 1) pixel(x, y); else offscreen.draw(new Ellipse2D.Double(xs - ws / 2, ys - hs / 2, ws, hs)); draw(); }
public JGImage rotateAny(double angle) { JGPoint size = getSize(); int sw = size.x; int sh = size.y; // destination size is upper bound size. Upper bound is the max // of the longest dimension and the figure's dimension at 45 degrees // = sw*sin(45)+sh*cos(45) ~= 1.5*(sw+sh) int dw = (int) Math.max(Math.max(sw, sh), 0.75 * (sw + sh)); int dh = dw; int xtrans = (dw - sw) / 2; int ytrans = (dh - sh) / 2; BufferedImage dst = createCompatibleImage(dw, dh, Transparency.BITMASK); Graphics2D g = (Graphics2D) dst.getGraphics(); AffineTransform tt = AffineTransform.getTranslateInstance(xtrans, ytrans); AffineTransform tr = AffineTransform.getRotateInstance(angle, sw / 2, sh / 2); tt.concatenate(tr); g.drawImage(img, tt, null); return new JREImage(dst); }
/** * Draw a filled square of side length 2r, centered on (x, y). * * @param x the x-coordinate of the center of the square * @param y the y-coordinate of the center of the square * @param r radius is half the length of any side of the square * @throws RuntimeException if r is negative */ public static void filledSquare(double x, double y, double r) { if (r < 0) throw new RuntimeException("square side length can't be negative"); double xs = scaleX(x); double ys = scaleY(y); double ws = factorX(2 * r); double hs = factorY(2 * r); if (ws <= 1 && hs <= 1) pixel(x, y); else offscreen.fill(new Rectangle2D.Double(xs - ws / 2, ys - hs / 2, ws, hs)); draw(); }
/** * Draw filled circle of radius r, centered on (x, y). * * @param x the x-coordinate of the center of the circle * @param y the y-coordinate of the center of the circle * @param r the radius of the circle * @throws RuntimeException if the radius of the circle is negative */ public static void filledCircle(double x, double y, double r) { if (r < 0) throw new RuntimeException("circle radius can't be negative"); double xs = scaleX(x); double ys = scaleY(y); double ws = factorX(2 * r); double hs = factorY(2 * r); if (ws <= 1 && hs <= 1) pixel(x, y); else offscreen.fill(new Ellipse2D.Double(xs - ws / 2, ys - hs / 2, ws, hs)); draw(); }
/** * Draw a square of side length 2r, centered on (x, y). * * @param x the x-coordinate of the center of the square * @param y the y-coordinate of the center of the square * @param r radius is half the length of any side of the square * @throws IllegalArgumentException if r is negative */ public static void square(double x, double y, double r) { if (r < 0) throw new IllegalArgumentException("square side length must be nonnegative"); double xs = scaleX(x); double ys = scaleY(y); double ws = factorX(2 * r); double hs = factorY(2 * r); if (ws <= 1 && hs <= 1) pixel(x, y); else offscreen.draw(new Rectangle2D.Double(xs - ws / 2, ys - hs / 2, ws, hs)); draw(); }
/** * Draw a filled rectangle of given half width and half height, centered on (x, y). * * @param x the x-coordinate of the center of the rectangle * @param y the y-coordinate of the center of the rectangle * @param halfWidth is half the width of the rectangle * @param halfHeight is half the height of the rectangle * @throws RuntimeException if halfWidth or halfHeight is negative */ public static void filledRectangle(double x, double y, double halfWidth, double halfHeight) { if (halfWidth < 0) throw new RuntimeException("half width can't be negative"); if (halfHeight < 0) throw new RuntimeException("half height can't be negative"); double xs = scaleX(x); double ys = scaleY(y); double ws = factorX(2 * halfWidth); double hs = factorY(2 * halfHeight); if (ws <= 1 && hs <= 1) pixel(x, y); else offscreen.fill(new Rectangle2D.Double(xs - ws / 2, ys - hs / 2, ws, hs)); draw(); }
/** * Draw an ellipse with given semimajor and semiminor axes, centered on (x, y). * * @param x the x-coordinate of the center of the ellipse * @param y the y-coordinate of the center of the ellipse * @param semiMajorAxis is the semimajor axis of the ellipse * @param semiMinorAxis is the semiminor axis of the ellipse * @throws RuntimeException if either of the axes are negative */ public static void filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis) { if (semiMajorAxis < 0) throw new RuntimeException("ellipse semimajor axis can't be negative"); if (semiMinorAxis < 0) throw new RuntimeException("ellipse semiminor axis can't be negative"); double xs = scaleX(x); double ys = scaleY(y); double ws = factorX(2 * semiMajorAxis); double hs = factorY(2 * semiMinorAxis); if (ws <= 1 && hs <= 1) pixel(x, y); else offscreen.fill(new Ellipse2D.Double(xs - ws / 2, ys - hs / 2, ws, hs)); draw(); }
/** * Draw a point at (x, y). * * @param x the x-coordinate of the point * @param y the y-coordinate of the point */ public static void point(double x, double y) { double xs = scaleX(x); double ys = scaleY(y); double r = penRadius; // double ws = factorX(2*r); // double hs = factorY(2*r); // if (ws <= 1 && hs <= 1) pixel(x, y); if (r <= 1) pixel(x, y); else offscreen.fill(new Ellipse2D.Double(xs - r / 2, ys - r / 2, r, r)); draw(); }