Java Code Examples for java.awt.Graphics2D.translate()

The following are Jave code examples for showing how to use translate() of the java.awt.Graphics2D class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: VISNode   File: JNodeConnection.java   View Source Code Vote up 7 votes
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.translate(-getX(), -getY());
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(Color.BLUE);
    int[] xs = new int[points.length];
    int[] ys = new int[points.length];
    for (int i = 0; i < points.length; i++) {
        xs[i] = points[i].x;
        ys[i] = points[i].y;
    }
    g2d.setColor(UIHelper.getColor("NodeConnection.border"));
    g2d.setStroke(new BasicStroke(3));
    g2d.drawPolyline(xs, ys, points.length);
    g2d.setPaint(new GradientPaint(points[0], UIHelper.getColor("NodeConnection.color1"), points[points.length - 1], UIHelper.getColor("NodeConnection.color2")));
    g2d.setStroke(new BasicStroke(1.5f));
    g2d.drawPolyline(xs, ys, points.length);
    g2d.dispose();
}
 
Example 2
Project: OpenJSharp   File: RenderTests.java   View Source Code Vote up 6 votes
public void runTest(Object ctx, int numReps) {
    FillEllipse2Ds.Context cctx = (FillEllipse2Ds.Context) ctx;
    int size = cctx.size;
    int x = cctx.initX;
    int y = cctx.initY;
    Ellipse2D ellipse = cctx.ellipse;
    Graphics2D g2d = (Graphics2D) cctx.graphics;
    g2d.translate(cctx.orgX, cctx.orgY);
    Color rCArray[] = cctx.colorlist;
    int ci = cctx.colorindex;
    do {
        if (rCArray != null) {
            g2d.setColor(rCArray[ci++ & NUM_RANDOMCOLORMASK]);
        }
        ellipse.setFrame(x, y, size, size);
        g2d.fill(ellipse);
        if ((x -= 3) < 0) x += cctx.maxX;
        if ((y -= 1) < 0) y += cctx.maxY;
    } while (--numReps > 0);
    cctx.colorindex = ci;
    g2d.translate(-cctx.orgX, -cctx.orgY);
}
 
Example 3
Project: geomapapp   File: FocalMechanismSolutionDB.java   View Source Code Vote up 6 votes
private BufferedImage drawFMSImage() {
	BufferedImage fms = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

	Graphics2D g = fms.createGraphics();
	Composite c = g.getComposite();

	g.setComposite(AlphaComposite.getInstance(
					AlphaComposite.CLEAR, 0));
	g.fillRect(0, 0, size, size);
	g.setComposite(c);

	g.translate(size / 2, size / 2);
	FMSDraw.drawFMS(g, size / 2,
			eq.strike1,
			eq.dip1, 
			eq.rake1, 
			eq.strike2, 
			eq.dip2, 
			eq.rake2);
	return fms;
}
 
Example 4
Project: incubator-netbeans   File: SlidingTabDisplayerButtonUI.java   View Source Code Vote up 6 votes
/** Provides the painting logic.  Note that this does not call any of the
 * painting methods of BasicToggleButtonUI */
@Override
public final void paint(Graphics g, JComponent c) {
    
    BasicSlidingTabDisplayerUI.IndexButton b = 
        (BasicSlidingTabDisplayerUI.IndexButton) c;
    
    Graphics2D g2d = (Graphics2D) g;
    
    paintBackground (g2d, b);
    
    Object orientation = b.getOrientation();
    
    AffineTransform tr = g2d.getTransform();
    if (orientation == TabDisplayer.ORIENTATION_EAST) {
         g2d.rotate( Math.PI / 2 ); 
         g2d.translate( 0, - c.getWidth() );
    } else if (orientation == TabDisplayer.ORIENTATION_WEST) {
         g2d.rotate(-Math.PI / 2); 
         g2d.translate(-c.getHeight(), 0);
    }

    paintIconAndText (g2d, b, orientation);
    g2d.setTransform (tr);
}
 
Example 5
Project: rapidminer   File: TextFieldBorder.java   View Source Code Vote up 6 votes
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
	Graphics2D g2 = (Graphics2D) g;
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

	g2.translate(x, y);

	if (c.isEnabled()) {
		if (c.isFocusOwner()) {
			g2.setColor(Colors.TEXTFIELD_BORDER_FOCUS);
		} else {
			g2.setColor(Colors.TEXTFIELD_BORDER);
		}
	} else {
		g2.setColor(Colors.TEXTFIELD_BORDER_DISABLED);
	}

	g2.drawRoundRect(0, 0, w - 1, h - 1, RapidLookAndFeel.CORNER_DEFAULT_RADIUS, RapidLookAndFeel.CORNER_DEFAULT_RADIUS);

	g2.translate(-x, -y);
}
 
Example 6
Project: rapidminer   File: SliderUI.java   View Source Code Vote up 6 votes
@Override
protected void paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) {
	if (Math.abs(trackRect.y - y) < 10 || Math.abs(trackRect.y + trackRect.height - y) < 10) {
		return;
	}

	Graphics2D g2 = (Graphics2D) g;

	int curVal = this.slider.getModel().getValue();
	double percentage = (double) curVal / (slider.getModel().getMaximum() - slider.getModel().getExtent());

	if (trackRect.height * percentage > trackRect.height - y) {
		g2.setColor(Colors.SLIDER_TRACK_BACKGROUND);
	} else {
		g2.setColor(Colors.SLIDER_TRACK_BORDER);
	}

	int trackLeft = (int) this.trackRect.getX() + 3;

	g2.translate(-tickBounds.x, 0);
	g2.fillRect(trackLeft, y, 5, 2);
	g2.translate(tickBounds.x, 0);

}
 
Example 7
Project: JavaGraph   File: GraphToVector.java   View Source Code Vote up 6 votes
/** Paints a given jGraph in a {@link Graphics} object. */
protected void toGraphics(JGraph<?> graph, Graphics2D graphics) {
    Rectangle2D bounds = graph.getGraphBounds();

    graphics.translate(-bounds.getMinX(), -bounds.getMinY());
    double scale = graph.getScale();
    graphics.scale(1.0 / scale, 1.0 / scale);

    graph.toScreen(bounds);

    Object[] selection = graph.getSelectionCells();
    boolean gridVisible = graph.isGridVisible();
    boolean dBuf = graph.isDoubleBuffered();
    graph.setGridVisible(false);
    graph.clearSelection();
    // Turn off double buffering, otherwise everything gets rasterized
    graph.setDoubleBuffered(false);

    graph.paint(graphics);

    graph.setDoubleBuffered(dBuf);
    graph.setSelectionCells(selection);
    graph.setGridVisible(gridVisible);
}
 
Example 8
Project: incubator-netbeans   File: AttachableAnchorShape.java   View Source Code Vote up 5 votes
public void paint(Graphics2D graphics, boolean source)
{
    int distance = getAdditionalDistance();
    graphics.translate(distance, 0);
    
    shape.paint(graphics, source);
    
    graphics.translate(-distance, 0);
}
 
Example 9
Project: geomapapp   File: ShipData.java   View Source Code Vote up 5 votes
protected void drawCurrentSeg(Graphics2D g, boolean on) {
		if( currentSeg==null ) return;
		Color color = g.getColor();
		Stroke stroke = g.getStroke();
		AffineTransform at = g.getTransform();
		g.setColor( on ? onColor : offColor );

//		***** GMA 1.5.2: TESTING
//		g.setStroke( new BasicStroke( 1f/ (float)map.getZoom()) );
		g.setStroke( new BasicStroke( 5f/ (float)map.getZoom()) );
//		***** GMA 1.5.2

		double wrap = map.getWrap();
		if( wrap>0. ) {
			g.translate(-wrap, 0.);
			g.draw( currentSeg );
			g.translate(wrap, 0.);
			g.draw( currentSeg );
			g.translate(wrap, 0.);
			g.draw( currentSeg );
		} else {
			g.draw( currentSeg );
		}
		g.setColor(color);
		g.setStroke(stroke);
		g.setTransform( at);
	}
 
Example 10
Project: rapidminer   File: SimilarityKDistanceVisualization.java   View Source Code Vote up 5 votes
private void drawPoints(Graphics2D g, int pixWidth, int pixHeight) {
	double sx = 0.0d;
	double sy = 0.0d;
	sx = ((double) pixWidth - LABEL_MARGIN_X) / (maxX - minX);
	sy = ((double) pixHeight - LABEL_MARGIN_Y) / (maxY - minY);

	Graphics2D coordinateSpace = (Graphics2D) g.create();
	coordinateSpace.translate(LABEL_MARGIN_X, LABEL_MARGIN_Y);
	drawGrid(coordinateSpace, -minX, -minY, sx, sy);
	drawPoints(coordinateSpace, -minX, -minY, sx, sy);
	coordinateSpace.dispose();
}
 
Example 11
Project: smile_1.5.0_java7   File: PlotPanel.java   View Source Code Vote up 5 votes
@Override
public int print(java.awt.Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page > 0) {
        // We have only one page, and 'page' is zero-based
        return NO_SUCH_PAGE;
    }

    Graphics2D g2d = (Graphics2D) g;
    
    // User (0,0) is typically outside the imageable area, so we must
    // translate by the X and Y values in the PageFormat to avoid clipping
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    // Scale plots to paper size.
    double scaleX = pf.getImageableWidth() / contentPane.getWidth();
    double scaleY = pf.getImageableHeight() / contentPane.getHeight();
    g2d.scale(scaleX, scaleY);

    // Disable double buffering
    RepaintManager currentManager = RepaintManager.currentManager(this);
    currentManager.setDoubleBufferingEnabled(false);

    // Now we perform our rendering
    contentPane.printAll(g);

    // Enable double buffering
    currentManager.setDoubleBufferingEnabled(true);

    // tell the caller that this page is part of the printed document
    return PAGE_EXISTS;
}
 
Example 12
Project: openjdk-jdk10   File: PgramUserBoundsTest.java   View Source Code Vote up 5 votes
static void testAll(Graphics2D g2d) {
    g2d.setTransform(identity);
    g2d.translate(100, 100);
    testPrimitives(g2d);

    g2d.setTransform(identity);
    g2d.scale(10, 10);
    testPrimitives(g2d);

    g2d.setTransform(identity);
    g2d.rotate(Math.PI/6);
    testPrimitives(g2d);
}
 
Example 13
Project: rapidminer   File: PlotterAdapter.java   View Source Code Vote up 5 votes
/** This method can be used to draw a legend on the given graphics context. */
public void drawSimpleNumericalLegend(Graphics graphics, int x, int y, String legendColumnName, String minColorString,
		String maxColorString) {
	Graphics2D g = (Graphics2D) graphics.create();

	// painting label name
	g.setFont(LABEL_FONT_BOLD);
	g.setColor(Color.black);
	g.drawString(legendColumnName, x, y + 1);
	Rectangle2D legendNameBounds = LABEL_FONT.getStringBounds(legendColumnName, g.getFontRenderContext());
	g.translate(legendNameBounds.getWidth() + 5, 0);

	// painting legend
	g.setFont(LABEL_FONT);
	g.setColor(Color.black);
	Rectangle2D minStringBounds = LABEL_FONT.getStringBounds(minColorString, g.getFontRenderContext());

	int keyX = x;
	int keyY = y;

	g.drawString(minColorString, keyX, keyY + 1);
	keyX += minStringBounds.getWidth() + 5;
	for (int i = 0; i < 100; i++) {
		double scaledColor = i / 100.0d;
		Color lineColor = getColorProvider().getPointColor(scaledColor, 255);
		g.setColor(lineColor);
		g.drawLine(keyX, keyY, keyX, keyY - 8);
		keyX++;
	}
	g.setColor(Color.black);
	Rectangle2D frame = new Rectangle2D.Double(keyX - 101, keyY - 8, 101, 8);
	g.draw(frame);
	keyX += 4;
	g.drawString(maxColorString, keyX, keyY + 1);
}
 
Example 14
Project: rapidminer   File: AndrewsCurves.java   View Source Code Vote up 5 votes
public void paintAndrewsPlot(Graphics g) {
	int pixWidth = getWidth() - 2 * MARGIN;
	int pixHeight = getHeight() - 2 * MARGIN;

	// translate to ignore margins
	Graphics2D translated = (Graphics2D) g.create();
	translated.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	translated.translate(MARGIN, MARGIN);

	// prepare data ...
	prepareData();

	// legend
	if ((colorColumn != -1) && !Double.isInfinite(minColor) && !Double.isInfinite(maxColor)
			&& (dataTable.isNominal(colorColumn) || (minColor != maxColor)) && (lines.size() > 0)) {
		drawLegend(g, dataTable, colorColumn);
	}

	// draw grid, lines, etc.
	if (lines.size() == 0) {
		translated.drawString("No plots selected.", 0, 0);
	} else {
		g.setColor(Color.black);
		draw(translated, pixWidth, pixHeight);
	}
	translated.dispose();
}
 
Example 15
Project: TrabalhoFinalEDA2   File: mxGraphOutline.java   View Source Code Vote up 4 votes
/**
 * Paints the background.
 */
protected void paintBackground(Graphics g)
{
	if (graphComponent != null)
	{
		Graphics2D g2 = (Graphics2D) g;
		AffineTransform tx = g2.getTransform();

		try
		{
			// Draws the background of the outline if a graph exists 
			g.setColor(graphComponent.getPageBackgroundColor());
			mxUtils.fillClippedRect(g, 0, 0, getWidth(), getHeight());

			g2.translate(translate.x, translate.y);
			g2.scale(scale, scale);

			// Draws the scaled page background
			if (!graphComponent.isPageVisible())
			{
				Color bg = graphComponent.getBackground();

				if (graphComponent.getViewport().isOpaque())
				{
					bg = graphComponent.getViewport().getBackground();
				}

				g.setColor(bg);
				Dimension size = graphComponent.getGraphControl().getSize();

				// Paints the background of the drawing surface
				mxUtils.fillClippedRect(g, 0, 0, size.width, size.height);
				g.setColor(g.getColor().darker().darker());
				g.drawRect(0, 0, size.width, size.height);
			}
			else
			{
				// Paints the page background using the graphics scaling
				graphComponent.paintBackgroundPage(g);
			}
		}
		finally
		{
			g2.setTransform(tx);
		}
	}
	else
	{
		// Draws the background of the outline if no graph exists 
		g.setColor(getBackground());
		mxUtils.fillClippedRect(g, 0, 0, getWidth(), getHeight());
	}
}
 
Example 16
Project: ramus   File: TextPaintCache.java   View Source Code Vote up 4 votes
public FRectangle paintText(final Graphics2D g, final String text,
                            final FRectangle frect, final int align, final int pos,
                            final boolean zoom, MovingArea area, boolean cached) {
    if (text.trim().length() == 0)
        return frect;

    TextKey key = new TextKey(cached);

    float width = (float) area.getDoubleOrdinate(frect.getWidth());
    if (zoom)
        width *= (float) area.zoom;

    key.width = width;
    key.text = text;
    key.font = g.getFont();

    TextBounds textBounds = getTextBounds(text, key);

    if (zoom) {
        g.scale(area.zoom, area.zoom);
        g.translate(frect.getX(), frect.getY());
    }
    if (nativePaint)
        textBounds.drawNative(g, align, pos, (float) frect.getHeight(),
                area);
    else if (searchText == null)
        textBounds.draw(g, align, pos, (float) frect.getHeight(), area);
    else {
        if (text.toLowerCase().contains(searchText)) {
            drawSearch(text, g, align, pos, (float) frect.getHeight(),
                    area, key);
        } else
            textBounds.draw(g, align, pos, (float) frect.getHeight(), area);

    }

    if (zoom) {
        g.translate(-frect.getX(), -frect.getY());
        g.scale(1.0 / area.zoom, 1.0 / area.zoom);
        return new FRectangle(frect.getX(), frect.getY(),
                textBounds.getWidth(), area.getDoubleOrdinate(textBounds
                .getHeight()) * area.zoom);
    }
    return new FRectangle(frect.getX(), frect.getY(),
            textBounds.getWidth(), area.getDoubleOrdinate(textBounds
            .getHeight()));
}
 
Example 17
Project: ramus   File: ColumnElement.java   View Source Code Vote up 4 votes
@Override
protected void paintText(Graphics2D g) {
    g.translate(6, 0);
    label.paint(g);
    g.translate(-6, 0);
}
 
Example 18
Project: rapidminer   File: AutoMLPImprovedNeuralNetVisualizer.java   View Source Code Vote up 4 votes
private void paintNodes(Graphics2D g, int height) {
	for (Map.Entry<Integer, List<Node>> entry : layers.entrySet()) {
		int layerIndex = entry.getKey();
		List<Node> layer = entry.getValue();
		int nodes = layer.size();
		if (layerIndex < layers.size() - 1) {
			nodes++;
		}

		String layerName = null;
		if (layerIndex == 0) {
			layerName = "Input";
		} else if (layerIndex == layers.size() - 1) {
			layerName = "Output";
		} else {
			layerName = "Hidden " + layerIndex;
		}

		Rectangle2D stringBounds = LABEL_FONT.getStringBounds(layerName, g.getFontRenderContext());
		g.setColor(Color.BLACK);
		g.drawString(layerName, (int) (((-1) * stringBounds.getWidth() / 2) + NODE_RADIUS / 2), 0);
		int yPos = (height / 2) - (nodes * ROW_HEIGHT / 2);
		for (int r = 0; r < nodes; r++) {
			Shape node = new Ellipse2D.Double(0, yPos, NODE_RADIUS, NODE_RADIUS);
			if ((layerIndex == 0) || (layerIndex == layers.size() - 1)) {
				if ((r < nodes - 1) || (layerIndex == layers.size() - 1)) {
					g.setPaint(SwingTools.makeYellowPaint(NODE_RADIUS, NODE_RADIUS));
				} else {
					g.setPaint(new Color(233, 233, 233));
				}
			} else {
				if (r < nodes - 1) {
					g.setPaint(SwingTools.makeBluePaint(NODE_RADIUS, NODE_RADIUS));
				} else {
					g.setPaint(new Color(233, 233, 233));
				}
			}
			g.fill(node);
			if ((layerIndex == this.selectedLayerIndex) && (r == this.selectedRowIndex)) {
				g.setColor(Color.RED);
			} else {
				g.setColor(Color.BLACK);
			}
			g.draw(node);
			yPos += ROW_HEIGHT;
		}
		g.translate(LAYER_WIDTH, 0);
		layerIndex++;
	}
}
 
Example 19
Project: geomapapp   File: MapServerA.java   View Source Code Vote up 4 votes
public static boolean getHighRes(double[] wesn, int width, int height, BufferedImage image) {
	getImage(wesn, width, height, image);
	Graphics2D g = image.createGraphics();
	Rectangle rect = new Rectangle(0, 0, width, height);
	double scale = (wesn[1] - wesn[0]) / (double)width;
	double r = 1/scale;
	if(r < (double)res[0]) return false;
	int ires = 0;
	while( r < (double)highRes[ires] && ires<highRes.length-1) ires++;
	if(ires == highRes.length-1) ires--;
	double gridScale = 600 / (double) highRes[ires];

	int x1 = (int) Math.floor( wesn[0] / gridScale);
	int x2 = (int) Math.ceil( wesn[1] / gridScale);
	int xmax = (int) Math.ceil( 360 / gridScale);

	int y1 = (int) Math.ceil( wesn[3] / gridScale);
	int y2 = (int) Math.floor( wesn[2] / gridScale);
	Rectangle clipRect = new Rectangle(8, 8, 600, 600);

	int xgrid, ygrid;
	String name;
	File file;
	double x0, y0;
	double west;
	double north;
	double offset;
	double pad = 8 / (double) highRes[ires];
	BufferedImage im = null;
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
		RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	AffineTransform at = new AffineTransform();
	boolean returnVal = false;
	for( int x=x1 ; x<x2 ; x++ ) {
		xgrid = x;
		offset = -pad;
		while(xgrid < 0) {
			xgrid += xmax;
			offset -= 360;
		}
		while(xgrid >= xmax) {
			xgrid -= xmax;
			offset += 360;
		}
		west = (double) xgrid * gridScale + offset;
		x0 = (west - wesn[0]) / scale;
		for( int y=y1 ; y>y2 ; y--) {

			north = (double)y *gridScale + pad;
			if( y>0 ) {
				name = "g" + highRes[ires] + "_" + xgrid + "_N" + y +".jpg";
			} else {
				ygrid = -y;
				name = "g" + highRes[ires] + "_" + xgrid + "_S" + ygrid +".jpg";
			}
			String url = haxby.map.MapApp.TEMP_BASE_URL + "tiles/g"+highRes[ires]+"/" + name;
			im = null;
			try { 
				BufferedInputStream in = new BufferedInputStream(
					(URLFactory.url(url)).openStream());
				//JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
				//im = decoder.decodeAsBufferedImage();
				im = ImageIO.read(in);
				in.close();
			} catch ( Exception ioe) {
				continue;
			}
			g.setTransform(at);
			g.setClip(rect);
			y0 = (wesn[3] - north) / scale ;
			g.translate(x0, y0);
			double scl = 1 / ((double) highRes[ires] * scale);
			g.scale(scl, scl);
			g.clip(clipRect);
			returnVal = true;
			g.drawRenderedImage(im, at);
		}
	}
	return returnVal;
}
 
Example 20
Project: rapidminer   File: ComboBoxUI.java   View Source Code Vote up 4 votes
/**
 * Draws the combobox itself.
 */
private void paintBox(Graphics g, JComponent c) {
	int w = c.getWidth();
	int h = c.getHeight() - 1;
	if (w <= 0 || h <= 0) {
		return;
	}

	Graphics2D g2 = (Graphics2D) g;
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

	if (c.isEnabled()) {
		if (Boolean.parseBoolean(String.valueOf(c.getClientProperty(RapidLookTools.PROPERTY_INPUT_BACKGROUND_DARK)))) {
			g2.setColor(Colors.COMBOBOX_BACKGROUND_DARK);
		} else {
			g2.setColor(Colors.COMBOBOX_BACKGROUND);
		}
	} else {
		g2.setColor(Colors.COMBOBOX_BACKGROUND_DISABLED);
	}

	g2.fillRoundRect(0, 0, w - 1, h, RapidLookAndFeel.CORNER_DEFAULT_RADIUS, RapidLookAndFeel.CORNER_DEFAULT_RADIUS);

	// arrow
	int ny = c.getSize().height / 2 - 3;
	int nx = c.getWidth() - 15;

	if (isDown && c.isEnabled()) {
		nx++;
		ny++;
	}
	g2.translate(nx, ny);

	if (c.isEnabled()) {
		g2.setColor(Colors.COMBOBOX_ARROW);
	} else {
		g2.setColor(Colors.COMBOBOX_ARROW_DISABLED);
	}

	w = 14;
	Polygon arrow = new Polygon(new int[] { 0, 4, 8 }, new int[] { 0, 6, 0 }, 3);
	g2.fillPolygon(arrow);

	g2.translate(-nx, -ny);
}