user3149851
user3149851

Reputation: 31

Button not working when trying to draw a line

I'm new to Java and teaching myself and trying to build this program. It will create dots when the mouse clicks, but won't draw a line when the button is pushed. (eventually the line will connect the dots, but I'm not there yet.) First I just need to get it to draw something when pushed and work from there. I've tried multiple things and I can't get it to work. Here is my code:

public void init()
{
    LineDrawListener listener = new LineDrawListener ();
    addMouseListener (listener);

    Button lineGraph = new Button("Graph Line");
    lineGraph.addActionListener (this);
    add (lineGraph, BorderLayout.EAST);

    setBackground (Color.yellow);
    setSize (APPLET_WIDTH, APPLET_HEIGHT);
}

public void paint(Graphics g)
{
    // draws dots on screen
    g.setColor(Color.blue);
    if (current != null)
    {
        xcoordinates.addElement (current.x);
        ycoordinates.addElement (current.y);
        count++;
        g.fillOval (current.x-4, current.y-4, 10, 10);
        repaint();
    }
    if (push == true)
    {
        g.drawLine (5, 5, 30 , 30);
        repaint();
    }
}

class LineDrawListener extends MouseAdapter
{
    public void mouseClicked (MouseEvent event)
   {
       current = event.getPoint();

    repaint();
}

}
public void actionPerformed (ActionEvent event)
{
    Object action = event.getSource();
    if(action==lineGraph)
    {
        push = true;
    }
}

Any help on how to get the button to work would be much appreciated. Thanks in advance.

Upvotes: 3

Views: 117

Answers (3)

camickr
camickr

Reputation: 324108

Don't override paint(). Don't invoke repaint() in a painting method, this can cause in infinite loop.

Check out Custom Painting Approaches for working examples of the two common ways to do custom paintint:

  1. by using a List to track the objects to be painted
  2. by painting to a BufferedImage

Upvotes: 4

Paul Samsotha
Paul Samsotha

Reputation: 208974

  1. Don't paint on top-level containers like JApplet
  2. Rather paint on a JPanel and override the paintComponent method and call super.paintComponet(g)

    @Override
    protected void paintComponent(Graphic g){
        super.paintComponent(g);
        ...
    }
    
  3. Don't call repaint() from inside the paint() method. Call it in your actionPerformed()

  4. Learn to post an SSCCE

Number 3 is your most dire problem

Upvotes: 4

Mad Physicist
Mad Physicist

Reputation: 114250

The reason you can not get the line to draw is that repaint() posts a request to repaint the component. You are using it as if it refreshes the view somehow. You need to change three things in your code, all in paint():

  1. Do not call repaint() in paint()
  2. Override paintComponent() rather than paint() so your borders get drawn if you should ever have them at a later time.
  3. Call super.paintComponent() in paintComponent() to do some initialization for you. It is probably OK not to for something this simple, but good practice for the future.

Here is what the code would look like:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    // draws dots on screen
    g.setColor(Color.blue);
    if (current != null)
    {
        xcoordinates.addElement (current.x);
        ycoordinates.addElement (current.y);
        count++;
        g.fillOval (current.x-4, current.y-4, 10, 10);
    }
    if (push == true)
    {
        g.drawLine (5, 5, 30 , 30);
    }
}

Upvotes: 4

Related Questions