Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:34:34+00:00 2026-06-15T13:34:34+00:00

I want to use canvas inside mousePressed . How can I do that? public

  • 0

I want to use canvas inside mousePressed. How can I do that?

public DragManager(Canvas canvas)
{
    canvas.addMouseListener(new MouseAdapter() {
        @Override public void mousePressed(MouseEvent e)
        {
            canvas.something(); // does not work.
        }
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:34:35+00:00Added an answer on June 15, 2026 at 1:34 pm

    As many of the guys over here already said you have to make function parameter final.

    public DragManager(final Canvas canvas)
    {
        canvas.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e)
            {
                canvas.something();
            }
        });
    }
    

    That means that this variable cannot point to any other object. E.g. you cannot do this inside function:

    canvas = SomeOtherCanvas
    

    If you create an object using a local class definition, that object can keep “living” after local variables have been discarded from the stack (after DragManager constructor completion). It has to have a copy of the local values. If you make this parameter final (so it’s guaranteed that reference inside constructor wouldn’t point to some other place) it’s really easy to have a copy: just copy a reference. If there was no such rule you (well, not you personally, but Java language) would need to constantly sync those values and that would be much more complex and slow solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use many bitmap for my game canvas. I can't Recycle bitmaps, because
I want to use certain animations such as bounce inside my canvas. Is it
I want to use Canvas.TextRect to write something on the canvas with 90 degree
I want to create a dynamic HTML5 canvas animation. This animation should use server
I want use BYTE_ORDER macro in my Xcode project but i can't because i
I have a canvas.DrawText. I want to use gestures to dynamically change the text
JSFiddle: http://jsfiddle.net/M2ALY/3/ My goal is to make a module that I can use and
I have a canvas that contains several bitmap images, I want the user to
I want to use Canvas class's drawPicture(Picture picture, Rect dst) method. Now I don't
I want to use the canvas api on a map designed in Adobe illustrator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.