Solved

How can i clear a circle with Graphics2D.

Posted on 2009-04-02
8
538 Views
Last Modified: 2013-12-29
I have a BufferedImage and its Graphics2D. I want to clear a circle out of it, basically make the circle transparent and also have the cleared circles border be properly antialiased. I have used a Graphics2D alot but i dont know how this can be done.
0
Comment
Question by:Thomas4019
  • 4
  • 3
8 Comments
 
LVL 17

Author Comment

by:Thomas4019
ID: 24055603
Note, i would like to do something like fillOval() or fillShape(new Ellipse2D() but if i set the color to transparent then when it draws the oval it has no effect. Would alpha premultiplying help me and how could this be done.
0
 
LVL 92

Expert Comment

by:objects
ID: 24055640
you cannot clear it, you need to paint over it
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24060141
I've coded you a function which will produce a buffered image of a specific colour, with a circle of a specified radius missing from the centre (it's anti-aliased too). Just call it, and then render the returned BufferedImage on top of your own when necessary. I've attached an example image of the result.

Note: if you adjust the 'dg' variable (>0) in the method, it will change the magnitude of the blurring/softening around the edge. I think dg=2 is good enough though.


		/* r - red component

		 * g - green

		 * b - blue

		 * w - width of image

		 * h - height of image

		 * rad - radius of image

		 */

		public BufferedImage circleThing(int r, int g, int b, int w, int h, int rad)

		{

			if(2*rad>Math.min(w,h)) return null;

			

			BufferedImage bimg=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);

			Graphics2D G=(Graphics2D)bimg.getGraphics();

			

			G.setColor(new Color(r,g,b,255));

			

			G.fillRect(0,0,w,(h>>1)-rad);

			G.fillRect(0,(h>>1)-rad,(w>>1)-rad,2*rad);

			G.fillRect((w>>1)+rad,(h>>1)-rad,(w>>1)-rad,2*rad);

			G.fillRect(0,(h>>1)+rad,w,(h>>1)-rad);

			

			double dg=2;

			for(int i=-rad; i<=rad; i++)

				for(int j=-rad; j<=rad; j++)

				{

					double d=Math.sqrt(i*i+j*j);

					if(d>rad)

					{

						G.setColor(new Color(r,g,b,255));

						G.fillRect((w>>1)+i,(h>>1)+j,1,1);

					}else if(Math.abs(d-rad)<dg)

					{

						double gap=Math.abs(d-rad);

						int op=(int)((1.0-gap/dg)*255.0);

						G.setColor(new Color(r,g,b,op));

						G.fillRect((w>>1)+i,(h>>1)+j,1,1);

					}

				}

			

			G.dispose();

			return bimg;

		}

Open in new window

example.jpg
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
ID: 24062446
Actually, dg=1 is more like it.
Here's an example of use (just me showing off a bit).
import javax.swing.*;

import java.awt.*;

import java.awt.image.*;
 

public class Example extends JFrame

{

	public Example()

	{

		super("Example");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		add(new Display(500,400));

		pack();

		setLocationRelativeTo(null);

		setVisible(true);

	}

	

	public static void main(String[]a)

	{

		new Example();

	}

	

	class Display extends JComponent implements Runnable

	{

		private int w,h;

		private Image img=null;

		private int t=0;

		private double p=0;

		

		public Display(int w, int h)

		{

			this.w=w;

			this.h=h;

			setPreferredSize(new Dimension(w,h));

			img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);

			Graphics g=img.getGraphics();

			g.setColor(new Color(0xEFEFEF));

			g.fillRect(0,0,w,h);

			g.setColor(new Color(0x888888));

			int m=10;

			for(int j=0; j<h/m; j++)

			{

				for(int i=0; i<w/m; i++)

				{

					if((i+j)%2==0)

					{

						g.fillRect(i*m,j*m,m,m);

					}

				}

			}

			g.dispose();

			

			new Thread(this).start();

		}

		

		public void run()

		{

			for(;;)

			{

				t+=2;

				t%=w;

				p+=.05;

				p%=2*Math.PI;

				repaint();

				try{Thread.sleep(20);}catch(Exception e){}

			}

		}

		

		public void paint(Graphics gg)

		{

			Graphics2D g=(Graphics2D)gg;

			

			g.drawImage(img,t,0,w,h,null);

			g.drawImage(img,t-w,0,w,h,null);

			

			g.drawImage(circleThing(255,255,255,w,h,(int)(100+50*Math.cos(p))),0,0,null);

		}

		

		public BufferedImage circleThing(int r, int g, int b, int w, int h, int rad)

		{

			if(2*rad>Math.min(w,h)) return null;

			

			BufferedImage bimg=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);

			Graphics2D G=(Graphics2D)bimg.getGraphics();

			

			G.setColor(new Color(r,g,b,255));

			

			G.fillRect(0,0,w,(h>>1)-rad);

			G.fillRect(0,(h>>1)-rad,(w>>1)-rad,2*rad);

			G.fillRect((w>>1)+rad,(h>>1)-rad,(w>>1)-rad,2*rad);

			G.fillRect(0,(h>>1)+rad,w,(h>>1)-rad);

			

			double dg=1;

			for(int i=-rad; i<=rad; i++)

				for(int j=-rad; j<=rad; j++)

				{

					double d=Math.sqrt(i*i+j*j);

					if(d>rad)

					{

						G.setColor(new Color(r,g,b,255));

						G.fillRect((w>>1)+i,(h>>1)+j,1,1);

					}else if(Math.abs(d-rad)<dg)

					{

						double gap=Math.abs(d-rad);

						int op=(int)((1.0-gap/dg)*255.0);

						G.setColor(new Color(r,g,b,op));

						G.fillRect((w>>1)+i,(h>>1)+j,1,1);

					}

				}

			

			G.dispose();

			

			return bimg;

		}

	}

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Author Closing Comment

by:Thomas4019
ID: 31566052
Awesome code, very cool. This is better than i was even asking for. Customizable feathering!  Thanks.
0
 
LVL 17

Author Comment

by:Thomas4019
ID: 24066046
Wow, that second code really is impressive.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 25083337
I just discovered that it's possible to perform clipping with Graphics2D, so thought I'd add this link here:
http://www.exampledepot.com/egs/java.awt/ClipShape.html?l=rel

(Unfortunately however, it does not seem to support antialiasing)
0
 
LVL 17

Author Comment

by:Thomas4019
ID: 25083536
Interesting, clipping would be useful. The Shape class has always mystified me. I understand how to use it. But how does the Java Graphics methods and others actually use the Shape? The only method that Graphics2D could use to efficiently draw Shapes would be Shape's getPathIterator() unless each method uses instanceof to figure out which Shape it is.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 126
firstswap challenge 20 67
servlet  URL Rewriting 1 27
factorial example 4 35
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now