0

I am trying to write a code that makes a dot with one click, line with two, triangle with three, and restarts on 4, but I keep getting a nullpointerexception on my first click. Here is my code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TriangleClicker extends JFrame {
    private final int width = 700;
    private final int height = 800;
    private JPanel panel;
    private MouseListener listener;
    private int p1x;
    private int p1y;
    private int p2x;
    private int p2y;
    private int p3x;
    private int p3y;
    private int count = 0;
    private JComponent tri;

public TriangleClicker() {
    this.setSize(width, height);
    this.add(createPanel());
    this.tri = new MousePressListener();
    this.add(this.tri);
    listener = new MousePressListener();
    this.addMouseListener(listener);
}

public JPanel createPanel() {
    panel = new JPanel();
    panel.setSize(width, height);
    return panel;
}

class MousePressListener extends JComponent implements MouseListener  {
    public Graphics g;
    public void drawLine1(Graphics g) {
        g.setColor(Color.BLACK);
        g.drawLine(p1x, p1y, p1x, p1y);
    }

    public void drawLine2(Graphics g) {
        g.drawLine(p1x, p1y, p2x, p2y);
    }

    public void drawLine3(Graphics g) {
        g.drawLine(p3x, p3y, p1x, p1y);
        g.drawLine(p3x, p3y, p2x, p2y);
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
        if (count == 0) {
            System.out.println("1");
            p1x = e.getX();
            p1y = e.getY();
            drawLine1(g);
            count++;
        }
        else if (count == 1) {
            p2x = e.getX();
            p2y = e.getY();
            drawLine2(g);
            count++;
        }
        else if(count == 2) {
            p3x = e.getX();
            p3y = e.getY();
            drawLine3(g);
            count++;
        }
        else if(count == 4) {
            g.clearRect(0, 0, WIDTH, HEIGHT);
            p1x = e.getX();
            p1y = e.getY();
            p2x = 0;
            p2y = 0;
            p3x = 0;
            p3y = 0;
            drawLine1(g);
            count = 1;
        }
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    }
}
}

My errors are in the makeLine1() method trying to set the color to black, and in the mousePressed() method trying to call makeLine1().

Thanks.

Keegan Moore
  • 29
  • 2
  • 6
  • `g` is `null` and this is not how custom painting should be done. Since this is the third time this (related) question has been asked, please tell your fellow class mates to have a look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) to better understand how painting should be done in Swing – MadProgrammer Apr 30 '18 at 22:58
  • You may also want to have a look at the answers provided to your fellow student earlier - [Adding a MouseListener to a panel](https://stackoverflow.com/questions/50092855/adding-a-mouselistener-to-a-panel/50092971#50092971) – MadProgrammer Apr 30 '18 at 22:58
  • Oh, and since you and the person who asked [this question](https://stackoverflow.com/questions/50108301/mousepressed-wont-respond-on-jpanel) are basically using the same code, you might like to give them a heads up of the problem they are about to run into – MadProgrammer Apr 30 '18 at 23:00
  • Oh, and, you might want to consider adding the `MouseListener` to the actual component you want to respond to the mouse events, as you're basically adding it to everything else but. You might find [How to Write a Mouse Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) useful – MadProgrammer Apr 30 '18 at 23:06

0 Answers0