6 Replies - 1152 Views - Last Post: 26 April 2012 - 06:23 AM Rate Topic: -----

#1 Silent Ace  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 07-January 09

Mouse click draw circle

Posted 23 April 2012 - 09:52 PM

This should be an easy one, but i don't know what the problem is. When the mouse is clicked on the panel, the coordinates are taken and a circle is drawn.

public class Circle extends JFrame {
	
	private JFrame masterFrame;
	private JPanel masterPanel;
	private int mouseX, mouseY;
	private Graphics g;
	
	public Circle(){
		masterPanel = new JPanel();
		
		setTitle("Circle");
		setSize(600, 400);
		setDefaultCloseOperation(masterFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setLocationRelativeTo(null);
		
		add(masterPanel);
		addMouseListener(null);
		
		HandlerMouse handler = new HandlerMouse();
		masterPanel.addMouseListener(handler);
	}
	
	private class HandlerMouse implements MouseListener{

		public void mouseClicked(MouseEvent evt) 
		{
		     mouseX = evt.getX();
	             mouseY = evt.getY();
	             paintCircle(mouseX, mouseY);
	             System.out.println("X: " + mouseX);
	             System.out.println("Y: " + mouseY);
		}

		public void mouseEntered(MouseEvent arg0) 
		{	
		}

		public void mouseExited(MouseEvent arg0) 
		{
		}

		public void mousePressed(MouseEvent evt) 
		{
		}

		public void mouseReleased(MouseEvent arg0) 
		{
		}
		
		public void paintCircle(int x, int y){
			//sets the color of the circle
			g.setColor(Color.BLUE);
			//draws a circle in JFrame
			g.drawOval(x, y, 50, 50);
			//fills the circle
			g.fillOval(x, y, 50, 50);
			repaint();
		}
		
	}
	
	public static void main(String[] args){
		Circle circle = new Circle();
	}
}




I get null exeption error.

Is This A Good Question/Topic? 0
  • +

Replies To: Mouse click draw circle

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Mouse click draw circle

Posted 23 April 2012 - 11:13 PM

Where's the exception thrown?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Mouse click draw circle

Posted 24 April 2012 - 01:55 PM

Always post you stack trace. It simplifies our job a lot.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Mouse click draw circle

Posted 24 April 2012 - 02:01 PM

And this is not a good idea

private Graphics g;

call the repaint() method and use the brand new Graphics object that is provided to you in the paint() or paintComponent() method.

your public void mouseClicked(MouseEvent evt)
should only set values in instance variables that will be used by paint()/paintComponent()
Was This Post Helpful? 0
  • +
  • -

#5 Silent Ace  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 07-January 09

Re: Mouse click draw circle

Posted 25 April 2012 - 09:37 AM

Thank you for your help but i still don't know where to add the Graphics object. Here is my new code with the errors that i am getting. When i debug it says that g is null. And don't mind the extra things i added they are to be used later. Thank you again.

package circle;

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

import javax.swing.*;

public class Circle extends JFrame {
	
	HandlerMouse handler = new HandlerMouse();
	
	private JPanel masterPanel;
	private JTextArea masterTextArea;
	private JScrollPane masterScrollPane;
	private int mouseX, mouseY;
	private Point point1, point2;
	Graphics g;
	
	public Circle(){
		
		setTitle("Circle");
		setSize(600, 400);
		setDefaultCloseOperation(new JFrame().EXIT_ON_CLOSE);
		//this must be set for custom layout of components
		setLayout(null);

		masterPanel = new JPanel();
		masterPanel.setSize(600,300);
		masterPanel.setLocation(0, 0);
		masterPanel.setBackground(Color.YELLOW);
		masterPanel.addMouseListener(handler);
		masterPanel.addMouseMotionListener(handler);
		
		masterTextArea = new JTextArea();
		masterTextArea.setBackground(Color.green);
		
		masterScrollPane = new JScrollPane();
		masterScrollPane.add(masterTextArea);
		masterScrollPane.setSize(600, 100);
		masterScrollPane.setLocation(0, 300);
		masterScrollPane.addMouseListener(handler);
		
		add(masterPanel);
		add(masterScrollPane);
		setLocationRelativeTo(null);
		setVisible(true);
		
	}
	
	public void paint(Graphics g, int x, int y){	
	
		g.setColor(Color.BLUE);
		g.drawOval(x, y, 50, 50);
	    g.fillOval(x, y, 50, 50);
	    repaint();
	}
	
	private class HandlerMouse implements MouseListener, MouseMotionListener{
		
		public void mouseClicked(MouseEvent evt){
			mouseX = evt.getX();
	        mouseY = evt.getY();
	        
	        paint(g, mouseX, mouseY);
	        
	        //get the coordinates from the mouse click in app
	        System.out.println("X: " + mouseX);
	        System.out.println("Y: " + mouseY);
	        
	        //get the number of clicks made
	        System.out.println("Clicked no. times: " + evt.getClickCount());
	        
	        if (point1 != null && point2 != null){	
				int radius1 = (int) (Math.sqrt(Math.pow(point2.x-point1.x, 2)+Math.pow(point2.y-point1.y, 2)));
				int radius2 = (int) (Math.sqrt(Math.pow(point2.x-point1.x, 2)+Math.pow(point2.y-point1.y, 2)));
				//drawOval (point1.x, point1.y, radius1, radius2);
			}
		}

		public void mouseEntered(MouseEvent arg0) 
		{	
		}

		public void mouseExited(MouseEvent arg0) 
		{
		}

		public void mousePressed(MouseEvent evt) 
		{
		}

		public void mouseReleased(MouseEvent arg0) 
		{
		}
		
		public void drawOval(Point one, Point two, int rad1, int rad2)
		{
			//g.drawOval(one, two, rad1, rad2);
		}

		public void mouseDragged(MouseEvent e) 
		{
		}
		
		public void mouseMoved(MouseEvent e) 
		{
		}
	}
	
	//method for drawing the circle
	/*public void paint(Graphics g)
	{
		//sets the color of the circle
		g.setColor(Color.BLUE);
		//draws a circle in JFrame
		g.drawOval(200, 200, 50, 50);
		//fills the circle
		g.fillOval(200, 200, 50, 50);
		repaint();
	}
	*/
	public static void main(String[] args){
		Circle circle = new Circle();
	}
}



The errors when the mouse clicks the yellow jpanel.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at circle.Circle.paint(Circle.java:57)
	at circle.Circle$HandlerMouse.mouseClicked(Circle.java:69)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)



This post has been edited by Silent Ace: 25 April 2012 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Mouse click draw circle

Posted 25 April 2012 - 08:16 PM

This is how it works

class Circle extends JFrame

    int x, y;

    public void paint(Graphics g) {
      super.paint(g);
      g.drawOval(x, y, 50, 50);
    }


  
       public void mouseClicked(MouseEvent evt){
            x = evt.getX();
            y = evt.getY();
            repaint();
       }


Was This Post Helpful? 0
  • +
  • -

#7 Silent Ace  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 07-January 09

Re: Mouse click draw circle

Posted 26 April 2012 - 06:23 AM

So i was very close only that i had two parameters added and for some reason that didn't work. Well all is good now. Thank you pbl you are a Java God :D.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1