Domanda

I am very new to MouseEvents and MouseListeners, and I had recently asked a question about creating a basketball shot chart. What I have so far is this

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

public class StatTracker extends JPanel //implements MouseListener
{
   JPanel court;
   int xCoord, yCoord, clicks;
   boolean made, missed = false;
   public void paintComponent(Graphics g)
   {
      //Draw court lines
      g.setColor(Color.BLUE);
      g.fillRect(190,0,120,190);
      g.drawOval(190,130,120,120);
      g.drawRect(0,0,500,400);
      g.drawArc(52,-110,396,360,0,-180);
      g.drawLine(52,0,52,70);
      g.drawLine(448,0,448,70);

      g.setColor(Color.BLACK);
      g.fillRect(220,35,60,5);
      g.fillOval(240,40,20,20);
      if(made)
      {
         g.setColor(Color.GREEN);
         g.drawString("O", xCoord, yCoord);
      }
      if(missed)
      {
         g.setColor(Color.RED);
         g.drawString("X", xCoord, yCoord);
      }
      //made=false;
      //missed=false;
   } 
   public StatTracker()
   { 
      setLayout(new BorderLayout());
      court = new JPanel();
   }
   public void mouseClicked(MouseEvent e) 
   {
      clicks = e.getClickCount();
      xCoord = e.getX();
      yCoord = e.getY();
      if(xCoord <=500 && xCoord >= 0)
      {
         if(clicks==1)
            missed=true;
         if(clicks==2)
            made=true;
      }
      repaint();
   }
}

So that basically if the mouse is clicked within the bounds of the court (0-500, 0-400), it will display an 'X' or 'O' based on if it was a single or double click. The main method is in a separate driver file and everything runs correctly (court appears on screen) but nothing occurs when I click on the court.

Any help or advice is appreciated, thank you.

È stato utile?

Soluzione

I'm assuming you commented out the MouseListener because you were getting an error.

The thing is, when you implements MouseListener, you need to @Override all the methods, not just mouseClicked

public class StatTracker extends JPanel implements MouseListener {
    ...

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}
}

The ones you don't need you can just leave empty. Just add code to the mouseClicked if that's all you need.

Also you need to call super.paintComponent(g) in your paintComponent method

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

Also as you can see from my code, I use the @Override annotation. You should make a habit of doing this for methods you are attempting to override. If it is a successful override, you won't get an error. If it isn't then you will get an error.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top