| | |
How to call a class that draws a circle in a MouseMoved method ??
![]() |
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
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.
If you need to see the class code just ask...Thanks in advance for anyone who helps!
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.
Java Syntax (Toggle Plain Text)
class MouseMovedListener implements MouseMotionListener { public void mouseMoved(MouseEvent e) { xposcounter = e.getX(); for(int i = 0; i <= grid.length; i++) { //Get the left hand side position of the grid int leftxpos = pCenter.getWidth()/2 - ((GapConstant*(grid.length+6)+(grid.length*rw))/2) + rw*i + GapConstant*i; //Get the right hand side position of the grid int rightxpos = pCenter.getWidth()/2 + ((GapConstant*(grid.length+6)+(grid.length*rw))/2) +rw*i + GapConstant*i; //Show the counter appearing above the columns the mouse is over if(xposcounter > leftxpos && xposcounter < rightxpos) { //Load the circle CircleComponent mycounter = new CircleComponent(); } } }
If you need to see the class code just ask...Thanks in advance for anyone who helps!
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 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()
Java Syntax (Toggle Plain Text)
CircleComponent circle; public void paintComponent(Graphics g){ if (circle != null{ circle.draw(g); } }
Java Syntax (Toggle Plain Text)
//Show the counter appearing above the columns the mouse is over if(xposcounter > leftxpos && xposcounter < rightxpos) { //Load the circle circle = new CircleComponent(); repaint(); }
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
This is what I had for my class circle component excluding your if statement:
now I get a connot find symbol error for the .draw part. I think that it because I used Ellipse?
Java Syntax (Toggle Plain Text)
package mypackage; import java.awt.*; import java.awt.Graphics2D; import java.awt.Graphics; import javax.swing.JComponent; import java.awt.Color; import java.awt.geom.Ellipse2D; public class CircleComponent extends JComponent { public CircleComponent() { } private Ellipse2D.Double playerCounter; private int xpos1 = 10; private int ypos2 = 10; private int width = 10; private int height = 10; public CircleComponent(int w, int h, int x, int y) { width = w; height = h; xpos1 = x; ypos2 = y; } public void paintComponent(Graphics g) { g.setColor (Color.red); playerCounter = new Ellipse2D.Double(30,30,30,30); setVisible(true); if (playerCounter != null){ playerCounter.draw(g); } } }
now I get a connot find symbol error for the .draw part. I think that it because I used Ellipse?
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
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!
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!
Java Syntax (Toggle Plain Text)
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g.setColor (Color.red); playerCounter = new Ellipse2D.Double(30,30,30,30); setVisible(true); if (playerCounter != null){ g2.draw(playerCounter); } }
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 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.
Java Syntax (Toggle Plain Text)
public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setColor (Color.red); playerCounter = new Ellipse2D.Double(30,30,30,30); g2d.draw(playerCounter); }
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.
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
•
•
•
•
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 ellipseYour mouseMoved event will need to postion/size that component as needed in the container and then call repaint() for that container.Java Syntax (Toggle Plain Text)
public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setColor (Color.red); playerCounter = new Ellipse2D.Double(30,30,30,30); g2d.draw(playerCounter); }
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.
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!
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.
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.
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
import mypackage.*; import java.awt.event.*; import java.awt.*; import javax.swing.event.MouseInputListener; import java.awt.event.MouseEvent; import javax.swing.*; import java.awt.Color; import java.awt.Shape; import java.awt.Rectangle; import java.awt.geom.Line2D; import java.awt.geom.Arc2D; import java.awt.geom.Point2D; import javax.swing.JApplet; import javax.swing.*; import java.awt.Graphics2D; import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JPanel; import javax.swing.JFrame; public class ProgramProj extends JFrame implements ActionListener, MouseInputListener { JPanel pNorth; JPanel row1; JPanel row2; JPanel pSouth; JPanel pEast; JPanel pWest; JPanel pCenter; JFrame frame; CircleComponent mycounter; Boolean c; Shape[][] grid; // Grid array int rw; // Grid circles width int rh; // Grid circles height int GapConstant; // Gap between each circle in the grid int xpos; // x position of grid int ypos; // y position of grid int xcolpos; // Position of where column chosen int ycolpos; // Position of where column chosen int xposcounter; // Position of the moving counter int cypos; int leftxpos; int rightxpos; int counterxpos; int[][] x; /* if(xcolpos >= xpos -(GapConstant/2) && xcolpos <= xpos + (rw+(GapConstant/2))) { System.out.println("Column 1"); } else { System.out.println("Not Column 1"); } */ public class MouseMovedListener implements MouseMotionListener { public void mouseMoved(MouseEvent e) { } //http://hivemind.apache.org/hivemind1/hivemind/apidocs/src-html/org/apache/hivemind/HiveMind.html#line.158 public void mouseDragged(MouseEvent e) {} } public void mouseClicked(MouseEvent e) { xcolpos = e.getX(); //get the x coordinate after mouse is clicked on the applet calculatecolumn(); //call the calculate column method } public void mouseMoved(MouseEvent e) { xposcounter = e.getX(); for(int i = 0; i <= grid.length; i++) { //Center the grid leftxpos = pCenter.getWidth()/2 - ((GapConstant*(grid.length)+(grid.length*rw))/2) + rw*i + GapConstant*i; rightxpos = leftxpos +rw; //Show the counter appearing above the columns the mouse is over if(xposcounter >= leftxpos && xposcounter <= rightxpos) { counterxpos = xposcounter; mycounter = new CircleComponent(counterxpos, ypos, rw, rh); //mycounter.translate(xposcounter, ypos); repaint(); System.out.println(counterxpos); c = true; //graf.drawOval(leftxpos, ypos, 10, 10); //pCenter.add(circle); //mycounter.setVisible(true); } } } public static void main(String args[]) { new ProgramProj(); } public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public ProgramProj() { setSize(1000,700); c = new Boolean(false); this.addMouseMotionListener(this); this.addMouseListener(this); this.setLayout (new BorderLayout()); pNorth = new JPanel(); add(pNorth, BorderLayout.NORTH); row1 = new JPanel(); row2 = new JPanel(); pSouth = new JPanel(); add(pSouth, BorderLayout.SOUTH); pEast = new JPanel(); add(pEast, BorderLayout.EAST); pWest = new JPanel(); add(pWest, BorderLayout.WEST); pCenter = new JPanel(); add(pCenter, BorderLayout.CENTER); pNorth.setLayout (new BorderLayout()); pNorth.setBackground(Color.white); row1.setBackground(Color.white); row2.setBackground(Color.white); pSouth.setLayout (new BorderLayout()); pSouth.setBackground(Color.white); pEast.setLayout (new BorderLayout()); pEast.setBackground(Color.white); pWest.setLayout (new BorderLayout()); pWest.setBackground(Color.white); pCenter.setLayout (new BorderLayout()); pCenter.setBackground(Color.white); JLabel one = new JLabel("Titles"); add(pNorth, BorderLayout.NORTH); pNorth.add(row1, BorderLayout.NORTH); pNorth.add(row2, BorderLayout.SOUTH); pNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); pEast.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); pSouth.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); pWest.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); pCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); JLabel two = new JLabel("a"); pSouth.add(two); JLabel three = new JLabel("a"); pEast.add(three); JLabel four = new JLabel("a"); pWest.add(four); JButton cmdCustomer = new JButton("Add Customer"); row2.add(cmdCustomer); JButton cmdEmployee = new JButton("Add Employee"); row2.add(cmdEmployee); JButton cmdFinance = new JButton("Finance"); row2.add(cmdFinance); JButton cmdDiscounts = new JButton("Discounts"); row2.add(cmdDiscounts); ImageIcon picture = new ImageIcon("mebw.jpg"); JLabel five = new JLabel(picture); pCenter.add(five, BorderLayout.CENTER); ///GRID//// GapConstant = 10; //Gap between each circle in the grid rw = 60; //Circles width in the grid rh = 60; //Circles height in the grid grid = new Shape[6][5]; x = new int[5][5]; for(int row = 0; row<5;row++) { for(int column =0; column<5;column++) { x[row][column] = 0; } } x[1][3] =1; showgrid(); setVisible(true);//SET APPLICATION FRAME VISIBLE } public void paint (Graphics graf) { super.paint(graf); for(int i = 0; i <= grid.length; i++) { //Center the grid xpos = pCenter.getWidth()/2 - ((GapConstant*(grid.length+6)+(grid.length*rw))/2) + rw*i + GapConstant*i; for(int j = 0; j <= grid[0].length; j++) { ypos = pCenter.getHeight()/2 - ((GapConstant*(grid.length-10)+(grid.length*rh))/2) + rh*j + GapConstant*j; //Draw array of ovals graf.drawOval(xpos,ypos, rw, rh); //Draw vertical lines from right to left graf.drawLine(xpos+(rw+GapConstant/2),ypos-GapConstant/2,xpos+(rw+GapConstant/2),ypos+(rh+GapConstant/2)); //Draw vertical lines from left to right graf.drawLine(xpos-(GapConstant/2),ypos-GapConstant/2,xpos-(GapConstant/2),ypos+(rh+GapConstant/2)); //Draw horizontal lines from top to bottom graf.drawLine(xpos-(GapConstant/2),ypos-GapConstant/2,xpos+(rw+GapConstant/2),ypos-GapConstant/2); //Draw horitonzal lines from bottom to top graf.drawLine(xpos-(GapConstant/2),ypos+(rw+GapConstant/2),xpos+(rw+GapConstant/2),ypos+(rw+GapConstant/2)); } } }
class
Java Syntax (Toggle Plain Text)
package mypackage; import java.awt.*; import java.awt.Graphics2D; import java.awt.Graphics; import javax.swing.JComponent; import java.awt.Color; import java.awt.geom.Ellipse2D; import java.awt.BasicStroke; public class CircleComponent extends JComponent { public CircleComponent() { } private int xpos1 = 10; private int ypos2 = 10; private int width = 10; private int height = 10; final BasicStroke stroke = new BasicStroke(2.0f); public CircleComponent(int w, int h, int x, int y) { width = w; height = h; xpos1 = x; ypos2 = y; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setStroke(stroke); g2.setColor (Color.red); //playerCounter = new Ellipse2D.Double(30,30,30,30); g2.draw(new Ellipse2D.Double(30,30,30,30)); setVisible(true); repaint(); } private Ellipse2D.Double playerCounter; }
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.
![]() |
Other Threads in the Java Forum
- Previous Thread: exceptions
- Next Thread: Splitting double into its integer portion and decimal portion
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






