Draw Circle Mouse Click Javafx

#1

  • 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 like shooting fish in a barrel 1, but i don't know what the trouble is. When the mouse is clicked on the console, the coordinates are taken and a circle is drawn.

public class Circle extends JFrame { 	 	individual JFrame masterFrame; 	private JPanel masterPanel; 	individual int mouseX, mouseY; 	private Graphics thousand; 	 	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 circumvolve 			g.setColor(Color.Blueish); 			//draws a circumvolve in JFrame 			1000.drawOval(ten, y, 50, 50); 			//fills the circle 			yard.fillOval(10, y, fifty, 50); 			repaint(); 		} 		 	} 	 	public static void chief(String[] args){ 		Circle circumvolve = new Circle(); 	} }            

I get null exeption error.


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 depict circle

Posted 23 Apr 2012 - 11:xiii PM

Where'due south the exception thrown?

#iii pbl User is offline

Reputation: 8381

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

Re: Mouse click draw circumvolve

Posted 24 April 2012 - 01:55 PM

E'er post you lot stack trace. Information technology simplifies our task 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 not a skillful idea

private Graphics g;

call the repaint() method and utilize the make 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 past paint()/paintComponent()

#v Silent Ace User is offline

  • New D.I.C Head

Reputation: 2

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

Re: Mouse click describe circle

Posted 25 Apr 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 cypher. And don't mind the extra things i added they are to exist used after. Thanks again.

parcel circle;  import coffee.awt.Color; import coffee.awt.Graphics; import coffee.awt.Graphics2D; import coffee.awt.Point; import java.awt.issue.MouseEvent; import java.awt.result.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; 	individual Point point1, point2; 	Graphics g; 	 	public Circle(){ 		 		setTitle("Circle"); 		setSize(600, 400); 		setDefaultCloseOperation(new JFrame().EXIT_ON_CLOSE); 		//this must exist set for custom layout of components 		setLayout(null);  		masterPanel = new JPanel(); 		masterPanel.setSize(600,300); 		masterPanel.setLocation(0, 0); 		masterPanel.setBackground(Color.Yellowish); 		masterPanel.addMouseListener(handler); 		masterPanel.addMouseMotionListener(handler); 		 		masterTextArea = new JTextArea(); 		masterTextArea.setBackground(Color.greenish); 		 		masterScrollPane = new JScrollPane(); 		masterScrollPane.add together(masterTextArea); 		masterScrollPane.setSize(600, 100); 		masterScrollPane.setLocation(0, 300); 		masterScrollPane.addMouseListener(handler); 		 		add(masterPanel); 		add together(masterScrollPane); 		setLocationRelativeTo(nix); 		setVisible(true); 		 	} 	 	public void pigment(Graphics grand, int x, int y){	 	 		g.setColor(Color.BLUE); 		g.drawOval(ten, y, l, 50); 	    g.fillOval(x, y, 50, 50); 	    repaint(); 	} 	 	private grade 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 	        Arrangement.out.println("X: " + mouseX); 	        Organisation.out.println("Y: " + mouseY); 	         	        //get the number of clicks fabricated 	        System.out.println("Clicked no. times: " + evt.getClickCount()); 	         	        if (point1 != null && point2 != null){	 				int radius1 = (int) (Math.sqrt(Math.pw(point2.x-point1.10, 2)+Math.pow(point2.y-point1.y, 2))); 				int radius2 = (int) (Math.sqrt(Math.pow(point2.x-point1.x, two)+Math.prisoner of war(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(Bespeak one, Point ii, int rad1, int rad2) 		{ 			//1000.drawOval(one, ii, rad1, rad2); 		}  		public void mouseDragged(MouseEvent e)  		{ 		} 		 		public void mouseMoved(MouseEvent east)  		{ 		} 	} 	 	//method for drawing the circle 	/*public void paint(Graphics g) 	{ 		//sets the color of the circle 		k.setColor(Color.BLUE); 		//draws a circumvolve in JFrame 		chiliad.drawOval(200, 200, 50, fifty); 		//fills the circle 		g.fillOval(200, 200, 50, 50); 		repaint(); 	} 	*/ 	public static void main(Cord[] args){ 		Circle circumvolve = new Circle(); 	} }            

The errors when the mouse clicks the yellow jpanel.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 	at circle.Circle.paint(Circumvolve.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 coffee.awt.EventQueue$three.run(Unknown Source) 	at java.security.AccessController.doPrivileged(Native Method) 	at java.security.ProtectionDomain$one.doIntersectionPrivilege(Unknown Source) 	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 	at java.awt.EventQueue$iv.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 coffee.awt.EventDispatchThread.pumpEvents(Unknown Source) 	at java.awt.EventDispatchThread.run(Unknown Source)            

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

#half dozen pbl User is offline

Reputation: 8381

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

Re: Mouse click depict circumvolve

Posted 25 April 2012 - 08:sixteen PM

This is how information technology works

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

#seven Silent Ace User is offline

  • New D.I.C Caput

Reputation: two

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

Re: Mouse click draw circle

Posted 26 April 2012 - 06:23 AM

And then i was very close merely that i had two parameters added and for some reason that didn't work. Well all is adept now. Give thanks you pbl you lot are a Java God :D.

scotthisgrat.blogspot.com

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

0 Response to "Draw Circle Mouse Click Javafx"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel