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

The following are Jave code examples for showing how to use setRenderingHints() 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: incubator-netbeans   File: ListCompletionView.java   View Source Code Vote up 6 votes
public @Override void paint(Graphics g) {
    Object value = (Map)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
    Map renderingHints = (value instanceof Map) ? (java.util.Map)value : null;
    if (renderingHints != null && g instanceof Graphics2D) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints oldHints = g2d.getRenderingHints();
        g2d.setRenderingHints(renderingHints);
        try {
            super.paint(g2d);
        } finally {
            g2d.setRenderingHints(oldHints);
        }
    } else {
        super.paint(g);
    }
}
 
Example 2
Project: COE1186   File: TimerBasedAnimation.java   View Source Code Vote up 6 votes
public void paint(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;

  RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);

  rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

  g2.setRenderingHints(rh);
  Dimension size = getSize();

  if (initialize) {
    reset(size.width, size.height);
    initialize = false;
  }
  this.step(size.width, size.height);
  render(size.width, size.height, g2);
}
 
Example 3
Project: incubator-netbeans   File: ResultBar.java   View Source Code Vote up 6 votes
public
@Override
void paint(Graphics g) {
    // Antialiasing if necessary
    Object value = (Map) (Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
    Map renderingHints = (value instanceof Map) ? (java.util.Map) value : null;
    if (renderingHints != null && g instanceof Graphics2D) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints oldHints = g2d.getRenderingHints();
        g2d.setRenderingHints(renderingHints);
        try {
            super.paint(g2d);
        } finally {
            g2d.setRenderingHints(oldHints);
        }
    } else {
        super.paint(g);
    }
}
 
Example 4
Project: incubator-netbeans   File: ListCompletionView.java   View Source Code Vote up 6 votes
public @Override void paint(Graphics g) {
    Object value = Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints"); //NOI18N
    Map renderingHints = (value instanceof Map) ? (java.util.Map)value : null;
    if (renderingHints != null && g instanceof Graphics2D) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints oldHints = g2d.getRenderingHints();
        g2d.setRenderingHints(renderingHints);
        try {
            super.paint(g2d);
        } finally {
            g2d.setRenderingHints(oldHints);
        }
    } else {
        super.paint(g);
    }
}
 
Example 5
Project: incubator-netbeans   File: CoverageBar.java   View Source Code Vote up 6 votes
public
@Override
void paint(Graphics g) {
    // Antialiasing if necessary
    Object value = (Map) (Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
    Map renderingHints = (value instanceof Map) ? (java.util.Map) value : null;
    if (renderingHints != null && g instanceof Graphics2D) {
        Graphics2D g2d = (Graphics2D) g;
        RenderingHints oldHints = g2d.getRenderingHints();
        g2d.setRenderingHints(renderingHints);
        try {
            super.paint(g2d);
        } finally {
            g2d.setRenderingHints(oldHints);
        }
    } else {
        super.paint(g);
    }
}
 
Example 6
Project: rapidminer   File: AnnotationDrawer.java   View Source Code Vote up 6 votes
/**
 * Creates an image of the given annotation and caches it with the specified cache id.
 *
 * @param anno
 *            the annotation to cache
 * @param cacheId
 *            the cache id for the given annotation
 * @return the cached image
 */
private Image cacheAnnotationImage(final WorkflowAnnotation anno, final int cacheId) {
	Rectangle2D loc = anno.getLocation();
	// paint each annotation with the same JEditorPane
	Dimension size = new Dimension((int) loc.getWidth(), (int) loc.getHeight());
	pane.setSize(size);
	pane.setText(AnnotationDrawUtils.createStyledCommentString(anno));
	pane.setCaretPosition(0);
	// draw annotation area to image and then to graphics
	// otherwise heavyweight JEdiorPane draws over everything and outside of panel
	BufferedImage img = new BufferedImage((int) loc.getWidth(), (int) loc.getHeight(), BufferedImage.TYPE_INT_ARGB);
	Graphics2D gImg = img.createGraphics();
	gImg.setRenderingHints(ProcessDrawer.HI_QUALITY_HINTS);
	// without this, the text is pixelated on half opaque backgrounds
	gImg.setComposite(AlphaComposite.SrcOver);
	// paint JEditorPane to image
	pane.paint(gImg);
	displayCache.put(anno.getId(), new SoftReference<Image>(img));
	cachedID.put(anno.getId(), cacheId);

	return img;
}
 
Example 7
Project: rapidminer   File: AbstractPackageDescriptorListCellRenderer.java   View Source Code Vote up 6 votes
protected Icon getResizedIcon(Icon originalIcon) {
    if(originalIcon == null) {
        return null;
    } else {
        int width = originalIcon.getIconWidth();
        int height = originalIcon.getIconHeight();
        if(width != 48) {
            double scale = 48.0D / (double)width;
            BufferedImage bi = new BufferedImage((int)(scale * (double)width), (int)(scale * (double)height), 2);
            Graphics2D g = bi.createGraphics();
            g.setRenderingHints(HI_QUALITY_HINTS);
            g.scale(scale, scale);
            originalIcon.paintIcon((Component)null, g, 0, 0);
            g.dispose();
            return new ImageIcon(bi);
        } else {
            return originalIcon;
        }
    }
}
 
Example 8
Project: Moenagade   File: LibraryPanel.java   View Source Code Vote up 6 votes
@Override
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g;
    RenderingHints rh = new RenderingHints(
             RenderingHints.KEY_TEXT_ANTIALIASING,
             RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHints(rh);

    g.setColor(BloxsColors.$BACKGROUND);
    g.fillRect(0, 0, getWidth(), getHeight());
    
    for (int i = 0; i < elements.size(); i++) {
        Element get = elements.get(i);
        get.draw((Graphics2D) g);
    }
}
 
Example 9
Project: OpenJSharp   File: AffineTransformOp.java   View Source Code Vote up 6 votes
/**
 * Transforms source image using transform specified at the constructor.
 * The resulting transformed image is stored in the destination image if one
 * is provided; otherwise a new BufferedImage is created and returned. 
 *
 * @param src source image
 * @param dst destination image
 * @throws IllegalArgumentException if the source and destination image are
 *          the same
 * @return transformed source image.
 */
public final BufferedImage filter (BufferedImage src, BufferedImage dst)
{
  if (dst == src)
    throw new IllegalArgumentException("src image cannot be the same as "
                                     + "the dst image");

  // If the destination image is null, then use a compatible BufferedImage
  if (dst == null)
    dst = createCompatibleDestImage(src, null);

  Graphics2D gr = dst.createGraphics();
  gr.setRenderingHints(hints);
  gr.drawImage(src, transform, null);
  return dst;
}
 
Example 10
Project: incubator-netbeans   File: ProfilerXYSelectionOverlay.java   View Source Code Vote up 5 votes
public void paint(Graphics g) {
    if (selectedValues.isEmpty()) return;

    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHints(chart.getRenderingHints());

    Iterator<Point> it = selectedValues.iterator();
    boolean linePainted = false;

    while (it.hasNext()) {
        Point p = it.next();

        if (!linePainted) {
            g2.setPaint(evenPerfPaint);
            g2.setStroke(evenPerfStroke);
            g2.drawLine(p.x, 0, p.x, getHeight());
            g2.setPaint(oddPerfPaint);
            g2.setStroke(oddPerfStroke);
            g2.drawLine(p.x, 0, p.x, getHeight());

            g2.setPaint(markPaint);
            g2.setStroke(markStroke);

            linePainted = true;
        }

        g2.fillOval(p.x - selectionExtent + 1, p.y - selectionExtent + 1,
                    selectionExtent * 2 - 1, selectionExtent * 2 - 1);
    }

}
 
Example 11
Project: rapidminer   File: RoundTitledBorder.java   View Source Code Vote up 5 votes
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
	Graphics2D g2d = (Graphics2D) g.create();
	g2d.setRenderingHints(ProcessDrawer.HI_QUALITY_HINTS);
	g2d.setStroke(new BasicStroke(2f));

	// clear edges, otherwise they will be in the color of the component background
	if (drawRoundFrame && !c.getBackground().equals(c.getParent().getBackground())) {
		Shape frame = new Rectangle2D.Float(x + 2, y + 2, width - 4, height - 4);
		g2d.setPaint(c.getParent().getBackground());
		g2d.draw(frame);
	}

	g2d.setPaint(paint);
	g2d.setFont(new Font("Dialog", Font.BOLD, 21));

	if (drawRoundFrame) {
		Shape roundFrame = new RoundRectangle2D.Float(x + 2, y + 2, width - 4, height - 4, 10, 10);
		g2d.draw(roundFrame);
	}

	if (number > 0) {
		Shape circle = new Ellipse2D.Float(20, 20, 34, 34);
		g2d.fill(circle);
		g2d.setPaint(Color.WHITE);
		g2d.drawString(String.valueOf(number), 29, 44);
	}

	if (key != null) {
		g2d.setPaint(paint);
		g2d.drawString(key, 60, 44);
	}

	g2d.dispose();
}
 
Example 12
Project: rapidminer   File: OverviewPanel.java   View Source Code Vote up 5 votes
@Override
protected void paintComponent(Graphics graphics) {
	super.paintComponent(graphics);
	double scaleX = (double) getWidth() / (double) processRenderer.getWidth();
	double scaleY = (double) getHeight() / (double) processRenderer.getHeight();
	scale = Math.min(scaleX, scaleY);
	double scaledW = processRenderer.getWidth() * scale;
	double scaledH = processRenderer.getHeight() * scale;

	Graphics2D g = (Graphics2D) graphics.create();
	g.translate((getWidth() - scaledW) / 2d, (getHeight() - scaledH) / 2d);
	g.scale(scale, scale);

	g.setRenderingHints(ProcessDrawer.LOW_QUALITY_HINTS);
	processRenderer.getOverviewPanelDrawer().draw(g, true);

	g.setStroke(new BasicStroke((int) (1d / scale)));

	Rectangle visibleRect = processRenderer.getVisibleRect();
	Rectangle drawRect = new Rectangle((int) visibleRect.getX(), (int) visibleRect.getY(),
			(int) visibleRect.getWidth() - 1, (int) visibleRect.getHeight() - 1);

	g.setColor(FILL_COLOR);
	g.fill(drawRect);

	g.setColor(DRAW_COLOR);
	g.draw(drawRect);

	g.dispose();
}
 
Example 13
Project: febit   File: GradientBackgroundFilter.java   View Source Code Vote up 5 votes
@Override
public BufferedImage render(final BufferedImage baseImage) {

    int width = baseImage.getWidth();
    int height = baseImage.getHeight();

    // create an opaque image
    RenderingHints hints = new RenderingHints(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_OFF);

    hints.add(new RenderingHints(RenderingHints.KEY_COLOR_RENDERING,
            RenderingHints.VALUE_COLOR_RENDER_QUALITY));
    hints.add(new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY));

    hints.add(new RenderingHints(RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY));

    BufferedImage imageWithBackground = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);

    Graphics2D graph = (Graphics2D) imageWithBackground.getGraphics();
    graph.setRenderingHints(hints);
    graph.setPaint(new GradientPaint(0, 0, colorFrom, width, height, colorTo));
    graph.fill(new Rectangle2D.Double(0, 0, width, height));
    graph.drawImage(baseImage, 0, 0, null);
    graph.dispose();

    return imageWithBackground;
}
 
Example 14
Project: What-Happened-to-Station-7   File: TStack.java   View Source Code Vote up 5 votes
public void apply(Graphics2D g)
{
	g.setTransform(transform);
	g.setPaint(paint);
	g.setStroke(stroke);
	g.setFont(font);
	g.setComposite(composite);
	g.setBackground(bgrnd);
	g.setClip(shape);
	g.setRenderingHints(hints);
}
 
Example 15
Project: OpenJSharp   File: ColorConvertOp.java   View Source Code Vote up 5 votes
/**
 * Copy a source image to a destination image, respecting their colorspaces 
 * and performing colorspace conversions if necessary.  
 * 
 * @param src The source image.
 * @param dst The destination image.
 */
private void copyimage(BufferedImage src, BufferedImage dst)
{
  // This is done using Graphics2D in order to respect the rendering hints.
  Graphics2D gg = dst.createGraphics();
  
  // If no hints are set there is no need to call
  // setRenderingHints on the Graphics2D object.
  if (hints != null)
    gg.setRenderingHints(hints);
  
  gg.drawImage(src, 0, 0, null);
  gg.dispose();
}
 
Example 16
Project: Tarski   File: mxGraphComponent.java   View Source Code Vote up 5 votes
/**
 * 
 */
public void paintComponent(Graphics g) {
  super.paintComponent(g);

  // Draws the background
  paintBackground(g);

  // Creates or destroys the triple buffer as needed
  if (tripleBuffered) {
    checkTripleBuffer();
  } else if (tripleBuffer != null) {
    destroyTripleBuffer();
  }

  // Paints the buffer in the canvas onto the dirty region
  if (tripleBuffer != null) {
    mxUtils.drawImageClip(g, tripleBuffer, this);
  }

  // Paints the graph directly onto the graphics
  else {
    Graphics2D g2 = (Graphics2D) g;
    RenderingHints tmp = g2.getRenderingHints();

    // Sets the graphics in the canvas
    try {
      mxUtils.setAntiAlias(g2, antiAlias, textAntiAlias);
      drawGraph(g2, true);
    } finally {
      // Restores the graphics state
      g2.setRenderingHints(tmp);
    }
  }

  eventSource.fireEvent(new mxEventObject(mxEvent.PAINT, "g", g));
}
 
Example 17
Project: febit   File: DefaultWordRender.java   View Source Code Vote up 4 votes
@Override
public void render(BufferedImage image, String word) {
    final int width = image.getWidth();
    final int height = image.getHeight();
    Graphics2D graphics2D = image.createGraphics();

    RenderingHints hints = new RenderingHints(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    hints.add(new RenderingHints(RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY));
    graphics2D.setRenderingHints(hints);

    char[] wordChars = word.toCharArray();
    int wordLen = wordChars.length;

    Font[] chosenFonts = new Font[wordLen];
    int[] charWidths = new int[wordLen];
    int widthNeeded = 0;

    int maxFontSize = 0;

    for (int i = 0; i < wordLen; i++) {
        Font font = RandomUtil.rnd(fonts);

        if (font.getSize() > maxFontSize) {
            maxFontSize = font.getSize();
        }

        FontMetrics fontMetrics = graphics2D.getFontMetrics(font);
        int charWidth = fontMetrics.charWidth(wordChars[i]);
        widthNeeded = widthNeeded + charWidth;
        chosenFonts[i] = font;
        charWidths[i] = charWidth;
    }
    widthNeeded += charSpace * (wordLen - 1);

    int startPosX = (width - widthNeeded) / 2;
    int startPosY = (height - maxFontSize) / 5 + maxFontSize;

    for (int i = 0; i < wordLen; i++) {
        graphics2D.setFont(chosenFonts[i]);
        graphics2D.setColor(RandomUtil.rnd(colors));
        graphics2D.drawString(String.valueOf(wordChars[i]), startPosX, startPosY);
        startPosX = startPosX + charWidths[i] + charSpace;
    }
}
 
Example 18
Project: febit   File: CubicCurveNoiseFilter.java   View Source Code Vote up 4 votes
@Override
public BufferedImage render(BufferedImage image) {

    final int width = image.getWidth();
    final int height = image.getHeight();

    final PathIterator pi = new CubicCurve2D.Float(factor1 * width, height
            * RandomUtil.nextFloat(), factor2 * width, height
            * RandomUtil.nextFloat(), factor3 * width, height
            * RandomUtil.nextFloat(), factor4 * width, height
            * RandomUtil.nextFloat())
            .getPathIterator(null, 2);

    final int[] pts = new int[200];
    int i = 0;

    float[] coords = new float[6];
    while (!pi.isDone() && i < 200) {
        int seg = pi.currentSegment(coords);
        if (seg == PathIterator.SEG_MOVETO || seg == PathIterator.SEG_LINETO) {
            pts[i++] = (int) coords[0];
            pts[i++] = (int) coords[1];
        }
        pi.next();
    }

    final Graphics2D graph = (Graphics2D) image.getGraphics();
    graph.setRenderingHints(new RenderingHints(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON));

    graph.setColor(color);

    i -= 2;
    int j = 0;
    while (j < i) {
        graph.drawLine(pts[j++], pts[j++], pts[j], pts[j + 1]);
    }

    graph.dispose();
    return image;
}
 
Example 19
Project: TrabalhoFinalEDA2   File: mxGraphComponent.java   View Source Code Vote up 4 votes
/**
 * 
 */
public void paintComponent(Graphics g)
{
	super.paintComponent(g);

	// Draws the background
	paintBackground(g);

	// Creates or destroys the triple buffer as needed
	if (tripleBuffered)
	{
		checkTripleBuffer();
	}
	else if (tripleBuffer != null)
	{
		destroyTripleBuffer();
	}

	// Paints the buffer in the canvas onto the dirty region
	if (tripleBuffer != null)
	{
		mxUtils.drawImageClip(g, tripleBuffer, this);
	}

	// Paints the graph directly onto the graphics
	else
	{
		Graphics2D g2 = (Graphics2D) g;
		RenderingHints tmp = g2.getRenderingHints();

		// Sets the graphics in the canvas
		try
		{
			mxUtils.setAntiAlias(g2, antiAlias, textAntiAlias);
			drawGraph(g2, true);
		}
		finally
		{
			// Restores the graphics state
			g2.setRenderingHints(tmp);
		}
	}

	eventSource.fireEvent(new mxEventObject(mxEvent.PAINT, "g", g));
}
 
Example 20
Project: geomapapp   File: D18oObservations.java   View Source Code Vote up 4 votes
public void draw(Graphics2D g) {
//	if(!display) return;
int yr1 = 1900;
int yr2 = 2005;
try {
	long[] interval = timeInterval();
	Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
	if( db.before.isSelected() ) {
			cal.setTimeInMillis(interval[0]);
			yr1 = cal.get(cal.YEAR);
	}
	if( db.after.isSelected() ) {
		cal.setTimeInMillis(interval[1]);
		yr2 = cal.get(cal.YEAR);
	}
} catch(Exception e) {
}
//	System.out.println( yr1 +"\t"+ yr2);
	float size = 2.0f + 6f/(float)map.getZoom();
	Arc2D.Float dot = new Arc2D.Float(-size/2f, -size/2f, size, size, 0f, 360f, Arc2D.CHORD);
	float size1 = 2.0f + 7f/(float)map.getZoom();
	GeneralPath triangle = new GeneralPath();
	triangle.moveTo(0f, size1/2f );
	triangle.lineTo(size1/2f, -size1/2f );
	triangle.lineTo(-size1/2f, -size1/2f );
	triangle.closePath();
//	Rectangle2D.Float square = new Rectangle2D.Float(-size1/2f, -size1/2f, size1, size1);
	AffineTransform at = g.getTransform();
	g.setStroke( new BasicStroke( .5f/(float)map.getZoom() ) );
	RenderingHints hints = g.getRenderingHints();
	g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	g.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);

	Shape shape = dot;
	for(int k=0 ; k<xyd.length ; k++) {
	//	Shape shape = triangle;
	//	if( xyd[k][5]<=1991f ) shape = dot;
		if( xyd[k][5]>yr2 )continue;
		if( xyd[k][5]<yr1 )continue;
		g.translate( (double)xyd[k][0], (double)xyd[k][1] );
	//	g.setColor( Color.black );
	//	g.draw(shape);
		g.setColor( color[k] );
		g.fill(shape);
		g.setColor( Color.black );
		g.draw(shape);
		g.setTransform( at );
	}
	g.setFont( (new Font("Serif", Font.BOLD, 1)).deriveFont( size1*1.5f));
	Rectangle2D rect = map.getClipRect2D();
	double s = (double) size1;
	double x = rect.getX() + s;
	double y = rect.getY() + 2.*s;
//	g.translate( x, y );
//	g.setColor( Color.white );
//	g.fill( dot );
//	g.setColor( Color.black );
//	g.draw( dot );
//	g.translate( s, s/2.);
//	String year = db.startF.getText();
//	String dateString = db.before.isSelected()
//		? "before 1/1/"+year
//		: "after 1/1/"+year;
//	g.drawString( dateString, 0, 0);
//	g.drawString( "before 1/1/1991", 0, 0);
//	g.setTransform(at);
//	g.translate( x, y+s*2.0 );
//	g.setColor( Color.white );
//	g.fill( triangle );
//	g.setColor( Color.black );
//	g.draw( triangle );
//	g.translate( s, s/2.);
//	g.drawString( "after 1/1/1991", 0, 0);
	g.setTransform(at);
	g.setRenderingHints( hints);
}