Java 8 Graphics - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Graphics class.

This code example shows how to use the following methods:create, dispose, setColor, draw, setStroke
		this.width = width;
		this.color = color;
	} 
 
	public void draw (BufferedImage image, Graphics2D g, UnicodeFont unicodeFont, Glyph glyph) {
		g = (Graphics2D)g.create();
		if (stroke != null)
			g.setStroke(stroke);
		else 
			g.setStroke(getStroke());
		g.setColor(color);
		g.draw(glyph.getShape());
		g.dispose();
	} 
 
	public float getWidth () { 
		return width;
	} 
 
	/** Sets the width of the outline. The glyphs will need padding so the outline doesn't get clipped. */ 
8
CodeRank
This code example shows how to use the following methods:dispose, drawImage, fillRect, setColor, setComposite
   Color shadowColor = Color.black;
   BufferedImage createShadowMask(BufferedImage image){ 
      BufferedImage mask = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); 
 
      Graphics2D g2d = mask.createGraphics(); 
      g2d.drawImage(image, 0, 0, null); 
      // Ar = As*Ad - Cr = Cs*Ad -> extract 'Ad'  
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, shadowOpacity)); 
      g2d.setColor(shadowColor); 
      g2d.fillRect(0, 0, image.getWidth(), image.getHeight()); 
      g2d.dispose(); 
      return mask; 
   }  
 
   ConvolveOp getBlurOp(int size) {
      float[] data = new float[size * size];
      float value = 1 / (float) (size * size);
      for (int i = 0; i < data.length; i++) {
         data[i] = value;
      } 
7
CodeRank
This code example shows how to use the following methods:drawImage, setClip, setColor, clip, draw
      if (anim == null)
         return; 
       
      Ellipse2D.Double ellipse =
         new Ellipse2D.Double(0,0,w,h);
      g2d.clip(ellipse);      
      g2d.drawImage(image, 0, 0, w, h, null); 
 
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);       
       
      g2d.setClip(null);      
      g2d.setStroke(new BasicStroke(3.0F));      
      g2d.setColor(Color.white);
      g2d.draw(ellipse);
       
      if (anim != null && anim.running()){
         anim.animateStep();
         guide.repaint();
      } 
7
CodeRank
This code example shows how to use the following methods:dispose, fillRect, setColor, setComposite
    } 
 
    protected BufferedImage getImage(int w, int h) {
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gc = image.createGraphics();
        gc.setComposite(AlphaComposite.Src);
 
        gc.setColor(new Color(0x00000000, true));
        gc.fillRect(0, 0, w, h);
 
        // done 
        gc.dispose();
 
        return image;
    } 
 
} 
7
CodeRank
This code example shows how to use the following methods:drawRect, setColor
 
      if (editable){
 
         if (true){ 
            Rectangle r = getActualBounds();
            g2d.setColor(getForeground());
            g2d.drawRect(0,0,r.width-1,r.height-1);
            g2d.setColor(Color.white);
            g2d.drawRect(1,1,r.width-3,r.height-3);
            g2d.setColor(getForeground());
            g2d.drawRect(2,2,r.width-5,r.height-5);
            g2d.setColor(Color.white);
            g2d.drawRect(3,3,r.width-7,r.height-7);
         }else{ 
            Rectangle r = getActualBounds();
            g2d.setColor(Color.red);            
            g2d.setStroke(new BasicStroke(3f));
            g2d.drawRect(1,1,r.width-3,r.height-3);
         } 
 
7
CodeRank
This code example shows how to use the following methods:dispose, fillRect, drawRenderedImage, setPaint
This code example shows how to use the following methods:drawString, fillRect, getFontMetrics, setColor, setFont
    public void paintComponent(Graphics g) {
        Insets ins = getInsets();
        int w = getWidth()-ins.left-ins.right;
        int h = getHeight()-ins.top-ins.bottom;
        if ( m_fheight == -1 ) {
            FontMetrics fm = g.getFontMetrics(getFont());
            if ( m_valign == SwingConstants.BOTTOM )
                m_fheight = fm.getDescent();
            else if ( m_valign == SwingConstants.TOP )
                m_fheight = fm.getAscent();
        } 
        g.setColor(getBackground());
        g.fillRect(ins.left,ins.top,w,h);
         
        if ( m_text == null )
            return; 
         
        g.setFont(getFont());
        g.setColor(getForeground());
        if ( m_valign == SwingConstants.BOTTOM ) {

7
CodeRank
This code example shows how to use the following methods:dispose, fillRect, setColor, drawRenderedImage, setComposite
        ErodeAlphaFilter s = new ErodeAlphaFilter( bevel * 10, 0.75f, 0.1f );
        matte = s.filter( src, (BufferedImage)null );
 
        BufferedImage shineLayer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = shineLayer.createGraphics();
		g.setColor( new Color( shineColor ) );
        g.fillRect( 0, 0, width, height );
        g.setComposite( AlphaComposite.DstIn );
        g.drawRenderedImage( matte, null );
        g.setComposite( AlphaComposite.DstOut );
        g.translate( xOffset, yOffset );
        g.drawRenderedImage( matte, null );
		g.dispose();
        shineLayer = new GaussianFilter( radius ).filter( shineLayer, (BufferedImage)null );
        shineLayer = new RescaleFilter( 3*brightness ).filter( shineLayer, shineLayer );
 
		g = dst.createGraphics();
        g.drawRenderedImage( src, null );
        g.setComposite( new AddComposite( 1.0f ) );
        g.drawRenderedImage( shineLayer, null );
7
CodeRank
This code example shows how to use the following methods:setColor, draw, fill, setPaint, setStroke
    @Override 
    public void draw(Graphics2D g2, Point p) {
        int x = p.x - 25;
        int y = p.y - 25;
        GradientPaint gradient = new GradientPaint(x, y, Color.BLUE, x + 50, y, Color.WHITE);
        g2.setPaint(gradient);
        g2.fill(new Rectangle2D.Double(x, y, 50, 50));
        BasicStroke wideStroke = new BasicStroke(2.0f);
        g2.setColor(Color.black);
        g2.setStroke(wideStroke);
        g2.draw(new Rectangle2D.Double(x, y, 50, 50));
    } 
} 
7
CodeRank
This code example shows how to use the following methods:dispose, drawImage, fillRect, setColor, setComposite
   Color shadowColor = Color.black;
   BufferedImage createShadowMask(BufferedImage image){ 
      BufferedImage mask = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); 
 
      Graphics2D g2d = mask.createGraphics(); 
      g2d.drawImage(image, 0, 0, null); 
      // Ar = As*Ad - Cr = Cs*Ad -> extract 'Ad'  
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, shadowOpacity)); 
      g2d.setColor(shadowColor); 
      g2d.fillRect(0, 0, image.getWidth(), image.getHeight()); 
      g2d.dispose(); 
      return mask; 
   }  
 
   ConvolveOp getBlurOp(int size) {
      float[] data = new float[size * size];
      float value = 1 / (float) (size * size);
      for (int i = 0; i < data.length; i++) {
         data[i] = value;
      } 
7
CodeRank
See Code Examples for Java 8 Graphics Methods: