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;
}