Implementation of “Image Mapping” technique in my project

Image mapping is frequently used in many cases where an image needs to be processed under certain rules. In case of designing a breadboard (I’m designing a digital logic circuit simulator in JAVA) I used a previously made image. Then using image mapping I made the program to detect each and every hole of the breadboard. So the code is given below.

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;
public class BreadBoard extends JFrame

{
private static JLabel statusText;

private ImageIcon breadBoard;

private int upperPanel[][];

private int lowerPanel[][];

private int midPanel1[][];

private int midPanel2[][];
public BreadBoard()

{

super("Checking Breadboard");

setLayout(new BorderLayout());
BreadPanel breadPanel=new BreadPanel();

add(breadPanel,BorderLayout.CENTER);
statusText=new JLabel("This is status bar");

add(statusText, BorderLayout.SOUTH);
upperPanel=new int [4][30];

lowerPanel= new int [4][30];

midPanel1=new int [60][10];

midPanel2=new int [60][10];
pointInitiator();
MouseHandler handler=new MouseHandler();

breadPanel.addMouseListener(handler);
}
public void pointInitiator()

{

int init=30;

int end=42;

//assining x values for upper and lower panel

for(int i=0; i<30 ;i++ )

{

upperPanel[0][i]=init;

upperPanel[1][i]=end;

lowerPanel[0][i]=init;

lowerPanel[1][i]=end;

init=init+32;

end=end+32;

}
//assihning y values for upper and lower panel

init=30;

end=42;

for(int i=0; i<30; i++)

{

upperPanel[2][i]=init;

upperPanel[3][i]=end;

lowerPanel[2][i]=init+472; //adding 472 to access the lower panel

lowerPanel[3][i]=end+472;

}
//assigning values to mid panel1

//asssigning y values

//the whole composition has been designed as per the arrangements in breadboard
init=102; //initial avalue for y co-ordiante

end= 114; //end value for y co-ordinate
for(int i=1; i<60;)

{

init=102;

end=114;

for(int j=0; j<10;)

{

midPanel1[i][j]=init;

midPanel1[i][j+1]=end;

init=init+32;

end=end+32;

j=j+2;

}
i=i+2;

}
//assigning x values

init=30;

end=42;
for(int i=0; i<=58;)

{
for(int j=0; j<=8;)

{

midPanel1[i][j]=init;

midPanel1[i][j+1]=end;

j=j+2;

}

init=init+32;

end=end+32;

i=i+2;

}
//priniting the array for checking the input strategy

/*  for(int i=0; i<60; i++)

{

for(int j=0; j<10; j++)

System.out.printf("%d ", midPanel1[i][j]);

System.out.println();

}

*/
//assigning values to mid panel2

//asssigning y values

//the whole composition has been designed as per the arrangements in breadboard
init=302; //initial avalue for y co-ordiante

end= 314; //end value for y co-ordinate
for(int i=1; i<60;)

{

init=302;

end=314;

for(int j=0; j<10;)

{

midPanel2[i][j]=init;

midPanel2[i][j+1]=end;

init=init+32;

end=end+32;

j=j+2;

}
i=i+2;

}
//assigning x values

init=30;

end=42;
for(int i=0; i<=58;)

{
for(int j=0; j<=8;)

{

midPanel2[i][j]=init;

midPanel2[i][j+1]=end;

j=j+2;

}

init=init+32;

end=end+32;

i=i+2;

}
//priniting the array for checking the input strategy

/*  for(int i=0; i<60; i++)

{

for(int j=0; j<10; j++)

System.out.printf("%d ", midPanel1[i][j]);

System.out.println();

}

*/
}
private class MouseHandler extends MouseAdapter

{

@Override

public void mouseClicked(MouseEvent event)

{

int x, y;

x=event.getX();

y=event.getY();
//checking upper panel

if(y>30&&y<42)

{

for(int i=0; i<30; i++)

{

if(x>=upperPanel[0][i]&&x<=upperPanel[1][i])

{

JOptionPane.showMessageDialog(new BreadBoard(), String.format("found"));

break;

}
}

}
//checking lower panel
if(y>502&&y<514)

{

for(int i=0; i<30; i++)

{

if(x>=lowerPanel[0][i]&&x<=lowerPanel[1][i])

{

JOptionPane.showMessageDialog(new BreadBoard(), String.format("found"));

break;

}
}

}
//checking mid panel 1 and mid panel 2
for(int i=0; i<60; )

{

for(int j=0; j<10; )

{

if(x>midPanel1[i][j]&&x<midPanel1[i][j+1])

{

if(y>midPanel1[i+1][j]&&y<midPanel1[i+1][j+1])

{

JOptionPane.showMessageDialog(new BreadBoard(), String.format("found"));

break;

}

}
if (x>midPanel2[i][j]&&x<midPanel2[i][j+1])

{

if(y>midPanel2[i+1][j]&&y<midPanel2[i+1][j+1])

{

JOptionPane.showMessageDialog(new BreadBoard(), String.format("found"));

break;

}
}

j=j+2;

}

i=i+2;

}
//checking mid panel2
}

@Override

public void mouseExited(MouseEvent event)

{

statusText.setText("Mouse outside panel");

}
@Override

public void mouseEntered(MouseEvent event)

{

statusText.setText("Mouse inside panel");
}

}
//the original breadboard
private class BreadPanel extends JPanel

{
public BreadPanel()

{

breadBoard=new ImageIcon("E:/JPG’s/breadboard final copy.png");

}
@Override

public void paintComponent(Graphics g)

{

super.paintComponent(g);

breadBoard.paintIcon(this, g, 0, 0);

}

}
}

here the main class is name “class BreadBoard“.  A method named point initiator() initiates all the co-ordinates necessary for recognizing the points in the breadboard image. As the image is 1000X544 pixels in dimensions and every hole is exactly 32 pixels far from each other, I used 4 different arrays to define the four layers of the breadboard.  And I used inner class BreadPanel to load the image of the breadboard. And the rest is done by Mouse Adaptors. So, here’s the code that executes the application.

<pre>import javax.swing.JFrame;

public class BreadBoardExec

{

public static void main(String args[])

{

BreadBoard breadBoard=new BreadBoard();

breadBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

breadBoard.setSize(1020, 600); breadBoard.setVisible(true);

}

}

and here’s the screen shot of this program.


Screenshot

Screenshot of the program

This porgram shows a dialogue box with a string in it named “found” every time user

clicks on  a hole by mouse. A lot more details n works are coming afterward……Promise.