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

The following are Jave code examples for showing how to use setRenderingHint() 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: DrawRotatedString.java   View Source Code Vote up 6 votes
private static BufferedImage createBufferedImage(final boolean  aa) {
    final BufferedImage bi = new BufferedImage(SIZE, SIZE,
                                               BufferedImage.TYPE_INT_RGB);
    final Graphics2D bg = bi.createGraphics();
    bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        aa ? RenderingHints.VALUE_ANTIALIAS_ON
                           : RenderingHints.VALUE_ANTIALIAS_OFF);
    bg.setColor(Color.RED);
    bg.fillRect(0, 0, SIZE, SIZE);
    bg.translate(100, 100);
    bg.rotate(Math.toRadians(90));
    bg.setColor(Color.BLACK);
    bg.setFont(bg.getFont().deriveFont(20.0f));
    bg.drawString("MMMMMMMMMMMMMMMM", 0, 0);
    bg.dispose();
    return bi;
}
 
Example 2
Project: Progetto-B   File: GraphicsJLabel.java   View Source Code Vote up 6 votes
/**
 * Paints the JLabel.
 *
 * @param g
 */
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    myPath.moveTo(xx[0], yy[0]);
    myPath.lineTo(xx[0], yy[0]);
    myPath.lineTo(xx[1], yy[1]);
    myPath.lineTo(xx[2], yy[2]);
    myPath.closePath();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(GRADIENT_PAINT);
    g2.fill(myPath);
    myPath.reset();
}
 
Example 3
Project: SER316-Dresden   File: HTMLEditorPane.java   View Source Code Vote up 6 votes
public void paint(Graphics g) {
    if (antiAlias) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        /*g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);*/
        super.paint(g2);
    } else {
        super.paint(g);
    }
}
 
Example 4
Project: org.alloytools.alloy   File: GraphViewer.java   View Source Code Vote up 6 votes
/**
 * Export the current drawing as a PNG file with the given file name and
 * image resolution.
 */
public void alloySaveAsPNG(String filename, double scale, double dpiX, double dpiY) throws IOException {
	try {
		int width = (int) (graph.getTotalWidth() * scale);
		if (width < 10)
			width = 10;
		int height = (int) (graph.getTotalHeight() * scale);
		if (height < 10)
			height = 10;
		BufferedImage bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D gr = (Graphics2D) (bf.getGraphics());
		gr.setColor(WHITE);
		gr.fillRect(0, 0, width, height);
		gr.setColor(BLACK);
		gr.scale(scale, scale);
		gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		graph.draw(new Artist(gr), scale, null, false);
		OurPNGWriter.writePNG(bf, filename, dpiX, dpiY);
	} catch (Throwable ex) {
		if (ex instanceof IOException)
			throw (IOException) ex;
		throw new IOException("Failure writing the PNG file to " + filename + " (" + ex + ")");
	}
}
 
Example 5
Project: jdk8u-jdk   File: DrawRotatedStringUsingRotatedFont.java   View Source Code Vote up 6 votes
/**
 * Creates an BufferedImage and draws a text, using two transformations,
 * one for graphics and one for font.
 */
private static BufferedImage createImage(final boolean aa,
                                         final AffineTransform gtx,
                                         final AffineTransform ftx) {
    final BufferedImage bi = new BufferedImage(SIZE, SIZE, TYPE_INT_RGB);
    final Graphics2D bg = bi.createGraphics();
    bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        aa ? RenderingHints.VALUE_ANTIALIAS_ON
                           : RenderingHints.VALUE_ANTIALIAS_OFF);
    bg.setColor(Color.RED);
    bg.fillRect(0, 0, SIZE, SIZE);
    bg.translate(100, 100);
    bg.transform(gtx);
    bg.setColor(Color.BLACK);
    bg.setFont(bg.getFont().deriveFont(20.0f).deriveFont(ftx));
    bg.drawString(STR, 0, 0);
    bg.dispose();
    return bi;
}
 
Example 6
Project: hearthstone   File: PartidaView.java   View Source Code Vote up 5 votes
public void pintarLinhaEvento(Graphics2D g2, Point origem, Point destino) {
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(5, BasicStroke.JOIN_ROUND, BasicStroke.CAP_ROUND));
    g2.setColor(Color.red);
    g2.drawLine(origem.x, origem.y, destino.x, destino.y);
    g2.setStroke(new BasicStroke(5, BasicStroke.JOIN_ROUND, BasicStroke.CAP_ROUND));
    g2.drawOval(destino.x - 25, destino.y - 25, 50, 50);
    g2.fillOval(destino.x - 10, destino.y - 10, 20, 20);
}
 
Example 7
Project: Logisim   File: About.java   View Source Code Vote up 5 votes
@Override
public void paintComponent(Graphics g) {
	if (AppPreferences.ANTI_ALIASING.getBoolean()) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	}
	super.paintComponent(g);
	drawText(g, 10, 20);
}
 
Example 8
Project: Moenagade   File: BloxsEditor.java   View Source Code Vote up 5 votes
@Override
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g;
    
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON );
    g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                        RenderingHints.VALUE_RENDER_QUALITY );

    g.setColor(BloxsColors.$BACKGROUND);
    g.fillRect(0, 0, getWidth(), getHeight());
    
    drawLast.clear();
    
    Rectangle result = new Rectangle();
    for (int i = 0; i < elements.size(); i++) {
        Element get = elements.get(i);
        result=get.draw((Graphics2D) g).union(result);
    }
    
    //System.out.println("Size: "+drawLast.size());
    for (int i = 0; i < drawLast.size(); i++) {
        result=drawLast.get(i).draw((Graphics2D) g).union(result);    
    }
    
    /*
    g.setColor(Color.RED);
    g.drawRect(result.x, result.y, result.width, result.height);
    */
    setPreferredSize(new Dimension(result.x+result.width, result.y+result.height));
    revalidate();
}
 
Example 9
Project: ramus   File: StartupPanel.java   View Source Code Vote up 5 votes
@Override
protected void paintComponent(Graphics g) {
    final float POINTS = 9;
    final float SIZE = 8;

    Graphics2D g2 = (Graphics2D) g.create();
    g2.setColor(Color.BLUE);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    long time = gTime;
    gTime += 1;
    time %= POINTS + 1;

    double angle = time / (POINTS + 1) * Math.PI * 2.0;

    double w = getWidth();
    double h = getHeight();

    for (int i = 0; i < POINTS; i++) {
        double x = Math.sin(angle);
        double y = -Math.cos(angle);

        angle += Math.PI * 2.0 / (POINTS + 1);

        float strength = 1f - (POINTS - i - 1) / POINTS;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, strength));

        double mx = w / 2 + x * w * (SIZE - 2) / SIZE / 2.0;
        double my = h / 2 + y * h * (SIZE - 2) / SIZE / 2.0;

        g2.fillOval((int) (mx - w / SIZE / 2), (int) (my - h / SIZE / 2), (int) (w / SIZE), (int) (h / SIZE));
    }

    g2.dispose();
}
 
Example 10
Project: BaseClient   File: TextureAnimations.java   View Source Code Vote up 5 votes
public static BufferedImage scaleBufferedImage(BufferedImage p_scaleBufferedImage_0_, int p_scaleBufferedImage_1_, int p_scaleBufferedImage_2_)
{
    BufferedImage bufferedimage = new BufferedImage(p_scaleBufferedImage_1_, p_scaleBufferedImage_2_, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2d.drawImage(p_scaleBufferedImage_0_, 0, 0, p_scaleBufferedImage_1_, p_scaleBufferedImage_2_, (ImageObserver)null);
    return bufferedimage;
}
 
Example 11
Project: rapidminer   File: TabbedPaneUI.java   View Source Code Vote up 5 votes
private static void paintSelectedTop(Graphics g, int x, int y, int w, int h) {
	Graphics2D g2 = (Graphics2D) g.create();
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

	drawTopTab(x, y, w, h, g2, Colors.TAB_BACKGROUND_SELECTED);

	g2.dispose();
}
 
Example 12
Project: Rubus   File: Convolution.java   View Source Code Vote up 5 votes
private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {

		BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
		g.dispose();
		g.setComposite(AlphaComposite.Src);

		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		return resizedImage;
	}
 
Example 13
Project: openjdk-jdk10   File: CrashPaintTest.java   View Source Code Vote up 5 votes
@Override
public PaintContext createContext(ColorModel cm,
                                  Rectangle deviceBounds,
                                  Rectangle2D userBounds,
                                  AffineTransform at,
                                  RenderingHints hints) {

    // Fill bufferedImage using
    final Graphics2D g2d = (Graphics2D) getImage().getGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setBackground(Color.PINK);
        g2d.clearRect(0, 0, size, size);

        g2d.setColor(Color.BLUE);
        g2d.drawRect(0, 0, size, size);

        g2d.fillOval(size / 10, size / 10,
                     size * 8 / 10, size * 8 / 10);

    } finally {
        g2d.dispose();
    }

    return super.createContext(cm, deviceBounds, userBounds, at, hints);
}
 
Example 14
Project: openjdk-jdk10   File: RenderToCustomBufferTest.java   View Source Code Vote up 5 votes
private static void renderTo(BufferedImage dst) {
    System.out.println("The buffer: " + dst);
    Graphics2D g = dst.createGraphics();

    final int w = dst.getWidth();
    final int h = dst.getHeight();

    g.setColor(Color.blue);
    g.fillRect(0, 0, w, h);

    g.setColor(Color.red);
    Font f = g.getFont();
    g.setFont(f.deriveFont(48f));

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    // NB: this clip ctriggers the problem
    g.setClip(50, 50, 200, 100);

    g.drawString("AA Text", 52, 90);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
           RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

    // NB: this clip ctriggers the problem
    g.setClip(50, 100, 100, 100);
    g.drawString("Text", 52, 148);

    g.dispose();
}
 
Example 15
Project: GIFKR   File: ImageTools.java   View Source Code Vote up 5 votes
public static BufferedImage unrotate(BufferedImage img, double angle, int origw, int origh, boolean subpixel, boolean fast) {

		img = rotate(img, 360 - (angle % 360), fast);

		int startx = (img.getWidth()-origw)/2;
		int starty = (img.getHeight()-origh)/2;

		int extra = (img.getWidth() - origw) >2 ? 1 : 0;

		img = img.getSubimage(startx, starty, origw + extra, origh + extra);

		AffineTransform t = new AffineTransform();

		if(subpixel) {
			double xerr = 0, yerr = 0;
			for(int x = 0; x < img.getWidth(); x++)
				xerr += ImageTools.getAlpha(img.getRGB(x, 0));
			for(int y = 0; y < img.getHeight(); y++)
				yerr += ImageTools.getAlpha(img.getRGB(0, y));

			xerr /= (img.getWidth() * 255);
			xerr = 1 - xerr;
			yerr /= (img.getHeight() * 255);
			yerr = 1 - yerr;
			t.translate(-yerr, -xerr);
		}

		BufferedImage ret = new BufferedImage(origw, origh, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = ret.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		g.drawImage(img, t, null);
		g.dispose();

		return ret;
	}
 
Example 16
Project: rapidminer   File: RadioButtonIcon.java   View Source Code Vote up 4 votes
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
	JRadioButton radioButton = (JRadioButton) c;
	ButtonModel bm = radioButton.getModel();
	int w = c.getWidth();
	int h = c.getHeight();
	if (h < 0 || w < 0) {
		return;
	}

	g.translate(x, y);

	boolean isSelected = bm.isSelected();
	boolean isEnabled = bm.isEnabled();
	boolean isPressed = bm.isPressed();

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

	// drawing background section
	if (!isEnabled) {
		g2.setColor(Colors.RADIOBUTTON_BACKGROUND_DISABLED);
	} else {
		g2.setColor(Colors.RADIOBUTTON_BACKGROUND);
	}
	Shape circle = new Ellipse2D.Double(2, 2, 12, 12);
	g2.fill(circle);

	if (!isEnabled) {
		g2.setColor(Colors.RADIOBUTTON_BORDER_DISABLED);
	} else {
		if (isPressed) {
			g2.setColor(Colors.RADIOBUTTON_BORDER_FOCUS);
		} else {
			g2.setColor(Colors.RADIOBUTTON_BORDER);
		}
	}
	g2.setStroke(RADIO_STROKE);
	circle = new Ellipse2D.Double(1, 1, 14, 14);
	g2.draw(circle);

	// drawing sphere
	if (isSelected) {
		if (isEnabled) {
			g2.setColor(Colors.RADIOBUTTON_CHECKED);
		} else {
			g2.setColor(Colors.RADIOBUTTON_CHECKED_DISABLED);
		}
		circle = new Ellipse2D.Double(4, 4, 9, 9);
		g2.fill(circle);
	}

	g.translate(-x, -y);
}
 
Example 17
Project: openjdk-jdk10   File: TextClipErrorTest.java   View Source Code Vote up 4 votes
public static void main(String[] args) {
    Locale.setDefault(Locale.US);

    // initialize j.u.l Looger:
    final Logger log = Logger.getLogger("sun.java2d.marlin");
    log.addHandler(new Handler() {
        @Override
        public void publish(LogRecord record) {
            Throwable th = record.getThrown();
            // detect any Throwable:
            if (th != null) {
                System.out.println("Test failed:\n" + record.getMessage());
                th.printStackTrace(System.out);

                throw new RuntimeException("Test failed: ", th);
            }
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    });

    log.info("TextClipErrorTest: start");

    // enable Marlin logging & internal checks:
    System.setProperty("sun.java2d.renderer.log", "true");
    System.setProperty("sun.java2d.renderer.useLogger", "true");
    System.setProperty("sun.java2d.renderer.doChecks", "true");

    BufferedImage image = new BufferedImage(256, 256,
                                            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = image.createGraphics();
    g2d.setColor(Color.red);
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        Font font = g2d.getFont();
        FontRenderContext frc = new FontRenderContext(
            new AffineTransform(), true, true);

        g2d.setStroke(new BasicStroke(4.0f,
                                      BasicStroke.CAP_ROUND,
                                      BasicStroke.JOIN_ROUND));

        final Shape badShape;
        if (SERIALIZE) {
            final GlyphVector gv1 = font.createGlyphVector(frc, "\u00d6");
            final Shape textShape = gv1.getOutline();

            final AffineTransform at1 = AffineTransform.getTranslateInstance(
                -2091202.554154681, 5548.601436981691);
            badShape = at1.createTransformedShape(textShape);
            serializeShape(badShape);
        } else {
            badShape = deserializeShape();
        }

        g2d.draw(badShape);

        // Draw anything within bounds and it fails:
        g2d.draw(new Line2D.Double(10, 20, 30, 40));

        if (SAVE_IMAGE) {
            final File file = new File("TextClipErrorTest.png");
            System.out.println("Writing file: " + file.getAbsolutePath());
            ImageIO.write(image, "PNG", file);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        g2d.dispose();
        log.info("TextClipErrorTest: end");
    }
}
 
Example 18
Project: faitic-checker   File: JCustomTextField.java   View Source Code Vote up 4 votes
@Override
public void paintComponent(Graphics g){
	
	Graphics2D g2=(Graphics2D) g;
	
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setColor(iBorderColor);
	g2.fillRoundRect(0, 0, getWidth(), getHeight(), 6, 6);
	
    g2.setColor(Color.white);
	g2.fillRoundRect(1, 1, getWidth()-2, getHeight()-2, 4, 4);
	
	super.paintComponent(g);
	
}
 
Example 19
Project: hearthstone   File: PopUp.java   View Source Code Vote up 4 votes
private void adjustGraphics(Graphics2D g) {
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
}
 
Example 20
Project: Mafia   File: Game.java   View Source Code Vote up 2 votes
private void render() {
	BufferStrategy buffer = this.getBufferStrategy();
	
	if(buffer == null) {
		this.createBufferStrategy(3);
		
		return;
	}
	
	
	Graphics2D g = (Graphics2D) buffer.getDrawGraphics();
	
	g.setRenderingHint(
	        RenderingHints.KEY_TEXT_ANTIALIASING,
	        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	
	
	////
	g.setColor(Color.BLACK);
	g.fillRect(0, 0, this.getWidth(), this.getHeight());
	
	
	g.translate(this.getWidth() / 2, this.getHeight() / 2);
	
	
	
	camera.translateToCamera(g, this);
	camera.translateCameraShake(g);
	
	map.render(g, this);
	
	camera.translateNoCameraShake(g);
	camera.translateFromCamera(g);
	
	
	gameStateManager.render(g, this);
	
	////
	
	g.dispose();
	buffer.show();
}