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

The following are Jave code examples for showing how to use scale() 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: jdk8u-jdk   File: Test8004821.java   View Source Code Vote up 7 votes
public static void main(final String[] args) {
    final int[] arrEmpty = {};
    final int[] arr1elem = {150};
    final BufferedImage bi = new BufferedImage(300, 300,
                                               BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = (Graphics2D) bi.getGraphics();
    test(g, arrEmpty);
    test(g, arr1elem);
    g.translate(2.0, 2.0);
    test(g, arrEmpty);
    test(g, arr1elem);
    g.scale(2.0, 2.0);
    test(g, arrEmpty);
    test(g, arr1elem);
    g.dispose();
}
 
Example 2
Project: org.alloytools.alloy   File: GraphViewer.java   View Source Code Vote up 7 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 3
Project: ALTTPMenuPractice   File: TurnAnalyzer.java   View Source Code Vote up 6 votes
@SuppressWarnings("serial")
public TurnAnalyzer(JFrame frame) {
	super(frame,"Turn analysis");
	menuPainter = new JPanel() {
		public void paint(Graphics g) {
			if (refImg == null) {
				return;
			}
			Graphics2D g2 = (Graphics2D) g;
			g2.scale(2, 2);

			g2.drawImage(refImg, 0, 0, null);
		}
	};
	menuPainter.setPreferredSize(MENU_SIZE_X2);
	menuPainter.setMinimumSize(MENU_SIZE_X2);
	menuPainter.setSize(MENU_SIZE_X2);
	initialize();
}
 
Example 4
Project: Tarski   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 5
Project: rapidminer   File: SimilarityKDistanceVisualization.java   View Source Code Vote up 6 votes
public void paintGraph(Graphics graphics, int pixWidth, int pixHeight) {
	Graphics2D g = (Graphics2D) graphics;
	Graphics2D scaled = (Graphics2D) g.create();

	scaled.translate(0, pixHeight + 1 + this.updatePanelHeight + 50);
	// prepare data
	prepareData();

	scaled.scale(1, -1);
	g.setColor(Color.black);
	drawPoints(scaled, pixWidth, pixHeight);

	scaled.dispose();

	// x-axis label
	String xAxisLabel = "sorted k-distances";
	Rectangle2D stringBounds = SCALED_LABEL_FONT.getStringBounds(xAxisLabel, g.getFontRenderContext());
	g.drawString(xAxisLabel, MARGIN + (float) (pixWidth / 2.0d - stringBounds.getWidth() / 2.0d), MARGIN
			+ (float) (pixHeight - 2.0d * stringBounds.getHeight()) + 3);

	// y-axis label
	String yAxisLabel = "k-distance value";
	stringBounds = LABEL_FONT.getStringBounds(yAxisLabel, g.getFontRenderContext());
	g.drawString(yAxisLabel, MARGIN, (int) (MARGIN + stringBounds.getHeight() + 6));
}
 
Example 6
Project: SpriteAnimator   File: SpriteAnimator.java   View Source Code Vote up 6 votes
/**
 * Draw every sprite
 */
public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D) g;
	g2.scale(zoom, zoom); // zoom in
	int xOffset = offset(posX);
	int yOffset = offset(posY);
	g2.translate(xOffset, yOffset);

	// draw background
	g2.drawImage(bg.getImage(), 0, 0, null);

	// Catch null steps; but they shouldn't happen
	if (steps == null || steps[step] == null) { return; }

	// catch other errors
	try {
		Anime t = steps[step];
		t.draw(g2, posX, posY);
	} catch (Exception e) {
		e.printStackTrace(); // thread conflicts cause errors? idk
	}
}
 
Example 7
Project: openjdk-jdk10   File: RepaintManagerFPUIScaleTest.java   View Source Code Vote up 6 votes
private static Image createTestImage(int w, int h,
        double scaleX, double scaleY, Color color) {

    int width = (int) Math.ceil(scaleX * w);
    int height = (int) Math.ceil(scaleY * h);
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g = img.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    g.scale(scaleX, scaleY);
    g.setColor(color);
    int d = 1;
    int d2 = 2 * d;
    g.drawLine(d, h / 2, w - d2, h / 2);
    g.drawLine(w / 2, d, w / 2, h - d2);
    g.drawRect(d, d, w - d2, h - d2);
    g.dispose();

    return img;
}
 
Example 8
Project: jdk8u-jdk   File: ScaledCopyArea.java   View Source Code Vote up 6 votes
public static void main(final String[] args) {
    final BufferedImage bi = new BufferedImage(100, 300,
                                               BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = bi.createGraphics();
    g.scale(2, 2);
    g.setColor(Color.RED);
    g.fillRect(0, 0, 100, 300);
    g.setColor(Color.GREEN);
    g.fillRect(0, 100, 100, 100);
    g.copyArea(0, 100, 100, 100, 0, -100);
    g.dispose();
    for (int x = 0; x < 100; ++x) {
        for (int y = 0; y < 100; ++y) {
            final int actual = bi.getRGB(x, y);
            final int exp = Color.GREEN.getRGB();
            if (actual != exp) {
                System.err.println("Expected:" + Integer.toHexString(exp));
                System.err.println("Actual:" + Integer.toHexString(actual));
                throw new RuntimeException("Test " + "failed");
            }
        }
    }
}
 
Example 9
Project: VISNode   File: ImageViewerPanel.java   View Source Code Vote up 5 votes
@Override
public void paintComponent(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    AffineTransform previousTransform = g2D.getTransform();
    g2D.scale(zoom, zoom);
    g2D.drawImage(buff, 0, 0, null);
    g2D.setTransform(previousTransform);
}
 
Example 10
Project: sbc-qsystem   File: FWelcome.java   View Source Code Vote up 5 votes
private static int write(Graphics2D g2, String text, int line, int x, double kx, double ky,
    int initY) {
    g2.scale(kx, ky);
    final int y = (int) Math
        .round((initY + line * WelcomeParams.getInstance().lineHeigth) / ky);
    g2.drawString(text, x, y);
    g2.scale(1 / kx, 1 / ky);
    return y;
}
 
Example 11
Project: openjdk-jdk10   File: DrawCachedImageAndTransform.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    GraphicsConfiguration gc = ge.getDefaultScreenDevice()
                                 .getDefaultConfiguration();
    VolatileImage vi = gc.createCompatibleVolatileImage(100, 100);

    Graphics2D g2d = vi.createGraphics();
    g2d.scale(2, 2);
    BufferedImage img = new BufferedImage(50, 50,
                                          BufferedImage.TYPE_INT_ARGB);

    g2d.drawImage(img, 10, 25, Color.blue, null);
    g2d.dispose();
}
 
Example 12
Project: javase   File: Sinx.java   View Source Code Vote up 5 votes
@SuppressWarnings("unused")
private void drawSinxDX(GeneralPath gp, Graphics2D g) {
	for (double i = 0.000001; i <= 8 * Math.PI; i += 0.0001 * Math.PI) {
		gp.lineTo(20 * i, 100 * -Math.sin(i) / i);
	}
	g.draw(gp);
	g.scale(-1, 1);
	g.draw(gp);
}
 
Example 13
Project: jdk8u-jdk   File: MisplacedBorder.java   View Source Code Vote up 5 votes
/**
 * Draws standard JMenuBar.
 */
private BufferedImage step1(final JMenuBar menubar) {
    final BufferedImage bi1 = new BufferedImage(W, H, TYPE_INT_ARGB_PRE);
    final Graphics2D g2d = bi1.createGraphics();
    g2d.scale(2, 2);
    g2d.setColor(Color.RED);
    g2d.fillRect(0, 0, W, H);
    menubar.paintAll(g2d);
    g2d.dispose();
    return bi1;
}
 
Example 14
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 15
Project: geomapapp   File: SCSImage.java   View Source Code Vote up 5 votes
public void paint(Graphics g) {
if( image==null ) return;
		Graphics2D g2 = (Graphics2D)g;
		g2.scale(zoom, zoom);
		g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION,
								RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.drawImage( image, 0, 0, this);
}
 
Example 16
Project: geomapapp   File: ImageBox.java   View Source Code Vote up 5 votes
public void paint(Graphics g2) {
	if (imgPath == null) return;
	Graphics2D g = (Graphics2D) g2;
	double w = getSize().getWidth();
	double h = getSize().getHeight();
	double w2 = image.getWidth(null);
	double h2 = image.getHeight(null);
	double s = Math.min(w / w2, h / h2);

	AffineTransform at = g.getTransform();
	g.scale(s, s);
	g.drawImage(image, 0, 0, null);
	g.setTransform(at);
}
 
Example 17
Project: ALTTPMenuPractice   File: PrettyButton.java   View Source Code Vote up 4 votes
public void paint(Graphics g) {
	Graphics2D g2 = (Graphics2D) g;
	g2.scale(ZOOM, ZOOM);
	g2.drawImage(bgCur, 0, 0, null);
	g2.drawImage(dispCur, 8, 8, null);
}
 
Example 18
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 19
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 20
Project: jdk8u-jdk   File: MultiResolutionImageTest.java   View Source Code Vote up 4 votes
void draw(Graphics graphics, float resolution) {
    Graphics2D g2 = (Graphics2D) graphics;
    g2.scale(resolution, resolution);
    g2.setColor((resolution == 1) ? COLOR_1X : COLOR_2X);
    g2.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
}