Results 1 to 12 of 12

Thread: [RESOLVED]Java draw circles on image

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Resolved [RESOLVED]Java draw circles on image

    hello, i found the answer to my last post finally. now i have a new task to implement. I need to draw a circle or oval when a point on an image is clicked. For instance if you were to click on the eye of a person in a image in a jpanel, i need to draw a red circle of where i just clicked. i am researdhing and figured out this, and i can post more of my code when i have more time, for i am pressed right now...


    some thing laong the lines of...

    Code:
            setColor(red);
    	fillCircle(newX,newY, 20);

    my only question now si what does the 20 mean ?

    remind you i am very new to java, but i will sit down later with my manual to try to implement. I'm sure it would be more helpful if i posted my code, but If anyone knows of what along the lines I am trying to do you willingness to help would be appercaited thanks. justin
    Last edited by jlbovo; Nov 4th, 2006 at 11:18 AM.
    --thanks for the help.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java draw circles on image

    I'm almost sure you didn't write any of the code, because if you did. You'd know that fillCircle is not Java built-in method. And since you are not calling it from any other object then I'd take a wild guess and say the body of the method is in your class so take another look and tell us what does it look like!!
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java draw circles on image

    i know, it not mine at all, i was just running thru a manual trying to get an idea of what i'm going for, just so other know. here's my code....

    Code:
    private class mouseImgEventHandler implements MouseListener, MouseMotionListener {
    
          //empty body of methods.
          public void mousePressed(MouseEvent e) {}
          public void mouseReleased(MouseEvent e) {}
          public void mouseEntered(MouseEvent e) {}
          public void mouseExited(MouseEvent e) {}
          public void mouseMoved(MouseEvent e) {}
          public void mouseDragged(MouseEvent e) {}
    
          //get X-Y location when mouse is clicked.
          public void mouseClicked(MouseEvent e) {
             int currentX = e.getX();
             int currentY = e.getY();
    
             //display X-Y on the message board.
             if(panelID == 1)
                parent.leftDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
             if(panelID == 2)
                parent.rightDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
             if(panelID == 3)
                parent.resultDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
             //repaint();
          }
       }
    i just put that up the as an example of what i am tryign to do.

    based on the x/y coordinates i get when i click the mouse, I want to draw circles..

    Code:
     public void paintComponent(Graphics graphics) {
           Graphics2D g = (Graphics2D)graphics.create();
           // eyes
           g.setColor(Color.red);
           g.fillOval(30, 30, 8, 8);
           g.fillOval(62, 30, 8, 8);


    .but how do i pass the mouse coordnates to the drawing of the oval ?
    Code:
         if(panelID == 1)
                parent.leftDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
           // eyes
           g.setColor(Color.red);
           g.fillOval(30, 30, 8, 8);
           g.fillOval(62, 30, 8, 8);
    and where would i incldue a line like this
    Code:
    public void paintComponent(Graphics graphics) {
           Graphics2D g = (Graphics2D)graphics.create();
    ...at the begginign of my mouse handler, or some where else ?



    does that clear my idea more, i thanks you again for stopping to help. justin
    --thanks for the help.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java draw circles on image

    but how do i pass the mouse coordnates to the drawing of the oval ?
    Are you serious??
    Code:
             int currentX = e.getX();
             int currentY = e.getY();
    and where would i incldue a line like this
    Anywhere you want, just make sure you declare the object before you start using it
    And BTW it's
    Code:
    Graphics2D g = (Graphics2D)graphics;
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java draw circles on image

    i'm sorry so sorry, like i say i am a begginer. No formal learning/training in this.

    so....

    Code:
           if(panelID == 1)
           parent.leftDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
           // eyes
           g.setColor(Color.red);
           g.fillOval(currentX, currentY, 8, 8);
    i think thats what i'm going for, will that draw a small oval on the spot that i click ??
    thanks again - justin
    --thanks for the help.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java draw circles on image

    here's my code

    Code:
    // mouse event handler, as an inner class of ImagePanel class.
       // 
       private class mouseImgEventHandler implements MouseListener, MouseMotionListener {
    
          //empty body of methods.
          public void mousePressed(MouseEvent e) {}
          public void mouseReleased(MouseEvent e) {}
          public void mouseEntered(MouseEvent e) {}
          public void mouseExited(MouseEvent e) {}
          public void mouseMoved(MouseEvent e) {}
          public void mouseDragged(MouseEvent e) {}
    
          //get X-Y location when mouse is clicked.
          public void mouseClicked(MouseEvent e) {
             int currentX = e.getX();
             int currentY = e.getY();
    	Graphics2D g = (Graphics2D)graphics;
    
             //display X-Y on the message board.
             if(panelID == 1)
                parent.leftDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
    		// eyes
           		g.setColor(Color.red);
           		g.fillOval(currentX, currentY, 8, 8);
             if(panelID == 2)
                parent.rightDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
             if(panelID == 3)
                parent.resultDisplay.setText("X:  " + currentX + ",   Y:  " + currentY);
             //repaint();
          }
       }
    here's my errors...
    UIDDemo1.java:349: cannot find symbol
    symbol : variable graphics
    location: class ImagePanel.mouseImgEventHandler
    Graphics2D g = (Graphics2D)graphics;
    ^
    1 error
    .....i will continue working on this code and i'm on my way to work. i thanks you for all your time and effort !!! justin
    --thanks for the help.

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java draw circles on image

    replace
    Code:
    Graphics2D g = (Graphics2D)graphics;
    with
    Code:
    Graphics2D g = (Graphics2D)this.getGraphics();
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java draw circles on image

    thaks again but i keep gettin an error...
    Code:
    UIDDemo1.java:350: cannot find symbol
    symbol  : method getGraphics()
    location: class ImagePanel.mouseImgEventHandler
            Graphics2D g = (Graphics2D)this.getGraphics();
                                       ^
    1 error
    ..do i need to import something at the begging of the file so i can call Graphics ? -justin
    --thanks for the help.

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java draw circles on image

    Does your class extend "JFrame, JPanel, Panel, Applet or ...etc"? This method "getGraphics" is inherited from the component class
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java draw circles on image

    it's a class that uses jpanel..

    Code:
    class ImagePanel extends JPanel {
    --thanks for the help.

  11. #11
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java draw circles on image

    remove the "this", just use the "getGraphics();"
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java draw circles on image

    awesome dude, thansk for all your help man. it works, it draws the circles !! i am so thanksful, I have learned alot from you over this. Thanks again for helping...I'm sure i'll have more questions, but now this topics is resolved. if i need more info i'll be sure to ask again ! - justin
    --thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width