Hi, I'm trying to write some code that will draw a filled in black circle at the coordinates where the mouse is clicked. Here is what I have written so far, I can currently get the coordinates of the mouse click point but nothing is displayed. I also get an error "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException". Could you please help? Thanks

package final;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.event.*;

/**
 *
 * @author
 */
public class RunProgram extends JPanel implements ActionListener, ChangeListener, MouseListener {
    private JPanel phasePanel;
    static JPanel contentPane;
    private JPanel picture1;
    private JPanel picture2;

    /** Creates a new instance of RunProgram */
    public RunProgram() {
        setLayout(new BorderLayout());

        //Create a panel and make it the content pane.
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(BorderFactory.createRaisedBevelBorder());

        //create panel to show original pictures
        picture1 = new JPanel();
        picture1.addMouseListener(this);
        // picture1.setBackground(Color.BLUE);
        picture1.setBorder(BorderFactory.createLoweredBevelBorder());
        picture1.setPreferredSize(new Dimension(450, 1000));

        picture2 = new JPanel();
        picture2.addMouseListener(this);
        //picture2.setBackground(Color.GREEN);
        picture2.setBorder(BorderFactory.createLoweredBevelBorder());
        picture2.setPreferredSize(new Dimension(450, 1000));

        JPanel controlPanel = new JPanel();
        //controlPanel.setBackground(Color.GRAY);
        controlPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        controlPanel.setPreferredSize(new Dimension(800, 50));
        controlPanel.setLayout(new FlowLayout());

        contentPane.add(phasePanel, BorderLayout.SOUTH);
        contentPane.add(toolBar, BorderLayout.NORTH);
        contentPane.add(picture1, BorderLayout.WEST);
        contentPane.add(picture2, BorderLayout.EAST);
        contentPane.add(bar, BorderLayout.NORTH);
        contentPane.add(toolBar, BorderLayout.CENTER);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame("ImageViewer");
        frame.add(new RunProgram());
        frame.setSize(1100, 900);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar(bar);
        frame.setContentPane(contentPane);
        frame.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        System.out.println("x = " + x + "y = " + y);
        drawCircle(e.getX()-(radius/2), e.getY()-(radius/2));
        repaint();
    }

    public void drawCircle(int x, int y) {
        Graphics g = getGraphics();
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        g.setColor(Color.BLACK);
        g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    
    int x,  y;
    int radius = 20;
}

In drawCircle g is null. That is why you get null pointer exception and that is also why nothing is being drawn.
You need to get the graphics from the Panel which is pressed. You can get this info from the event, e.getSource(); which returns an object. Type cast this object to a JPanel and get the graphics from that. But make it safe!
E.g.

if(source instanceof JPanel) {
  Graphics g = source.getGraphics();
} // else ignore

Good luck!

Thanks so much, I did exactly what you said and now the circle is being displayed. Thanks for your help!

plz send me whole code of drawing aa circle in netbean 6.9

ok cool thanks, I would not advise to use mouseclick most of the time release is prefered to my knowledge . For the people like Muniba, code could look something like this . BTW renamed the class is I want to use it as test case for a touch screen.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.*;
import javax.swing.event.*;

public class TouchScreen extends JPanel implements ActionListener, ChangeListener, MouseListener {
    /**
     * 
     */
    //??? ok eclipse wants this:
    private static final long serialVersionUID = 1L;

    private JPanel phasePanel;
    static JPanel contentPane;   
    private JPanel picture1;
    private JPanel picture2; 
    /** Creates a new instance of RunProgram */

    public TouchScreen() {
        setLayout(new BorderLayout());
        //Create a panel and make it the content pane.28.
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(BorderFactory.createRaisedBevelBorder());
        //create panel to show original pictures32.        
        picture1 = new JPanel();    
        picture1.addMouseListener(this);
        // picture1.setBackground(Color.BLUE);
        picture1.setBorder(BorderFactory.createLoweredBevelBorder());
        picture1.setPreferredSize(new Dimension(450, 1000));
        picture2 = new JPanel();
        picture2.addMouseListener(this);
        //picture2.setBackground(Color.GREEN);
        picture2.setBorder(BorderFactory.createLoweredBevelBorder());
        picture2.setPreferredSize(new Dimension(450, 1000));
        JPanel controlPanel = new JPanel();
        //controlPanel.setBackground(Color.GRAY);
        controlPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        controlPanel.setPreferredSize(new Dimension(800, 50));
        controlPanel.setLayout(new FlowLayout());
        //contentPane.add(phasePanel, BorderLayout.SOUTH);
        //contentPane.add(toolBar, BorderLayout.NORTH);
        contentPane.add(picture1, BorderLayout.WEST);
        contentPane.add(picture2, BorderLayout.EAST);
        //contentPane.add(bar, BorderLayout.NORTH);
        //contentPane.add(toolBar, BorderLayout.CENTER);
    }
    /**59.     * @param args the command line arguments*/

    public static void main(String[] args) {
        JFrame frame = new JFrame("ImageViewer");
        frame.add(new TouchScreen());
        frame.setSize(1100, 900);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setJMenuBar(bar); // does not exist
        frame.setContentPane(contentPane);
        frame.setVisible(true);
    }

    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        System.out.println("x = " + x + "y = " + y);
        drawCircle(e.getX()-(radius/2), e.getY()-(radius/2), e.getSource(),false);
        repaint();
    }


    public void mouseReleased(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        System.out.println("x = " + x + "y = " + y);
        drawCircle(e.getX()-(radius/2), e.getY()-(radius/2), e.getSource(),true);
        repaint();
    }

    public void drawCircle(int x, int y, Object source, boolean fill) {
        if(source instanceof JPanel) {
            Graphics g = ((JComponent) source).getGraphics();

             // g.clearRect(0,0,450,1000);

            g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
            g.setColor(Color.BLACK);
            if (fill) {
                g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
            }

        } // else ignore
    }


    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

    int x,  y;
    int radius = 20;
    @Override
    public void stateChanged(ChangeEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.