The technical term for this type of control is "CAPTCHA" (An acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart"). Just Google "Java CAPTCHA" and you'll get tons of information on it. There are several open source as well as commercial CAPTCHA tools out there.
You could take a look at reCAPTCHA, it's a Carnegie Mellon University project that helps digitize old books as a side-effect. It's a web service though.