Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

add event listener to jbutton for drawing lines

Hi, can anybody help me with this small program.
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;
}
}
[4139 byte] By [hugh] at [2007-11-11 8:26:00]
# 1 Re: add event listener to jbutton for drawing lines
change the declaration of your class to:

class City extends JButton implements MouseListener

Think about how much a MouseListener is going to do attached to each Button. Will that button know about the other buttons? Or a particular button? Perhaps your frame is the best place to be implementing your Listener for these events ...

Just wondering ...
nspils at 2007-11-11 22:35:23 >
# 2 Re: add event listener to jbutton for drawing lines
Hi nspils,
Thanks for your reply.
As you suggest each city will produce a different set of lines (starting and terminating at different points) so I guess you are saying that I need to implement the listener to the frame. I have now done just that and put the line addMouseListener(this) into the frame constructor. Now my map draws lines from the point where the mouse enters the frame, I think the frame is generating the mouse event object (not my Cities), when I try to tie the event listener to a specific city using the line addMouseListener(city[0]) I get the usual compiler message addMouseListener cannot be applied to my city objects. How do I trigger the event from the City.

Thank you for your help on this so far.
hugh at 2007-11-11 22:36:28 >
# 3 Re: add event listener to jbutton for drawing lines
if you want something to draw when you push a JButton, you want to be listening for one of the events which the button push can produce. It wouldn't be a mouse event, it would be a(n) _____________ event.
nspils at 2007-11-11 22:37:32 >
# 4 Re: add event listener to jbutton for drawing lines
Thanks nspils,

Your advice has helped a lot, I now use the ActionEvent class so when I click the JButton it draws the line. I think I can handle the rest from here.
hugh at 2007-11-11 22:38:26 >
# 5 Re: add event listener to jbutton for drawing lines
on the subject of drawing lines does anyone know how to set the lines to bold. Ive tried g.setBold(); but I wasn't surprised when it didn't work.
hugh at 2007-11-11 22:39:36 >
# 6 Re: add event listener to jbutton for drawing lines
you can cast your Graphics object to Graphics2D, which has the method setStroke. this will define the thiknes of your line, i think.
graviton at 2007-11-11 22:40:29 >
# 7 Re: add event listener to jbutton for drawing lines
I'm not really sure how I should implement the setStroke method

I've tried

Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new wideStroke);

and variations of this but the compiler wont take any of these. Can you see where I'm going wrong.

Thanks for your help
hugh at 2007-11-11 22:41:38 >
# 8 Re: add event listener to jbutton for drawing lines
have a look at interface stroke ( http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Stroke.html) and the implementing class basicstroke ( http://java.sun.com/j2se/1.4.2/docs/api/java/awt/BasicStroke.html).
graviton at 2007-11-11 22:42:36 >