How to call a class that draws a circle in a MouseMoved method ??

Reply

Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

How to call a class that draws a circle in a MouseMoved method ??

 
0
  #1
Feb 27th, 2008
I'm trying to make a circle appear from a class I've developed to move following the x co-ordinate of the mouse. I've done that part correctly, it's just loading the class...Can anyone tell me why it isn't loading?

I do love to solve problems myself but I've been working on this for a long time and I think it might be a silly mistake, I'm new to Java so I would really appreciate the help.

  1.  
  2. class MouseMovedListener implements MouseMotionListener
  3. {
  4. public void mouseMoved(MouseEvent e)
  5. {
  6.  
  7. xposcounter = e.getX();
  8.  
  9. for(int i = 0; i <= grid.length; i++)
  10. {
  11. //Get the left hand side position of the grid
  12. int leftxpos = pCenter.getWidth()/2 - ((GapConstant*(grid.length+6)+(grid.length*rw))/2) + rw*i + GapConstant*i;
  13.  
  14. //Get the right hand side position of the grid
  15. int rightxpos = pCenter.getWidth()/2 + ((GapConstant*(grid.length+6)+(grid.length*rw))/2) +rw*i + GapConstant*i;
  16.  
  17. //Show the counter appearing above the columns the mouse is over
  18. if(xposcounter > leftxpos && xposcounter < rightxpos)
  19. {
  20. //Load the circle
  21. CircleComponent mycounter = new CircleComponent();
  22.  
  23. }
  24. }
  25. }

If you need to see the class code just ask...Thanks in advance for anyone who helps!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,432
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #2
Feb 27th, 2008
I assume CircleComponent provides a method to draw to a graphics context? Just creating an instance of CircleComponent won't do anything for you by itself. Painting operations for a component will occur in paintComponent(Graphics). This means that your paintComponent() method is going to need access to that CircleComponent object so it can call the appropriate method on it to render to the graphics context. Something like
  1. CircleComponent circle;
  2.  
  3. public void paintComponent(Graphics g){
  4. if (circle != null{
  5. circle.draw(g);
  6. }
  7. }
Your mouse move handler can then either create a new CircleComponent, or better yet, alter the properties on a single instance as needed, and call repaint()
  1. //Show the counter appearing above the columns the mouse is over
  2. if(xposcounter > leftxpos && xposcounter < rightxpos)
  3. {
  4. //Load the circle
  5. circle = new CircleComponent();
  6. repaint();
  7. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #3
Feb 27th, 2008
This is what I had for my class circle component excluding your if statement:

  1. package mypackage;
  2. import java.awt.*;
  3. import java.awt.Graphics2D;
  4. import java.awt.Graphics;
  5. import javax.swing.JComponent;
  6. import java.awt.Color;
  7. import java.awt.geom.Ellipse2D;
  8.  
  9. public class CircleComponent extends JComponent
  10. {
  11.  
  12. public CircleComponent() { }
  13.  
  14. private Ellipse2D.Double playerCounter;
  15. private int xpos1 = 10;
  16. private int ypos2 = 10;
  17. private int width = 10;
  18. private int height = 10;
  19.  
  20. public CircleComponent(int w, int h, int x, int y)
  21. {
  22. width = w;
  23. height = h;
  24. xpos1 = x;
  25. ypos2 = y;
  26. }
  27.  
  28. public void paintComponent(Graphics g)
  29. {
  30. g.setColor (Color.red);
  31. playerCounter = new Ellipse2D.Double(30,30,30,30);
  32. setVisible(true);
  33.  
  34. if (playerCounter != null){
  35. playerCounter.draw(g);
  36. }
  37.  
  38. }
  39. }

now I get a connot find symbol error for the .draw part. I think that it because I used Ellipse?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #4
Feb 27th, 2008
Update for anyone still willing to help:
I've stopped getting the error for the class now but still no moving circle. Heres what I did to get rid of my error. My MouseMove code looks just the same but instead I've added repaint() Please help!

  1.  
  2. public void paintComponent(Graphics g)
  3. {
  4. Graphics2D g2 = (Graphics2D) g;
  5. g.setColor (Color.red);
  6. playerCounter = new Ellipse2D.Double(30,30,30,30);
  7. setVisible(true);
  8.  
  9. if (playerCounter != null){
  10. g2.draw(playerCounter);
  11. }
  12. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,432
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #5
Feb 27th, 2008
Ok, you're doing something quite different than I assumed. I'm not sure how you are placing that component withing whatever container you're using, but the paintComponent() method will need to use the Graphics2D.draw(java.awt.Shape) method to render the ellipse
  1. public void paintComponent(Graphics g)
  2. {
  3. Graphics2D g2d = (Graphics2D)g;
  4. g2d.setColor (Color.red);
  5. playerCounter = new Ellipse2D.Double(30,30,30,30);
  6. g2d.draw(playerCounter);
  7. }
Your mouseMoved event will need to postion/size that component as needed in the container and then call repaint() for that container.

Edit: Post collision there, you figured out the Graphics2D part I see. How are you placing this component? It will have to be in a container of some sort for it to be rendered.
Last edited by Ezzaral; Feb 27th, 2008 at 7:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #6
Feb 27th, 2008
Originally Posted by Ezzaral View Post
Ok, you're doing something quite different than I assumed. I'm not sure how you are placing that component withing whatever container you're using, but the paintComponent() method will need to use the Graphics2D.draw(java.awt.Shape) method to render the ellipse
  1. public void paintComponent(Graphics g)
  2. {
  3. Graphics2D g2d = (Graphics2D)g;
  4. g2d.setColor (Color.red);
  5. playerCounter = new Ellipse2D.Double(30,30,30,30);
  6. g2d.draw(playerCounter);
  7. }
Your mouseMoved event will need to postion/size that component as needed in the container and then call repaint() for that container.

Edit: Post collision there, you figured out the Graphics2D part I see. How are you placing this component? It will have to be in a container of some sort for it to be rendered.
I'm using panels as my container...Will panels work for this? I've replaced the code with your paintComponent code...Still no errors but not working. I've tried placing the actual circle to the center panel using "pCenter.add(mycounter);" and refreshing both in the MouseMoved method and within the public void init() method just in case.

P.S. I've also set my locations of the circle "mycounter = new CircleComponent(xposcounter,ypos,rw,rh);" (xposcounter is the position of the mouse, rw = width, rh = height)
Last edited by Cleo123; Feb 27th, 2008 at 7:45 pm. Reason: now = not!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,432
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #7
Feb 27th, 2008
So are you now seeing the circle at all after you have added it to the panel? Note that your paintComponent() method is not actually using the class attributes to create the ellipse, just hard-coded values.

Is there any reason that CircleComponent needs to extend JComponent? You could simply have a draw(Graphics) method in the class which takes the graphics as a parameter (same as it does now) and have your panel's paintComponent() method just call draw() on your CircleComponent and pass along the Graphics reference. This is what I was indicating in the earlier post. You don't have to extend JComponent just to have a method that draws on a graphics context that you pass to it and then you wouldn't have to mess with adding unnecessary components to the panel.
Last edited by Ezzaral; Feb 27th, 2008 at 8:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #8
Feb 27th, 2008
I'm a little lost now. When I get rid of extends JComponent it doesn't like "pCenter.add(mycounter);" but even when I have got that it's not doing anything; not even letting a circle appear. I haven't used the same x,y,h,w attributes as in the class because the variables I want to use are in the main class and it has problems when I try to use them in the separate CircleComponent class. I haven't defined the same variables from the main class into the separate class just in case the value of the variables change.

My center panel doesn't have it's very own paintComponent() method because its just under public ProgramProj() {pNorth = new JPanel(); ...} My application does have its own public void paint (Graphics graf) but I don't know how it can draw the circle based on the x position of the mouse as it needs MouseMoved(MouseEvent e).

I am actually trying to make a connect 4 game where the counter moves above the grid where the mouse is over the grid.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,432
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #9
Feb 27th, 2008
Perhaps if you could post a screen shot of the main screen with a bit of explanation of the component hierarchy it would help. I think you are probably making it more difficult on yourself than it needs to be, but without knowing a little more of the structure I can't recommend too much.
Last edited by Ezzaral; Feb 27th, 2008 at 10:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #10
Feb 28th, 2008
  1.  
  2. import mypackage.*;
  3.  
  4. import java.awt.event.*;
  5. import java.awt.*;
  6. import javax.swing.event.MouseInputListener;
  7. import java.awt.event.MouseEvent;
  8.  
  9. import javax.swing.*;
  10. import java.awt.Color;
  11. import java.awt.Shape;
  12. import java.awt.Rectangle;
  13. import java.awt.geom.Line2D;
  14. import java.awt.geom.Arc2D;
  15. import java.awt.geom.Point2D;
  16.  
  17. import javax.swing.JApplet;
  18. import javax.swing.*;
  19. import java.awt.Graphics2D;
  20. import java.awt.Graphics;
  21. import javax.swing.JComponent;
  22. import javax.swing.JPanel;
  23. import javax.swing.JFrame;
  24.  
  25. public class ProgramProj extends JFrame implements ActionListener, MouseInputListener
  26. {
  27. JPanel pNorth;
  28. JPanel row1;
  29. JPanel row2;
  30. JPanel pSouth;
  31. JPanel pEast;
  32. JPanel pWest;
  33. JPanel pCenter;
  34. JFrame frame;
  35. CircleComponent mycounter;
  36. Boolean c;
  37.  
  38. Shape[][] grid; // Grid array
  39. int rw; // Grid circles width
  40. int rh; // Grid circles height
  41. int GapConstant; // Gap between each circle in the grid
  42. int xpos; // x position of grid
  43. int ypos; // y position of grid
  44. int xcolpos; // Position of where column chosen
  45. int ycolpos; // Position of where column chosen
  46. int xposcounter; // Position of the moving counter
  47. int cypos;
  48. int leftxpos;
  49. int rightxpos;
  50. int counterxpos;
  51.  
  52. int[][] x;
  53.  
  54. /* if(xcolpos >= xpos -(GapConstant/2) && xcolpos <= xpos + (rw+(GapConstant/2)))
  55.   {
  56.   System.out.println("Column 1");
  57.   }
  58.   else
  59.   {
  60.   System.out.println("Not Column 1");
  61.   }
  62. */
  63. public class MouseMovedListener implements MouseMotionListener
  64. {
  65. public void mouseMoved(MouseEvent e)
  66. {
  67.  
  68.  
  69.  
  70. }
  71. //http://hivemind.apache.org/hivemind1/hivemind/apidocs/src-html/org/apache/hivemind/HiveMind.html#line.158
  72.  
  73.  
  74. public void mouseDragged(MouseEvent e) {}
  75.  
  76. }
  77.  
  78. public void mouseClicked(MouseEvent e)
  79. {
  80. xcolpos = e.getX(); //get the x coordinate after mouse is clicked on the applet
  81. calculatecolumn(); //call the calculate column method
  82. }
  83.  
  84. public void mouseMoved(MouseEvent e) {
  85.  
  86. xposcounter = e.getX();
  87.  
  88. for(int i = 0; i <= grid.length; i++)
  89. {
  90. //Center the grid
  91. leftxpos = pCenter.getWidth()/2 - ((GapConstant*(grid.length)+(grid.length*rw))/2) + rw*i + GapConstant*i;
  92. rightxpos = leftxpos +rw;
  93.  
  94. //Show the counter appearing above the columns the mouse is over
  95. if(xposcounter >= leftxpos && xposcounter <= rightxpos)
  96. {
  97. counterxpos = xposcounter;
  98. mycounter = new CircleComponent(counterxpos, ypos, rw, rh);
  99. //mycounter.translate(xposcounter, ypos);
  100. repaint();
  101. System.out.println(counterxpos);
  102. c = true;
  103. //graf.drawOval(leftxpos, ypos, 10, 10);
  104. //pCenter.add(circle);
  105. //mycounter.setVisible(true);
  106.  
  107. }
  108. }
  109.  
  110.  
  111. }
  112.  
  113. public static void main(String args[])
  114. {
  115. new ProgramProj();
  116. }
  117.  
  118.  
  119. public void mouseExited(MouseEvent e) {}
  120.  
  121. public void mouseEntered(MouseEvent e) {}
  122.  
  123. public void mouseReleased(MouseEvent e) {}
  124.  
  125. public void mousePressed(MouseEvent e) {}
  126.  
  127. public void mouseDragged(MouseEvent e) {}
  128.  
  129. public ProgramProj()
  130. {
  131. setSize(1000,700);
  132. c = new Boolean(false);
  133. this.addMouseMotionListener(this);
  134. this.addMouseListener(this);
  135.  
  136. this.setLayout (new BorderLayout());
  137.  
  138. pNorth = new JPanel();
  139. add(pNorth, BorderLayout.NORTH);
  140. row1 = new JPanel();
  141. row2 = new JPanel();
  142. pSouth = new JPanel();
  143. add(pSouth, BorderLayout.SOUTH);
  144. pEast = new JPanel();
  145. add(pEast, BorderLayout.EAST);
  146. pWest = new JPanel();
  147. add(pWest, BorderLayout.WEST);
  148. pCenter = new JPanel();
  149. add(pCenter, BorderLayout.CENTER);
  150.  
  151. pNorth.setLayout (new BorderLayout());
  152. pNorth.setBackground(Color.white);
  153.  
  154. row1.setBackground(Color.white);
  155. row2.setBackground(Color.white);
  156.  
  157. pSouth.setLayout (new BorderLayout());
  158. pSouth.setBackground(Color.white);
  159.  
  160. pEast.setLayout (new BorderLayout());
  161. pEast.setBackground(Color.white);
  162.  
  163. pWest.setLayout (new BorderLayout());
  164. pWest.setBackground(Color.white);
  165.  
  166. pCenter.setLayout (new BorderLayout());
  167. pCenter.setBackground(Color.white);
  168.  
  169. JLabel one = new JLabel("Titles");
  170.  
  171. add(pNorth, BorderLayout.NORTH);
  172. pNorth.add(row1, BorderLayout.NORTH);
  173. pNorth.add(row2, BorderLayout.SOUTH);
  174.  
  175. pNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  176. pEast.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  177. pSouth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  178. pWest.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  179. pCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
  180.  
  181. JLabel two = new JLabel("a");
  182. pSouth.add(two);
  183.  
  184. JLabel three = new JLabel("a");
  185. pEast.add(three);
  186.  
  187. JLabel four = new JLabel("a");
  188. pWest.add(four);
  189.  
  190. JButton cmdCustomer = new JButton("Add Customer");
  191. row2.add(cmdCustomer);
  192.  
  193. JButton cmdEmployee = new JButton("Add Employee");
  194. row2.add(cmdEmployee);
  195.  
  196. JButton cmdFinance = new JButton("Finance");
  197. row2.add(cmdFinance);
  198.  
  199. JButton cmdDiscounts = new JButton("Discounts");
  200. row2.add(cmdDiscounts);
  201.  
  202. ImageIcon picture = new ImageIcon("mebw.jpg");
  203. JLabel five = new JLabel(picture);
  204.  
  205. pCenter.add(five, BorderLayout.CENTER);
  206.  
  207. ///GRID////
  208. GapConstant = 10; //Gap between each circle in the grid
  209. rw = 60; //Circles width in the grid
  210. rh = 60; //Circles height in the grid
  211.  
  212. grid = new Shape[6][5];
  213.  
  214. x = new int[5][5];
  215.  
  216. for(int row = 0; row<5;row++)
  217. {
  218. for(int column =0; column<5;column++)
  219. {
  220. x[row][column] = 0;
  221.  
  222. }
  223. }
  224.  
  225. x[1][3] =1;
  226. showgrid();
  227.  
  228. setVisible(true);//SET APPLICATION FRAME VISIBLE
  229.  
  230. }
  231.  
  232. public void paint (Graphics graf)
  233. {
  234.  
  235. super.paint(graf);
  236.  
  237. for(int i = 0; i <= grid.length; i++)
  238. {
  239. //Center the grid
  240. xpos = pCenter.getWidth()/2 - ((GapConstant*(grid.length+6)+(grid.length*rw))/2) + rw*i + GapConstant*i;
  241.  
  242. for(int j = 0; j <= grid[0].length; j++)
  243.  
  244. {
  245. ypos = pCenter.getHeight()/2 - ((GapConstant*(grid.length-10)+(grid.length*rh))/2) + rh*j + GapConstant*j;
  246.  
  247. //Draw array of ovals
  248. graf.drawOval(xpos,ypos, rw, rh);
  249. //Draw vertical lines from right to left
  250. graf.drawLine(xpos+(rw+GapConstant/2),ypos-GapConstant/2,xpos+(rw+GapConstant/2),ypos+(rh+GapConstant/2));
  251. //Draw vertical lines from left to right
  252. graf.drawLine(xpos-(GapConstant/2),ypos-GapConstant/2,xpos-(GapConstant/2),ypos+(rh+GapConstant/2));
  253. //Draw horizontal lines from top to bottom
  254. graf.drawLine(xpos-(GapConstant/2),ypos-GapConstant/2,xpos+(rw+GapConstant/2),ypos-GapConstant/2);
  255. //Draw horitonzal lines from bottom to top
  256. graf.drawLine(xpos-(GapConstant/2),ypos+(rw+GapConstant/2),xpos+(rw+GapConstant/2),ypos+(rw+GapConstant/2));
  257. }
  258. }
  259. }

class

  1.  
  2. package mypackage;
  3. import java.awt.*;
  4. import java.awt.Graphics2D;
  5. import java.awt.Graphics;
  6. import javax.swing.JComponent;
  7. import java.awt.Color;
  8. import java.awt.geom.Ellipse2D;
  9. import java.awt.BasicStroke;
  10.  
  11. public class CircleComponent extends JComponent
  12. {
  13.  
  14. public CircleComponent() { }
  15.  
  16. private int xpos1 = 10;
  17. private int ypos2 = 10;
  18. private int width = 10;
  19. private int height = 10;
  20. final BasicStroke stroke = new BasicStroke(2.0f);
  21.  
  22. public CircleComponent(int w, int h, int x, int y)
  23. {
  24. width = w;
  25. height = h;
  26. xpos1 = x;
  27. ypos2 = y;
  28. }
  29.  
  30. public void paintComponent(Graphics g)
  31. {
  32. Graphics2D g2 = (Graphics2D) g;
  33. g2.setStroke(stroke);
  34. g2.setColor (Color.red);
  35. //playerCounter = new Ellipse2D.Double(30,30,30,30);
  36. g2.draw(new Ellipse2D.Double(30,30,30,30));
  37. setVisible(true);
  38. repaint();
  39.  
  40. }
  41.  
  42. private Ellipse2D.Double playerCounter;
  43.  
  44. }

Thats most of my code, hopefully that'll give you an idea of where I've gone wrong.
Thanks for the help you've given me already.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC