Javascript required
Skip to content Skip to sidebar Skip to footer

Java Fx Program Draws Circle Where Mouseclicked

#1

  • New D.I.C Head

Reputation: 2

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

Mouse click draw circle

Posted 23 Apr 2012 - 09:52 PM

This should be an easy one, only 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 class Circumvolve extends JFrame { 	 	individual JFrame masterFrame; 	private JPanel masterPanel; 	private int mouseX, mouseY; 	private Graphics chiliad; 	 	public Circle(){ 		masterPanel = new JPanel(); 		 		setTitle("Circle"); 		setSize(600, 400); 		setDefaultCloseOperation(masterFrame.EXIT_ON_CLOSE); 		setVisible(true); 		setLocationRelativeTo(null); 		 		add together(masterPanel); 		addMouseListener(goose egg); 		 		HandlerMouse handler = new HandlerMouse(); 		masterPanel.addMouseListener(handler); 	} 	 	private form HandlerMouse implements MouseListener{  		public void mouseClicked(MouseEvent evt)  		{ 		     mouseX = evt.getX(); 	             mouseY = evt.getY(); 	             paintCircle(mouseX, mouseY); 	             Organisation.out.println("10: " + 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 10, int y){ 			//sets the colour of the circle 			g.setColor(Colour.BLUE); 			//draws a circle in JFrame 			g.drawOval(10, y, 50, 50); 			//fills the circle 			g.fillOval(x, y, l, 50); 			repaint(); 		} 		 	} 	 	public static void master(String[] args){ 		Circle circumvolve = new Circle(); 	} }            

I get nada exeption mistake.


Is This A Proficient Question/Topic? 0

  • +

#two blackcompe User is offline

Reputation: 1159

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

Re: Mouse click describe circle

Posted 23 April 2012 - 11:13 PM

Where's the exception thrown?

#three 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

Always post you stack trace. Information technology simplifies our job a lot.

#iv pbl User is offline

Reputation: 8381

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

Re: Mouse click describe circle

Posted 24 Apr 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 pigment() or paintComponent() method.

your public void mouseClicked(MouseEvent evt)
should only set values in example variables that will be 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 draw circle

Posted 25 Apr 2012 - 09:37 AM

Thank yous for your assistance but i still don't know where to add the Graphics object. Hither is my new code with the errors that i am getting. When i debug information technology says that thou is null. And don't mind the extra things i added they are to be used afterward. Cheers again.

parcel circle;  import java.awt.Colour; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Signal; import coffee.awt.upshot.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; 	private JTextArea masterTextArea; 	individual JScrollPane masterScrollPane; 	private int mouseX, mouseY; 	private Point point1, point2; 	Graphics g; 	 	public Circle(){ 		 		setTitle("Circumvolve"); 		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(Colour.Xanthous); 		masterPanel.addMouseListener(handler); 		masterPanel.addMouseMotionListener(handler); 		 		masterTextArea = new JTextArea(); 		masterTextArea.setBackground(Colour.green); 		 		masterScrollPane = new JScrollPane(); 		masterScrollPane.add together(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 10, int y){	 	 		g.setColor(Colour.Blueish); 		g.drawOval(x, y, 50, 50); 	    g.fillOval(ten, y, 50, fifty); 	    repaint(); 	} 	 	individual class HandlerMouse implements MouseListener, MouseMotionListener{ 		 		public void mouseClicked(MouseEvent evt){ 			mouseX = evt.getX(); 	        mouseY = evt.getY(); 	         	        paint(g, mouseX, mouseY); 	         	        //become the coordinates from the mouse click in app 	        Organisation.out.println("Ten: " + mouseX); 	        Arrangement.out.println("Y: " + mouseY); 	         	        //get the number of clicks made 	        System.out.println("Clicked no. times: " + evt.getClickCount()); 	         	        if (point1 != cipher && point2 != zip){	 				int radius1 = (int) (Math.sqrt(Math.prisoner of war(point2.ten-point1.x, two)+Math.pow(point2.y-point1.y, 2))); 				int radius2 = (int) (Math.sqrt(Math.pow(point2.x-point1.10, 2)+Math.prisoner of war(point2.y-point1.y, 2))); 				//drawOval (point1.ten, 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 one, Point ii, int rad1, int rad2) 		{ 			//m.drawOval(1, two, rad1, rad2); 		}  		public void mouseDragged(MouseEvent e)  		{ 		} 		 		public void mouseMoved(MouseEvent e)  		{ 		} 	} 	 	//method for drawing the circle 	/*public void paint(Graphics 1000) 	{ 		//sets the color of the circle 		yard.setColor(Color.BLUE); 		//draws a circle in JFrame 		k.drawOval(200, 200, l, 50); 		//fills the circle 		g.fillOval(200, 200, 50, fifty); 		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" coffee.lang.NullPointerException 	at circle.Circumvolve.paint(Circle.java:57) 	at circle.Circle$HandlerMouse.mouseClicked(Circle.coffee:69) 	at coffee.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 coffee.awt.Component.dispatchEventImpl(Unknown Source) 	at java.awt.Container.dispatchEventImpl(Unknown Source) 	at java.awt.Component.dispatchEvent(Unknown Source) 	at coffee.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 	at coffee.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 coffee.awt.EventQueue.access$000(Unknown Source) 	at java.awt.EventQueue$three.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 coffee.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 coffee.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 	at coffee.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 mail has been edited by Silent Ace: 25 April 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:16 PM

This is how information technology works

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

#7 Silent Ace User is offline

  • New D.I.C Head

Reputation: 2

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

Re: Mouse click draw circle

Posted 26 April 2012 - 06:23 AM

Then i was very close but that i had two parameters added and for some reason that didn't work. Well all is practiced now. Thank you pbl you are a Java God :D.

mcbrydejoher1959.blogspot.com

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