0

This is the code I have so far; I understand what you mean I just can't see a way to implement it as of yet. Hopefully this will allow you to see what needs to be done to implement your idea.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;

public class maptest extends JFrame implements MouseListener {
	
	int xsize=700, ysize=700,x,y,distance,minDistance;
	String storeName;
	
	public maptest() {
		setSize(xsize, ysize); //Set JFrame size
		setVisible(true); //Set visiility to true
		show(); //Show
		addMouseListener(this); //Add mouse listener events
	}
	
	class store{
		String name;
		int x, y, width, height, centerX, centerY, distanceFromX, distanceFromY, distanceFromMouseClick;
	
		public store(String storeName,int storeX,int storeY, int storeWidth, 
					 int storeHeight, int storeCenterX,int storeCenterY,
					 int storeDistanceFromX, int storeDistanceFromY, 
					 int storeDistanceFromMouseClick) {
			name = storeName;
			x = storeX;
			y = storeY;
			width = storeWidth;
			height = storeHeight;
			centerX = storeCenterX;
			centerY = storeCenterY;
			distanceFromX = storeDistanceFromX;
			distanceFromY = storeDistanceFromY;
			distanceFromMouseClick = storeDistanceFromMouseClick;
		}
	
		public void drawStore(Graphics g) {
			g.drawRect(x,y,width,height);
		}
		public boolean isClicked(int a, int b) {
  		 return (a >= x) && (a <= x+width) && (b >= y) && (b <= y+height);
		}
			
		public int distanceFrom(int x, int y) {
			if(x-centerX<0) {
				distanceFromX = centerX-x;
			} else {
				distanceFromX = x-centerX;
			}
			if(y-centerY<0) {
				distanceFromY = centerY-y;
			} else {
				distanceFromY = y-centerY;
			}
			distanceFromMouseClick = distanceFromX+distanceFromY;
			return distanceFromMouseClick;
		
		}
		
		
		public String getName() {
			return name;
		}
		
	}
	
	store Asda = new store("Asda",50,50,40,40,70,70,0,0,0);
	store Tesco = new store("Tesco",50,100,40,40,70,120,0,0,0);
	store Morrisons = new store("Morrisons",500,50,40,40,520,70,0,0,0);
	store Sainsburys = new store("Sainsburys",500,100,40,40,520,120,0,0,0);
	
	store[] stores = {Asda,Tesco,Morrisons,Sainsburys};	
	
	class road{
		int x1,x2,y1,y2;
		
		public road(int roadX1,int roadX2,int roadY1,int roadY2) {
			x1 = roadX1;
			x2 = roadX2;
			y1 = roadY1;
			y2 = roadY2;
		}
		
		
		public void drawRoad(Graphics g) {
			g.drawLine(x1,x2,y1,y2);
		}
	
	}
								
	road firstTop = new road(90,63,500,63);
	road firstBottom1 = new road(90,77,150,77);
	road firstBottom2 = new road(164,77,440,77);
	road firstBottom3 = new road(454,77,500,77);
	road secondTop1 = new road(90,113,150,113);
	road secondTop2 = new road(164,113,440,113);
	road secondTop3 = new road(454,113,500,113);
	road secondBottom1 = new road(90,127,150,127);
	road secondBottom2 = new road(164,127,440,127);
	road secondBottom3 = new road(454,127,500,127);
	road thirdLeft1 = new road(150,77,150,113);
	road thirdLeft2 = new road(150,127,150,500);
	road thirdRight1 = new road(164,77,164,113);
	road thirdRight2 = new road(164,127,164,486);		
	road fourthLeft1 = new road(440,77,440,113);
	road fourthLeft2 = new road(440,127,440,486);
	road fourthRight1 = new road(454,77,454,113);
	road fourthRight2 = new road(454,127,454,500);
	road fifthTop = new road(164,486,440,486);
	road fifthBottom = new road(150,500,454,500);
	
	road[] roads = {firstTop,firstBottom1,firstBottom2,firstBottom3,
					secondTop1,secondTop2,secondTop3,secondBottom1,secondBottom2,secondBottom3,
					thirdLeft1,thirdLeft2,thirdRight1,thirdRight2,
					fourthLeft1,fourthLeft2,fourthRight1,fourthRight2,
					fifthTop,fifthBottom};

		
	public void mouseClicked(MouseEvent e) {
	x=e.getX();
	y=e.getY();
		store clickedStore = null;
		store closestStore = null;
			for(int i=0; i < stores.length; i++) {
				if (stores[i].isClicked(x,y)) {
					clickedStore = stores[i];
					storeName = clickedStore.getName();
				}
			}
			 	
		
		repaint();
	}
	
	public void mouseExited(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	
	public void paint(Graphics g) {
		super.paint(g);
		
		for (int i = 0; i< stores.length; i++) {
			stores[i].drawStore(g);
		}
		g.setColor(Color.blue);
		for (int i = 0; i< roads.length; i++) {
			roads[i].drawRoad(g);
		}
		g.drawOval(x,y,3,3);
		
		g.drawString(storeName,40,40);
	}
	
}
0

Start by adding a distanceFrom(int x, int y) method to your store class, which will return the distance from a point (x,y)

public float distanceFrom(int x, int y) {}

Your mouseClicked method can then loop all of the stores and check the distance to each store.

Between that and the pseudo-code I posted above, you should be able to get a start with it.

Edited by Ezzaral: n/a

0

Hi - I just got up and saw these recent posts.
Ezzaral is right (how many times have I said that now?).
If you look way back in my own posts you will find this:

For closest store you could do a variant on isClicked(...) to return how far the x,y coords are from the center of the door of the store. You can then use these in a simple loop to find which Store is closest.

Then in the following post I proposed exactly the same code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.