import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random;
import java.awt.Robot;
class location
{
float lon;
float lat;
String resourcename;
location()
{
}
location(float x,float y)
{
lon=x;
lat=y;
}
}
public class NearestResource extends Canvas implements ActionListener{
Frame myframe;
TextField text,text1,text2,text3;
Label l1,l2;
Button b,b1,b3;
Font myfont;
Color textcolor;
Color circlecolor;
Random rg;
Robot r;
int x,y,i,res1,res2;
boolean flag,flag2,flag5;
List list1;
location arr[];
Panel p,p1;
String resource[],str;
float temp_lon,temp_lat;
public NearestResource() {
this("Serif", Font.PLAIN, 18, Color.pink, Color.black);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Find")
{
temp_lon=Float.valueOf((text1.getText()).trim()).floatValue();
temp_lat=Float.valueOf((text2.getText()).trim()).floatValue();
x=plotLon(temp_lon);
y=plotLat(temp_lat);
flag=true;
circlecolor=r.getPixelColor(x,y);
flag2=false;
repaint();
}
if(ae.getActionCommand()=="Search")
{
i=0;
flag2=true;
flag5=false;
str=new String(text3.getText());
repaint();
}
if(ae.getActionCommand()=="Reset")
{
flag=false;
flag2=false;
repaint();
}
}
public NearestResource(String ff, int fs, int fz, Color bg, Color fg){
try
{
setBackground(Color.WHITE);
circlecolor = bg.brighter();
textcolor = fg;
flag=false;
flag2=false;
flag5=false;
r=new Robot();
list1=new List(10,false);
myfont = new Font(ff, fs, fz);
rg = new Random();
text = new TextField("Finding a place in the Map");
myframe = new Frame("FIND THE NEAREST RESOURCE");
p=new Panel();
p1=new Panel();
l1=new Label("Longitude");
text1=new TextField("82.69");
l2=new Label("Latitude");
text2=new TextField("13.80");
b=new Button("Find");
text3=new TextField("POST OFFICE");
b1=new Button("Search");
b3=new Button("Reset");
p.add(l1);
p.add(text1);
p.add(l2);
p.add(text2);
p.add(b);
p.add(text3);
p.add(b1);
p.add(b3);
p1.add(list1);
b.addActionListener(this);
b1.addActionListener(this);
b3.addActionListener(this);
resource=new String[5];
resource[0]=new String("ATM");
resource[1]=new String("POST OFFICE");
resource[2]=new String("BUS STOP");
resource[3]=new String("PETROL BUNK");
resource[4]=new String("TEMPLE");
arr=new location[10];
Random r = new Random();
for(int i=0;i<10;i++)
{
arr[i]=new location(myRandom(20,1200),myRandom(20,900));
arr[i].resourcename=resource[r.nextInt(5)];
list1.addItem(i + " " + arr[i].resourcename);
}
myframe.add(p, BorderLayout.NORTH);
myframe.add(p1, BorderLayout.EAST);
myframe.add(this, BorderLayout.CENTER);
myframe.setSize(new Dimension(700,700));
myframe.setLocation(0,0);
myframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
repaint();
}
});
myframe.setVisible(true);
}
catch(Exception ex)
{
System.out.println("Error");
}
}
public void paint(Graphics g) {
String st = text.getText();
if (st.length() == 0) return;
if (g instanceof Graphics2D) {
Dimension cd = getSize();
Point pt = new Point(cd.width / 2, cd.height / 2);
g.setColor(Color.BLACK);
g.fillRect(20, 20, 1200, 900);
for(int i=0;i<10;i++)
{
g.setColor(Color.green);
g.fillArc((int)arr[i].lon,(int)arr[i].lat,10, 10,0, 360);
g.setColor(Color.magenta);
g.drawString(Integer.toString(i),(int)arr[i].lon,(int)arr[i].lat);
}
if(flag)
{
g.setColor(Color.red);
g.fillArc(x,y,10, 10,0, 360);
}
if(flag2)
{
g.setColor(Color.gray);
while(i<=700)
{
if(flag5) { break;}
for(int j=x-i;j<=x+i;j++)
{
if(check(j,y-i)){res1=j;res2=y-i; flag5=true; break;}
g.fillArc(j,y-i,10, 10,0, 360);
}
for(int j=y-i;j<=y+i;j++)
{
if(check(x+i,j)){ res1=x+i;res2=j;flag5=true; break;}
g.fillArc(x+i,j,10, 10,0, 360);
}
for(int j=x+i;j>=x-i;j--)
{
if(check(j,y+i)){ res1=j;res2=y+i;flag5=true; break;}
g.fillArc(j,y+i,10, 10,0, 360);
}
for(int j=y+i;j>=y-i;j--)
{
if(check(x-i,j)){ res1=x-i;res2=j;flag5=true; break;}
g.fillArc(x-i,j,10, 10,0, 360);
}
repaint();
i++;
}
}
if(flag5)
{
g.setColor(Color.blue);
g.fillArc(res1,res2,15, 15,0, 360);
}
g.setColor(textcolor);
g.setFont(myfont);
}
else {
System.out.println("Error");
}
}
public boolean check(int l1,int l2)
{
for(int i=0;i<10;i++)
{
if(((int)arr[i].lon)==l1)
{
if(((int)arr[i].lat)==l2)
{
if((arr[i].resourcename).equalsIgnoreCase(str)) return true;
}
}
}
return false;
}
public static int myRandom(int min,int max)
{
Random rl=new Random();
int i=rl.nextInt(max);
if(i<=min) return myRandom(min,max);
return i;
}
public int plotLat(float f)
{
f=f*100;
f=f-1080;
int i;
i=(int)f;
return i;
}
public int plotLon(float f)
{
f=f*100;
f=f-7860;
int i;
i=(int)f;
return i;
}
public static void main(String args[]) {
NearestResource nr;
nr = new NearestResource();
}
}
No comments:
Post a Comment