codes from http://www.javaexamples.org