List of usage examples for java.awt Graphics2D setTransform
public abstract void setTransform(AffineTransform Tx);
From source file:tufts.vue.LWComponent.java
/** * Useful for drawing drag images into an existing graphics buffer, or drawing exportable images. * * @param alpha 0.0 (invisible) to 1.0 (no alpha -- completely opaque) * @param maxSize max dimensions for image. May be null. Image may be smaller than maxSize. * @param fillColor -- if non-null, will be rendered as background for image. * @param zoomRequest -- desired zoom; ignored if maxSize is non-null * also set, background fill will have transparency of alpha^3 to enhance contrast. *///from w w w. j ava 2s.co m public void drawImage(Graphics2D g, double alpha, Dimension maxSize, Color fillColor, double zoomRequest) { //if (DEBUG.IMAGE) out("drawImage; size " + maxSize); final boolean drawBorder = false;// this instanceof LWMap; // hack for dragged images of LWMaps final Rectangle2D.Float bounds = getImageBounds(); final Rectangle clip = g.getClipBounds(); final Size fillSize = new Size(bounds); final double zoom = computeZoomAndSize(bounds, maxSize, zoomRequest, fillSize); if (DEBUG.IMAGE) out(TERM_GREEN + "drawImage:" + "\n\t mapBounds: " + fmt(bounds) + "\n\t fill: " + fillColor + "\n\t maxSize: " + maxSize + "\n\t zoomRequest: " + zoomRequest + "\n\t fitZoom: " + zoom + "\n\t fillSize: " + fillSize + "\n\t gc: " + g + "\n\t clip: " + fmt(clip) + "\n\t alpha: " + alpha + TERM_CLEAR); final int width = fillSize.pixelWidth(); final int height = fillSize.pixelHeight(); final DrawContext dc = new DrawContext(g, this); dc.setInteractive(false); if (alpha == OPAQUE) { dc.setPrintQuality(); } else { // if alpha, assume drag image (todo: better specified as an argument) dc.setDraftQuality(); } dc.setBackgroundFill(getRenderFillColor(null)); // sure we want null here? dc.setClipOptimized(false); // always draw all children -- don't bother to check bounds if (DEBUG.IMAGE) out(TERM_GREEN + "drawImage: " + dc + TERM_CLEAR); if (fillColor != null) { // if (false && alpha != OPAQUE) { // Color c = fillColor; // // if we have an alpha and a fill, amplify the alpha on the background fill // // by changing the fill to one that has alpha*alpha, for a total of // // alpha*alpha*alpha given our GC already has an alpha set. // fillColor = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (alpha*alpha*255+0.5)); // } if (alpha != OPAQUE) dc.setAlpha(alpha, AlphaComposite.SRC); // erase any underlying in cache if (DEBUG.IMAGE) out("drawImage: fill=" + fillColor); g.setColor(fillColor); g.fillRect(0, 0, width, height); } else { //if (alpha != OPAQUE) { // we didn't have a fill, but we have an alpha: make sure any cached data is cleared // todo?: if fill is null, we need to clear as well -- it means we have implied alpha on any non-drawn bits // TODO: if this is a selection drag, we usually want to fill with the map color (or ideally, the color // of the common parent, e.g., a slide, if there's one common parent) dc.g.setComposite(AlphaComposite.Clear); g.fillRect(0, 0, width, height); } //if (alpha != OPAQUE) dc.setAlpha(alpha, AlphaComposite.SRC); if (DEBUG.IMAGE && DEBUG.META) { // Fill the entire imageable area g.setColor(Color.green); g.fillRect(0, 0, Short.MAX_VALUE, Short.MAX_VALUE); } final AffineTransform rawTransform = g.getTransform(); if (zoom != 1.0) dc.g.scale(zoom, zoom); // translate so that the upper left corner of the map region // we're drawing is at 0,0 on the underlying image g.translate(-bounds.getX(), -bounds.getY()); // GC *must* have a bounds set or we get NPE's in JComponent (textBox) rendering dc.setMasterClip(bounds); if (DEBUG.IMAGE && DEBUG.META) { // fill the clipped area so we can check our clip bounds dc.g.setColor(Color.red); dc.g.fillRect(-Short.MAX_VALUE / 2, -Short.MAX_VALUE / 2, // larger values than this can blow out internal GC code and we get nothing Short.MAX_VALUE, Short.MAX_VALUE); } if (this instanceof LWImage) { // for some reason, raw images don't seem to want to draw unless we fill first dc.g.setColor(Color.white); dc.g.fill(bounds); } // render to the image through the DrawContext/GC pointing to it draw(dc); if (drawBorder) { g.setTransform(rawTransform); //g.setColor(Color.red); //g.fillRect(0,0, Short.MAX_VALUE, Short.MAX_VALUE); if (DEBUG.IMAGE) { g.setColor(Color.black); dc.setAntiAlias(false); } else g.setColor(Color.darkGray); g.drawRect(0, 0, width - 1, height - 1); } if (DEBUG.IMAGE) out(TERM_GREEN + "drawImage: completed\n" + TERM_CLEAR); }
From source file:uk.ac.babraham.BamQC.Graphs.ScatterGraph.java
@Override protected void paintComponent(Graphics g) { g.setColor(Color.WHITE);//from w ww .jav a2 s .com g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.BLACK); if (g instanceof Graphics2D) { ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } double yStart, xStart; if (minY % yInterval == 0) { yStart = minY; } else { yStart = yInterval * (((int) minY / yInterval) + 1); } if (minX % xInterval == 0) { xStart = minX; } else { xStart = xInterval * (((int) minX / xInterval) + 1); } int xOffset = 0; // Draw the yLabel on the left of the yAxis int yLabelRightShift = 12; if (yLabel == null || yLabel.isEmpty()) { yLabelRightShift = 0; } else { if (g instanceof Graphics2D) { Graphics2D g2 = (Graphics2D) g; AffineTransform orig = g2.getTransform(); g2.rotate(-Math.PI / 2); g2.setColor(Color.BLACK); g2.drawString(yLabel, -getY(-yInterval) / 2 - (g.getFontMetrics().stringWidth(yLabel) / 2), yLabelRightShift); g2.setTransform(orig); } } // Draw the y axis labels int lastYLabelEnd = Integer.MAX_VALUE; for (double i = yStart; i <= maxY; i += yInterval) { String label = "" + i; label = label.replaceAll(".0$", ""); // Don't leave trailing .0s where we don't need them. // Calculate the new xOffset depending on the widest ylabel. int width = g.getFontMetrics().stringWidth(label); if (width > xOffset) { xOffset = width; } // place the y axis labels so that they don't overlap when the plot is resized. int baseNumberHeight = g.getFontMetrics().getHeight(); int baseNumberPosition = getY(i) + (baseNumberHeight / 2); if (baseNumberPosition + baseNumberHeight < lastYLabelEnd) { // Draw the y axis labels g.drawString(label, yLabelRightShift + 6, baseNumberPosition); lastYLabelEnd = baseNumberPosition + 2; } } // Give the x axis a bit of breathing space xOffset = xOffset + yLabelRightShift + 8; // Now draw horizontal lines across from the y axis g.setColor(new Color(180, 180, 180)); for (double i = yStart; i <= maxY; i += yInterval) { g.drawLine(xOffset, getY(i), getWidth() - 10, getY(i)); } g.setColor(Color.BLACK); // Draw the graph title int titleWidth = g.getFontMetrics().stringWidth(graphTitle); g.drawString(graphTitle, (xOffset + ((getWidth() - (xOffset + 10)) / 2)) - (titleWidth / 2), 30); // Draw the xLabel under the xAxis g.drawString(xLabel, (getWidth() / 2) - (g.getFontMetrics().stringWidth(xLabel) / 2), getHeight() - 5); // Now draw the data points double baseWidth = (getWidth() - (xOffset + 10)) / (maxX - minX); // System.out.println("Base Width is "+baseWidth); // Let's find the longest label, and then work out how often we can draw labels int lastXLabelEnd = 0; // Draw the x axis labels for (double i = xStart; i <= maxX; i += xInterval) { g.setColor(Color.BLACK); String baseNumber = "" + i; baseNumber = baseNumber.replaceAll(".0$", ""); // Don't leave trailing .0s where we don't need them. // Calculate the new xOffset depending on the widest ylabel. int baseNumberWidth = g.getFontMetrics().stringWidth(baseNumber); int baseNumberPosition = (int) (xOffset + (baseWidth * i) - (baseNumberWidth / 2)); if (baseNumberPosition > lastXLabelEnd) { g.drawString(baseNumber, baseNumberPosition, getHeight() - 25); lastXLabelEnd = baseNumberPosition + baseNumberWidth + 5; } // Now draw vertical lines across from the y axis g.setColor(new Color(180, 180, 180)); g.drawLine((int) (xOffset + (baseWidth * i)), getHeight() - 40, (int) (xOffset + (baseWidth * i)), 40); g.setColor(Color.BLACK); } // Now draw the axes g.drawLine(xOffset, getHeight() - 40, getWidth() - 10, getHeight() - 40); g.drawLine(xOffset, getHeight() - 40, xOffset, 40); // Initialise the arrays containing the tooltips rectangles = new ArrayList<Rectangle>(); tips = new ArrayList<String>(); g.setColor(Color.BLUE); // Draw the data points double ovalSize = 5; // We distinguish two inputs since the x label does not start from 0. // used for computing the actual line points as if they were starting from 0. double[] inputVar = new double[data.length]; double[] responseVar = new double[data.length]; for (int d = 0; d < data.length; d++) { double x = getX(xCategories[d], xOffset) - ovalSize / 2; double y = getY(data[d]) - ovalSize / 2; g.fillOval((int) x, (int) y, (int) (ovalSize), (int) (ovalSize)); g.drawString(toolTipLabels[d], (int) x + 2, (int) y + 16); inputVar[d] = Double.valueOf(xCategories[d]); responseVar[d] = data[d]; // Tool tips Rectangle r = new Rectangle((int) x, (int) y, (int) (ovalSize), (int) (ovalSize)); rectangles.add(r); tips.add(toolTipLabels[d]); } g.setColor(Color.BLACK); // Draw the intercept // WARNING: Is drawing a least squares regression line asserting that "the distribution follows a power law" correct? // This is our case if we plot log-log.. // It seems not in this paper (Appendix A) http://arxiv.org/pdf/0706.1062v2.pdf if (data.length > 1) { LinearRegression linReg = new LinearRegression(inputVar, responseVar); double intercept = linReg.intercept(); double slope = linReg.slope(); double rSquare = linReg.R2(); // Let's now calculate the two points (x1, y1) and (xn, yn) // (x1, y1). We need to skip the areas where x1<minY and y1>maxY double x1 = minX; double y1 = slope * minX + intercept; if (y1 < minY) { x1 = (minY - intercept) / slope; y1 = minY; } else if (y1 > maxY) { x1 = (maxY - intercept) / slope; y1 = maxY; } // (xn, yn). maxX which essentially is inputVar[inputVar.length-1] double xn = maxX; double yn = slope * maxX + intercept; if (g instanceof Graphics2D) { ((Graphics2D) g).setStroke(new BasicStroke(1.5f)); } g.setColor(Color.RED); g.drawLine(getX(x1, xOffset), getY(y1), getX(xn, xOffset), getY(yn)); g.setColor(Color.BLACK); if (g instanceof Graphics2D) { ((Graphics2D) g).setStroke(new BasicStroke(1)); } // Draw the legend for the intercept String legendString = "y = " + Precision.round(slope, 3) + "x"; if (intercept < 0) legendString += " - " + Precision.round(-intercept, 3); else legendString += " + " + Precision.round(intercept, 3); int width = g.getFontMetrics().stringWidth(legendString); // First draw a box to put the legend in g.setColor(Color.WHITE); g.fillRect(xOffset + 10, 45, width + 8, 35); g.setColor(Color.LIGHT_GRAY); g.drawRect(xOffset + 10, 45, width + 8, 35); // Now draw the legend label g.setColor(Color.RED); g.drawString(legendString, xOffset + 13, 60); g.drawString("R^2 = " + Precision.round(rSquare, 3), xOffset + 13, 76); g.setColor(Color.BLACK); } }
From source file:util.ui.UiUtilities.java
/** * Scales Icons to a specific size/* w ww . j a v a 2 s. com*/ * * @param icon * Icon that should be scaled * @param width * scaled width * @param height * scaled height * @return Scaled Icon */ public static Icon scaleIcon(Icon icon, int width, int height) { if (icon == null) { return null; } int currentWidth = icon.getIconWidth(); int currentHeight = icon.getIconHeight(); if ((currentWidth == width) && (currentHeight == height)) { return icon; } try { // Create Image with Icon BufferedImage iconImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = iconImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); AffineTransform z = g2.getTransform(); g2.setTransform(z); icon.paintIcon(null, g2, 0, 0); g2.dispose(); BufferedImage scaled = scaleDown(iconImage, width, height); // Return new Icon return new ImageIcon(scaled); } catch (Exception ex) { ex.printStackTrace(); } return icon; }
From source file:VASSAL.counters.Labeler.java
public void draw(Graphics g, int x, int y, Component obs, double zoom) { updateCachedImage();/*from w w w . jav a 2 s . com*/ piece.draw(g, x, y, obs, zoom); // FIXME: We should be drawing the text at the right size, not scaling it! final Point p = getLabelPosition(); final int labelX = x + (int) (zoom * p.x); final int labelY = y + (int) (zoom * p.y); AffineTransform saveXForm = null; final Graphics2D g2d = (Graphics2D) g; if (rotateDegrees != 0) { saveXForm = g2d.getTransform(); final AffineTransform newXForm = AffineTransform.getRotateInstance(Math.toRadians(rotateDegrees), x, y); g2d.transform(newXForm); } imagePainter.draw(g, labelX, labelY, zoom, obs); if (rotateDegrees != 0) { g2d.setTransform(saveXForm); } }
From source file:view.BackgroundImageController.java
public void setGraphBackgroundImage(final VisualizationViewer vv, final ImageIcon background, final double xOffset, final double yOffset) { removeBackgroundImage(vv);// ww w . ja va 2s . co m preRender = new VisualizationViewer.Paintable() { public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; AffineTransform oldXform = g2d.getTransform(); AffineTransform lat = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT) .getTransform(); AffineTransform vat = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW) .getTransform(); AffineTransform at = new AffineTransform(); at.concatenate(g2d.getTransform()); at.concatenate(vat); at.concatenate(lat); g2d.setTransform(at); g.drawImage(background.getImage(), (int) Math.round(-background.getIconWidth() / xOffset), (int) Math.round(-background.getIconHeight() / yOffset), background.getIconWidth(), background.getIconHeight(), vv); g2d.setTransform(oldXform); } public boolean useTransform() { return false; } }; vv.addPreRenderPaintable(preRender); background.getImage().flush(); Runtime.getRuntime().gc(); }