f



Creating Custom Components

Hi

I am trying to create my own Line class onto which mouseListeners can be
registered. Basically, when the mouse stops or passes over the line I want
the line to change colour. There will be multiple lines placed onto one
panel, so I want the code to be contained in the Line class. How can I
create the Line class so it has the capacity to add a mouseListener to it
and therefore recognise when the mouse has passed over it?

Thanks

Tony


0
Tony
6/26/2003 10:49:58 AM
comp.lang.java.gui 8286 articles. 0 followers. Post Follow

4 Replies
597 Views

Similar Articles

[PageSpeed] 53

On Thu, 26 Jun 2003 12:49:58 +0200, Tony wrote:

> Hi
> 
> I am trying to create my own Line class onto which mouseListeners can be
> registered. Basically, when the mouse stops or passes over the line I
> want the line to change colour. There will be multiple lines placed onto
> one panel, so I want the code to be contained in the Line class. How can
> I create the Line class so it has the capacity to add a mouseListener to
> it and therefore recognise when the mouse has passed over it?
> 
> Thanks
> 

Hi,

don't know if I understand your problem right.
Do you let your Line class extend from JComponent?
If not, do so and then call myLine.addMouseListener(myLine)
or myLine.addMouseMotionListener(myLine)
Therefore, Line must implement the MouseListener interface(or
MouseMotionListener interface)

Did this help?? If not, ask again.
Ciao, bene
0
Benjamin
6/26/2003 11:47:35 AM
Hi Benjamin

This was right and it did help, thanks. I now have another question. In my
Line class I override the paint() method with some arbitrary bit of code
like this:

public void paint(Graphics g){
    g.drawString("Hello", 20, 20);
}

In another class I attempt to call this method like so

public class DrawingBoard extends JFrame{
     JPanel drawingPanel;
    Line line = new Line();
    ...

    public DrawingBoard(){
        drawingPanel.add(line);
    }

    public void paint(Graphics g){
        //what do I place here to paint or repaint all the components placed
        //onto the drawingPanel? Is it something like this?
        drawingPanel.repaint();
    }
}

Thanks

Tony



0
Tony
6/26/2003 12:37:05 PM
Tony wrote:
>     public void paint(Graphics g){
>         //what do I place here to paint or repaint all the components placed
>         //onto the drawingPanel? Is it something like this?
>         drawingPanel.repaint();
>     }
> }

What about super.paint()?


0
Jacob
6/26/2003 12:43:45 PM
Tony wrote:
> If I create a custom component whos only functionality is to draw a line
> onto any panel that it is added to, (possibly using the Graphics.drawLine()
> method) how would I do this? This is what I have so far, but things do not
> work right. It is driving me crazy:
> 
> public class Line extends JComponent{
> 
>     public void paintComponent(Graphics g){
>         g.drawLine(10, 10, 20, 20);
>         super.paintComponent(g);
>     }
> }

Did you try to swap the two statements?

0
Jacob
6/26/2003 3:30:18 PM
Reply: