Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Drawing in response to mouse input

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing in response to mouse input

    I want to draw in response to mouse events. The only way I know how to draw is using a Graphics2D object, as passed to paintComponent(). I tried saving that object in an instance variable, and then using it in mouse event handlers, but no drawing occurs. What am I doing wrong?

    Is there a way to get a Graphics2D object from a panel or a frame? Where does paintComponent() get invoked and where does the Graphics2D object come from?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Drawing in response to mouse input

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in response to mouse input

    I read this, and it doesn't answer my question.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Drawing in response to mouse input

    Think of it from a Model/View/Controller perspective (which Swing is based upon). The view is the JComponent you are drawing to through the paintComponent method. The controller is some type of user input, for instance a MouseListener. The workflow would then be to have the Controller update the model, then tell the view to do the drawing. For instance, if you want to draw a dot where the user's mouse clicked, 1) get the point from the mouse listener 2) save it to your 'model' 3) call repaint on the component 4) within the paintComponent, draw your model (in this case the point). The link Kevin provided above has a great example of doing just this.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Drawing in response to mouse input

    Quote Originally Posted by daved View Post
    I read this, and it doesn't answer my question.
    I read this reply, and it doesn't ask a question.

    You need to be more specific- the tutorial I posted tells you exactly how to obtain a Graphics instance to draw with, and how to setup an application that performs custom drawing. What part of that confused you? What did you try?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. servlet response
    By booj in forum Java Servlet
    Replies: 3
    Last Post: April 12th, 2012, 04:05 AM
  2. httpServlet response
    By lorik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 10:29 AM
  3. How Manipulate HTTP response?
    By nikos in forum Java Networking
    Replies: 1
    Last Post: October 7th, 2010, 12:22 PM
  4. empty response received
    By grashmi13 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 16th, 2010, 05:30 PM
  5. Not able to receive SOAP response
    By abhisekh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 12th, 2010, 07:20 AM

Tags for this Thread