Draw Circle Mouse Click Java

#one

  • 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 exist an easy 1, merely i don't know what the problem is. When the mouse is clicked on the console, the coordinates are taken and a circle is drawn.

public form Circle extends JFrame { 	 	private JFrame masterFrame; 	private JPanel masterPanel; 	individual int mouseX, mouseY; 	private Graphics g; 	 	public Circumvolve(){ 		masterPanel = new JPanel(); 		 		setTitle("Circle"); 		setSize(600, 400); 		setDefaultCloseOperation(masterFrame.EXIT_ON_CLOSE); 		setVisible(truthful); 		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); 	             Arrangement.out.println("10: " + mouseX); 	             Organisation.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 colour of the circumvolve 			g.setColor(Colour.Blueish); 			//draws a circumvolve in JFrame 			g.drawOval(x, y, fifty, 50); 			//fills the circumvolve 			g.fillOval(x, y, 50, 50); 			repaint(); 		} 		 	} 	 	public static void master(String[] args){ 		Circle circle = new Circle(); 	} }            

I go null exeption fault.


Is This A Good Question/Topic? 0

  • +

#2 blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Mouse click draw circumvolve

Posted 23 April 2012 - xi:13 PM

Where's the exception thrown?

#3 pbl User is offline

Reputation: 8381

  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Mouse click draw circle

Posted 24 April 2012 - 01:55 PM

E'er mail you stack trace. It simplifies our job a lot.

#4 pbl User is offline

Reputation: 8381

  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Mouse click draw circle

Posted 24 April 2012 - 02:01 PM

And this is non a practiced thought

private Graphics g;

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

your public void mouseClicked(MouseEvent evt)
should merely set values in example variables that volition exist used by paint()/paintComponent()

#5 Silent Ace User is offline

  • New D.I.C Head

Reputation: 2

  • View blog
  • Posts: 42
  • Joined: 07-Jan 09

Re: Mouse click describe circle

Posted 25 April 2012 - 09:37 AM

Thanks for your help but i however don't know where to add together the Graphics object. Hither is my new code with the errors that i am getting. When i debug it says that g is null. And don't listen the actress things i added they are to exist used after. Cheers 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.issue.MouseListener; import coffee.awt.event.MouseMotionListener;  import javax.swing.*;  public course Circle extends JFrame { 	 	HandlerMouse handler = new HandlerMouse(); 	 	individual JPanel masterPanel; 	individual JTextArea masterTextArea; 	private JScrollPane masterScrollPane; 	individual int mouseX, mouseY; 	private Point point1, point2; 	Graphics grand; 	 	public Circle(){ 		 		setTitle("Circle"); 		setSize(600, 400); 		setDefaultCloseOperation(new JFrame().EXIT_ON_CLOSE); 		//this must be gear up for custom layout of components 		setLayout(naught);  		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 together(masterPanel); 		add(masterScrollPane); 		setLocationRelativeTo(null); 		setVisible(true); 		 	} 	 	public void paint(Graphics grand, int ten, int y){	 	 		g.setColor(Color.Bluish); 		g.drawOval(10, y, 50, fifty); 	    grand.fillOval(x, y, l, 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("Ten: " + mouseX); 	        System.out.println("Y: " + mouseY); 	         	        //get the number of clicks made 	        System.out.println("Clicked no. times: " + evt.getClickCount()); 	         	        if (point1 != null && point2 != cipher){	 				int radius1 = (int) (Math.sqrt(Math.pow(point2.x-point1.x, two)+Math.pow(point2.y-point1.y, 2))); 				int radius2 = (int) (Math.sqrt(Math.prisoner of war(point2.ten-point1.ten, two)+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(Betoken ane, Point two, int rad1, int rad2) 		{ 			//thousand.drawOval(one, two, rad1, rad2); 		}  		public void mouseDragged(MouseEvent e)  		{ 		} 		 		public void mouseMoved(MouseEvent eastward)  		{ 		} 	} 	 	//method for cartoon the circumvolve 	/*public void paint(Graphics grand) 	{ 		//sets the colour of the circle 		thou.setColor(Color.Blue); 		//draws a circumvolve in JFrame 		g.drawOval(200, 200, 50, 50); 		//fills the circumvolve 		1000.fillOval(200, 200, 50, 50); 		repaint(); 	} 	*/ 	public static void main(String[] args){ 		Circle circumvolve = new Circumvolve(); 	} }            

The errors when the mouse clicks the yellow jpanel.

Exception in thread "AWT-EventQueue-0" coffee.lang.NullPointerException 	at circumvolve.Circle.pigment(Circle.java:57) 	at circumvolve.Circumvolve$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 coffee.awt.window.dispatchEventImpl(Unknown Source) 	at coffee.awt.Component.dispatchEvent(Unknown Source) 	at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 	at coffee.awt.EventQueue.access$000(Unknown Source) 	at coffee.awt.EventQueue$3.run(Unknown Source) 	at coffee.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 coffee.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 	at coffee.awt.EventQueue.dispatchEvent(Unknown Source) 	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 	at coffee.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 	at coffee.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 Apr 2012 - 09:38 AM

#6 pbl User is offline

Reputation: 8381

  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Mouse click describe circle

Posted 25 April 2012 - 08:sixteen PM

This is how it works

form Circle extends JFrame      int x, y;      public void paint(Graphics k) {       super.paint(g);       g.drawOval(x, y, l, 50);     }             public void mouseClicked(MouseEvent evt){             x = evt.getX();             y = evt.getY();             repaint();        }            

#seven Silent Ace User is offline

  • New D.I.C Head

Reputation: ii

  • View blog
  • Posts: 42
  • Joined: 07-Jan 09

Re: Mouse click depict circle

Posted 26 April 2012 - 06:23 AM

So i was very close just that i had ii parameters added and for some reason that didn't work. Well all is practiced at present. Thank you pbl you are a Java God :D.

clementbese1996.blogspot.com

Source: https://www.dreamincode.net/forums/topic/276524-mouse-click-draw-circle/

0 Response to "Draw Circle Mouse Click Java"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel