add event listener to jbutton for drawing lines
I'm trying to place a map (on a JPanel) in a JFrame with 12 JButtons representing cities on the map.
When the user moves the mouse over a city, lines radiate from that city to some of the other cities.
It is supposed to represent airline routes.
The problem is that as a beginner I find all these event listeners a bit confusing and as a result I may be a long way off getting this to work (but I would really appreciate some advice).
When I try to add my listener object to a city the compiler keeps telling me that addMouseListener cannot be applied to my city objects (which extend JButton). The program compiles OK if I don't add a listener to the buttons (but dosn't work). Here is my code, I realise the lines will be drawn in the wrong places but I can sort that out later
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import java.awt.event.*;
public class Map1 extends JFrame
{
City[] city = new City[12];
private Graphics g;
public static void main(String[] args)
{
new Map1();
}
public Map1()
{
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RouteMap myMap = new RouteMap();
city[0] = new City(5,5);
city[1] = new City(20,20);
city[2] = new City(45,45);
city[3] = new City(60,60);
city[4] = new City(85,85);
city[5] = new City(100,100);
city[6] = new City(125,125);
city[7] = new City(140,140);
city[8] = new City(165,165);
city[9] = new City(180,180);
city[10] = new City(205,205);
city[11] = new City(220,220);
for(int i = 0; i < 12; i ++)
{
myMap.add(city[i]);
}
this.add(myMap);
this.setVisible(true);
}
private class Listener implements MouseListener
{
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e)
{
g = getGraphics();
g.setColor(Color.black);
g.drawLine(e.getX(),e.getY(),0,0);
g.dispose();
}
public void mouseExited(MouseEvent e)
{
g = getGraphics();
g.setColor(getBackground());
g.dispose();
}
}
}
class RouteMap extends JPanel
{
Image bgImage=null;
String message = "";
public RouteMap()
{
super(null);
this.setLayout(null);
try
{
getFileImage("/home/msc2005/hroarty/public_html/images/map.jpg");
}
catch (Exception ex)
{
message="File load failed: "+ex.getMessage();
}
}
public void paintComponent(Graphics g)
{
if (bgImage != null)
{
g.drawImage(bgImage,0,0,this);
}
else
{
g.drawString(message,40,40);
}
}
private void getFileImage(String filePath) throws InterruptedException, IOException
{
FileInputStream in = new FileInputStream(filePath);
byte [] b=new byte[in.available()];
in.read(b);
in.close();
bgImage=Toolkit.getDefaultToolkit().createImage(b) ;
MediaTracker mt=new MediaTracker(this);
mt.addImage(bgImage,0);
mt.waitForAll();
}
}
class City extends JButton
{
private Color c = new Color(255, 255, 0);
private int x;
private int y;
City(int horizontal, int vertical)
{
this.x = horizontal;
this.y = vertical;
this.setBounds(horizontal,vertical,8,8);
this.setBackground(c);
//addMouseListener(this);
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}

