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

The following are Jave code examples for showing how to use setTransform() 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: litiengine   File: RenderEngine.java   View Source Code Vote up 7 votes
public static void drawShape(final Graphics2D g, final Shape shape, final Stroke stroke) {
  if (shape == null) {
    return;
  }

  final AffineTransform oldTransForm = g.getTransform();
  final Stroke oldStroke = g.getStroke();
  final AffineTransform t = new AffineTransform();
  t.scale(Game.getCamera().getRenderScale(), Game.getCamera().getRenderScale());
  t.translate(Game.getCamera().getPixelOffsetX(), Game.getCamera().getPixelOffsetY());

  g.setTransform(t);
  g.setStroke(stroke);
  g.draw(shape);
  g.setTransform(oldTransForm);
  g.setStroke(oldStroke);
}
 
Example 2
Project: litiengine   File: CombatEntityVision.java   View Source Code Vote up 6 votes
@Override
public void renderFogOfWar(final Graphics2D g) {
  if (this.fogOfWar == null) {
    return;
  }

  final AffineTransform oldTransform = g.getTransform();
  final AffineTransform at = new AffineTransform();
  at.scale(Game.getCamera().getRenderScale(), Game.getCamera().getRenderScale());
  at.translate(Game.getCamera().getPixelOffsetX(), Game.getCamera().getPixelOffsetY());

  g.setTransform(at);
  g.setColor(FogOfWarColor);
  g.fill(this.fogOfWar);
  g.setTransform(oldTransform);
}
 
Example 3
Project: TrabalhoFinalEDA2   File: mxGraphOutline.java   View Source Code Vote up 6 votes
/**
 * Paints the graph outline.
 */
public void paintGraph(Graphics g)
{
	if (graphComponent != null)
	{
		Graphics2D g2 = (Graphics2D) g;
		AffineTransform tx = g2.getTransform();

		try
		{
			Point tr = graphComponent.getGraphControl().getTranslate();
			g2.translate(translate.x + tr.getX() * scale,
					translate.y + tr.getY() * scale);
			g2.scale(scale, scale);

			// Draws the scaled graph
			graphComponent.getGraphControl().drawGraph(g2, drawLabels);
		}
		finally
		{
			g2.setTransform(tx);
		}
	}
}
 
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: geomapapp   File: GMAProfile.java   View Source Code Vote up 6 votes
public void draw(Graphics2D g) {
	arc = null;;
	arc2 = null;;
	if( path==null)return;
	AffineTransform at = g.getTransform();
	if( !enabled || line==null) return;
	g.setStroke( new BasicStroke(4.f/(float)map.getZoom()) );
	g.setColor(Color.white);
	g.draw( path );
	double wrap = map.getWrap();
	if( wrap>0.) {
		g.translate(wrap, 0.);
		g.draw( path );
	}
	g.setTransform(at);
}
 
Example 6
Project: VASSAL-src   File: Labeler.java   View Source Code Vote up 6 votes
public void draw(Graphics g, int x, int y, Component obs, double zoom) {
    updateCachedImage();
    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);
    }
  }
 
Example 7
Project: Panako   File: QIFFTAudioInfoLayer.java   View Source Code Vote up 6 votes
@Override
public void mouseMoved(MouseEvent e) {
	if(graphics !=null ){
		LinkedPanel panel = (LinkedPanel) e.getComponent();
		Graphics2D g = (Graphics2D) panel.getGraphics();
		g.setTransform(panel.getTransform());
		
		float timeOffset = LayerUtilities.pixelsToUnits(g, e.getX(), true);
		float time = cs.getMin(Axis.X) + timeOffset;
		float frequencyOffset = LayerUtilities.pixelsToUnits(g, panel.getHeight() - e.getY(), false);
		float frequency = cs.getMin(Axis.Y) + frequencyOffset;
		
		float timeDiameter = LayerUtilities.pixelsToUnits(g, 10, true) * 2f;
		float frequencyDiameter = LayerUtilities.pixelsToUnits(g, 10, false)* 2f;
		
	}
	
}
 
Example 8
Project: litiengine   File: RenderEngine.java   View Source Code Vote up 5 votes
public static void fillShape(final Graphics2D g, final Shape shape) {
  if (shape == null) {
    return;
  }

  final AffineTransform oldTransForm = g.getTransform();
  final AffineTransform t = new AffineTransform();
  t.scale(Game.getCamera().getRenderScale(), Game.getCamera().getRenderScale());
  t.translate(Game.getCamera().getPixelOffsetX(), Game.getCamera().getPixelOffsetY());

  g.setTransform(t);
  g.fill(shape);
  g.setTransform(oldTransForm);
}
 
Example 9
Project: JBotSim   File: JNode.java   View Source Code Vote up 5 votes
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    double direction = this.node.getDirection();
    if (direction != Math.PI / 2) {
        AffineTransform newXform = g2d.getTransform();
        newXform.rotate(direction + Math.PI / 2, drawSize, drawSize);
        g2d.setTransform(newXform);
    }
    g2d.drawImage(scaledIcon, 0, 0, null);
    JTopology jTopology = (JTopology) this.getParent();
    for (NodePainter painter : jTopology.nodePainters)
        painter.paintNode(g2d, node);
}
 
Example 10
Project: Tarski   File: GraphViewer.java   View Source Code Vote up 5 votes
/** This method is called by Swing to draw this component. */
@Override public void paintComponent(final Graphics gr) {
    super.paintComponent(gr);
    Graphics2D g2 = (Graphics2D)gr;
    AffineTransform oldAF = (AffineTransform) (g2.getTransform().clone());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.scale(scale, scale);
    Object sel=(selected!=null ? selected : highlight);
    GraphNode c=null;
    if (sel instanceof GraphNode && ((GraphNode)sel).shape()==null) { c = (GraphNode)sel; sel = c.ins.get(0); }
    graph.draw(new Artist(g2), scale, sel, true);
    if (c!=null) { gr.setColor(((GraphEdge)sel).color()); gr.fillArc(c.x()-5-graph.getLeft(), c.y()-5-graph.getTop(), 10, 10, 0, 360); }
    g2.setTransform(oldAF);
}
 
Example 11
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 12
Project: openjdk-jdk10   File: TestTransform.java   View Source Code Vote up 5 votes
public static void testTransformedFont(AffineTransform a, Object textHint) {
    BufferedImage bi = new BufferedImage(200, 200,
                               BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) bi.getGraphics();
    g2.setFont(g2.getFont().deriveFont(12.0f));
    g2.setTransform(a);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textHint);
    g2.drawString("test", 100, 100);
}
 
Example 13
Project: FaceAnnotationTool   File: MyImagePanel.java   View Source Code Vote up 5 votes
public void drawStaticEllipses(Graphics2D g2d) {
	AffineTransform old = g2d.getTransform();
	// -----------------------------------------//
	ArrayList<Ellipse> staticEllipses = mImg.getEllipseStatic();
	if (staticEllipses == null)
		return;
	int i = 0;
	for (Ellipse e : staticEllipses) {
		e = e.offset(minX, minY);
		BasicStroke bs = UniversalTool.getPreferableStroke(Math.max(e.major, e.minor) * 2);

		// do rotation
		g2d.rotate(e.angle, e.x, e.y);
		// draw elps

		Ellipse2D.Double ed = e.getErectedEllipse2D();
		if(i == highlightedStaticEllipseIdx){
			g2d.setColor(Color.CYAN);
		}else{
			g2d.setColor(Color.black);
		}
		
		g2d.setStroke(bs);
		g2d.draw(ed);

		g2d.setColor(Color.yellow);
		g2d.setStroke(new BasicStroke(bs.getLineWidth() / 2));

		g2d.draw(ed);

		// reset transform
		g2d.setTransform(old);
		i++;
	}
}
 
Example 14
Project: geomapapp   File: ImageLayer.java   View Source Code Vote up 5 votes
public void draw(Graphics2D g, AffineTransform aTrans, Rectangle2D bounds) {
	AffineTransform at0 = g.getTransform();
	g.transform( aTrans );
	g.translate( x, y );
	g.scale( scale, scale );
	g.drawRenderedImage( image, new AffineTransform() );
	g.setTransform( at0 );
}
 
Example 15
Project: JDigitalSimulator   File: Simulation.java   View Source Code Vote up 5 votes
@Override public void paint(Graphics defaultGraphics) {
	super.paint(defaultGraphics);
	Graphics2D graphics = prepareGraphics(defaultGraphics);
	AffineTransform transform = graphics.getTransform();
	graphics.transform(this.transform);

	paint = System.currentTimeMillis();
	if(isGridVisible())
		paintGrid(graphics, super.getPreferredSize());

	List<Component> components = new ArrayList<Component>(getAllComponents());
	if(dragComponents!=null)
		components.removeAll(dragComponents);
	paintComponents(graphics, components, dragWires);
	if(selectWire!=null) {
		graphics.setColor(SELECTION);
		graphics.fill(selectWire.getSelectionArea());
	} else if(selectComponent==null)
		paintSelection(graphics);
	if(dragContact!=null&&dragContact.isWired())
		dragContact.getWire().paint(graphics);
	if(dragComponents!=null) {
		Point point = invertPoint(addPoints(select, invertPoint(current)));
		graphics.drawImage(dragImage, point.x, point.y, null);
		if(dragWires!=null) for(Wire dragWire:dragWires) {
			if(dragComponents.contains(dragWire.getSource().getComponent()))
				dragWire.paint(graphics, point);
			else if(dragComponents.contains(dragWire.getTarget().getComponent()))
				dragWire.paint(graphics, null, point);
		}
	}

	graphics.setTransform(transform);
}
 
Example 16
Project: Tarski   File: GraphViewer.java   View Source Code Vote up 5 votes
/** This method is called by Swing to draw this component. */
@Override public void paintComponent(final Graphics gr) {
    super.paintComponent(gr);
    Graphics2D g2 = (Graphics2D)gr;
    AffineTransform oldAF = (AffineTransform) (g2.getTransform().clone());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.scale(scale, scale);
    Object sel=(selected!=null ? selected : highlight);
    GraphNode c=null;
    if (sel instanceof GraphNode && ((GraphNode)sel).shape()==null) { c = (GraphNode)sel; sel = c.ins.get(0); }
    graph.draw(new Artist(g2), scale, sel, true);
    if (c!=null) { gr.setColor(((GraphEdge)sel).color()); gr.fillArc(c.x()-5-graph.getLeft(), c.y()-5-graph.getTop(), 10, 10, 0, 360); }
    g2.setTransform(oldAF);
}
 
Example 17
Project: geomapapp   File: MGGData.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 ? ON_COLOR : OFF_COLOR );

//		***** 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 18
Project: openjdk-jdk10   File: DrawImageCoordsTest.java   View Source Code Vote up 5 votes
public static void main(String[] args) {

        /* Create an image to draw, filled in solid red. */
        BufferedImage srcImg =
             new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        Graphics srcG = srcImg.createGraphics();
        srcG.setColor(Color.red);
        int w = srcImg.getWidth(null);
        int h = srcImg.getHeight(null);
        srcG.fillRect(0, 0, w, h);

        /* Create a destination image */
        BufferedImage dstImage =
           new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        Graphics2D dstG = dstImage.createGraphics();
        /* draw image under a scaling transform that overflows int */
        AffineTransform tx = new AffineTransform(0.5, 0, 0, 0.5,
                                                  0, 5.8658460197478485E9);
        dstG.setTransform(tx);
        dstG.drawImage(srcImg, 0, 0, null );
        /* draw image under the same overflowing transform, cancelling
         * out the 0.5 scale on the graphics
         */
        dstG.drawImage(srcImg, 0, 0, 2*w, 2*h, null);
        if (Color.red.getRGB() == dstImage.getRGB(w/2, h/2)) {
             throw new RuntimeException("Unexpected color: clipping failed.");
        }
        System.out.println("Test Thread Completed");
    }
 
Example 19
Project: jdk8u-jdk   File: FontPanel.java   View Source Code Vote up 4 votes
private void modeSpecificDrawLine( Graphics2D g2, String line,
                                   int baseX, int baseY ) {
    /// ABP - keep track of old tform, restore it later
    AffineTransform oldTx = null;
    oldTx = g2.getTransform();
    g2.translate( baseX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    switch ( drawMethod ) {
      case DRAW_STRING:
        g2.drawString( line, 0, 0 );
        break;
      case DRAW_CHARS:
        g2.drawChars( line.toCharArray(), 0, line.length(), 0, 0 );
        break;
      case DRAW_BYTES:
        try {
            byte lineBytes[] = line.getBytes( "ISO-8859-1" );
            g2.drawBytes( lineBytes, 0, lineBytes.length, 0, 0 );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.drawGlyphVector( gv, (float) 0, (float) 0 );
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout( line, testFont,
                                        g2.getFontRenderContext() );
        tl.draw( g2, (float) 0, (float) 0 );
        break;
      case GV_OUTLINE:
        GlyphVector gvo =
          testFont.createGlyphVector( g2.getFontRenderContext(), line );
        g2.draw( gvo.getOutline( (float) 0, (float) 0 ));
        break;
      case TL_OUTLINE:
        TextLayout tlo =
          new TextLayout( line, testFont,
                          g2.getFontRenderContext() );
        AffineTransform at = new AffineTransform();
        g2.draw( tlo.getOutline( at ));
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTx );

}
 
Example 20
Project: parabuild-ci   File: ChartUtilities.java   View Source Code Vote up 4 votes
/**
 * Writes a scaled version of a chart to an output stream in PNG format.
 *
 * @param out  the output stream (<code>null</code> not permitted).
 * @param chart  the chart (<code>null</code> not permitted).
 * @param width  the unscaled chart width.
 * @param height  the unscaled chart height.
 * @param widthScaleFactor  the horizontal scale factor.
 * @param heightScaleFactor  the vertical scale factor.
 *
 * @throws IOException if there are any I/O problems.
 */
public static void writeScaledChartAsPNG(OutputStream out,
                                         JFreeChart chart,
                                         int width,
                                         int height,
                                         int widthScaleFactor,
                                         int heightScaleFactor) throws IOException {

    if (out == null) {
        throw new IllegalArgumentException("Null 'out' argument.");
    }
    if (chart == null) {
        throw new IllegalArgumentException("Null 'chart' argument.");
    }

    double desiredWidth = width * widthScaleFactor;
    double desiredHeight = height * heightScaleFactor;
    double defaultWidth = width;
    double defaultHeight = height;
    boolean scale = false;

    // get desired width and height from somewhere then...
    if ((widthScaleFactor != 1) || (heightScaleFactor != 1)) {
        scale = true;
    }

    double scaleX = desiredWidth / defaultWidth;
    double scaleY = desiredHeight / defaultHeight;

    BufferedImage image = new BufferedImage(
        (int) desiredWidth, (int) desiredHeight, BufferedImage.TYPE_INT_ARGB
    );
    Graphics2D g2 = image.createGraphics();

    if (scale) {
        AffineTransform saved = g2.getTransform();
        g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
        chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), null, null);
        g2.setTransform(saved);
        g2.dispose();
    }
    else {
        chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), null, null);
    }
    out.write(encodeAsPNG(image));

}