public enum Traffic { NORTH, SOUTH, EAST, WEST, NOTHING }
static Traffic isOn = Traffic.NOTHING;
@Override
public void run() {
while(true){
switch (isOn) {
case NORTH:
System.out.println(" north is on ");
break;
case SOUTH:
System.out.println(" south is on ");
break;
case EAST:
System.out.println(" east is on ");
break;
case WEST:
System.out.println(" west is on ");
break;
case NOTHING:
System.out.println(" default is on ");
break;
default:
System.out.println(" default is on ");
break;
}
}
}
public static void main(String[] election) throws InterruptedException {
ThreadExamples t = new ThreadExamples();
Thread th = new Thread(t);
th.start();
th.sleep(5000);
isOn = Traffic.NORTH;
th.sleep(1000);
isOn = Traffic.WEST;
}
}
How to manipulate/control a running thread from another class.
How to add a facebook share button to your blog/website
Description : add the following html tag to your website/blog and change the url
<a class="free facebook apps" shareLink id="faceBookshare" href="http://www.facebook.com/sharer.php?u=http://javabelazy.blogspot.in/" title="share this post to your facebook timeline" >Facebook like box</a>
the above html code add a social networking link to your website
This example helps you to compare two images (either jpeg or png) using pixel. first the program check whether two images are of equal breadth and width, then it compare each pixel by pixel and return the output. The program will check whether the two images given resembles each other or not. This is one of the simplest way and pixel based.
/*
* Image comparison done here is using pixels
*
*/
public class ImageComparison {
public boolean compareTwoImages(File fileOne, File fileTwo) {
Boolean isTrue = true;
try{
Image imgOne = ImageIO.read(fileOne);
Image imgTwo = ImageIO.read(fileTwo);
BufferedImage bufImgOne = ImageIO.read(fileOne);
BufferedImage bufImgTwo = ImageIO.read(fileTwo);
int imgOneHt = bufImgOne.getHeight();
int imgTwoHt = bufImgTwo.getHeight();
int imgOneWt = bufImgOne.getWidth();
int imgTwoWt = bufImgTwo.getWidth();
if(imgOneHt!=imgTwoHt ||(imgOneWt!=imgTwoWt)){
System.out.println(" size are not equal ");
isTrue = false;
}
for(int x =0; x< imgOneHt; x++ ){
for(int y =0; y <imgOneWt ; y++){
if(bufImgOne.getRGB(x, y) != bufImgTwo.getRGB(x, y) ){
System.out.println(" size are not equal ");
isTrue = false;
break;
}
}
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isTrue;
}
public static void main(String[] args) {
File f = new File("E:\\javaaptitude\\Oracle.jpeg");
File f2 = new File("E:\\javaaptitude\\Capgemini.jpeg");
ImageComparison imgComp = new ImageComparison();
System.out.println(imgComp.compareTwoImages(f, f2));
}
}
The above code is for image having same width and height, if the image height and width is different use this code
public boolean compareTwoImages(File fileOne, File fileTwo) {
Boolean isTrue = true;
try{
Image imgOne = ImageIO.read(fileOne);
Image imgTwo = ImageIO.read(fileTwo);
BufferedImage bufImgOne = ImageIO.read(fileOne);
BufferedImage bufImgTwo = ImageIO.read(fileTwo);
int imgOneHt = bufImgOne.getHeight();
int imgTwoHt = bufImgTwo.getHeight();
int imgOneWt = bufImgOne.getWidth();
int imgTwoWt = bufImgTwo.getWidth();
if(imgOneHt!=imgTwoHt ||(imgOneWt!=imgTwoWt)){
System.out.println(" size are not equal ");
isTrue = false;
}
for(int x =0; x < imgOneWt; x++ ){ //replace the loop, if needed
for(int y =0; y < imgOneHt ; y++){
if(bufImgOne.getRGB(x, y) != bufImgTwo.getRGB(x, y) ){
System.out.println(" rgb are not equal ");
isTrue = false;
break;
}
}
}
}catch (IOException e) {
e.printStackTrace();
}
return isTrue;
}
public static void main(String[] softwareEngineer) {
File OracleJava = new File("C:\\Image\FingerPrint\analysis.jpg");
File javaOracle = new File("C:\\Histogram\match\image.jpg");
ImageComparison imgComp = new ImageComparison();
System.out.println(imgComp.compareTwoImages( OracleJava , javaOracle));
}
}
from comments :-
To work the above code for different dimensional images the for loop has to replaced.
Change
the for loop to the following .... for(int x =0; x < imgOneWt; x++
){ for(int y =0; y < imgOneHt ; y++){ if(bufImgOne.getRGB(x, y) !=
bufImgTwo.getRGB(x, y) ){ System.out.println(" size are not equal ");
isTrue = false; break; } } }
Hope you had tried the above code. Read this for to know how to create a captcha image in java : java be lazy