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 8656903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:23:38+00:00 2026-06-12T15:23:38+00:00

I seem to have a problem with references to java objects, probably as a

  • 0

I seem to have a problem with references to java objects, probably as a result of years of programming in C. The following code is supposed to enable me to move objects of “WordPart” around a canvas. When I choose the first object, it moves as expected. When I release the first object, and choose the second, the location of of both the first and the second objects becomes identical. (I have removed code needed for compilation, but irrelevant to the question.) As far as I can tell, the activeImage reference seems to point to both objects in the wordPartList simultaneously. Please help me find my misunderstanding.

...
public class ONG2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new SurfaceView2(this));
}

public class SurfaceView2 extends SurfaceView implements SurfaceHolder.Callback {    

ArrayList<WordParts> wordPartList;
WordParts activeImage;
Point activePosition;
String TAG = "SurfaceView2";
public SurfaceView2(Context context) {
super(context);        
wordPartList = NewPartList(context);
this.getHolder().addCallback(this);
setFocusable(true);
activeImage = null;
activePosition = new Point();
}
/**
 * 
 * @return list of word parts
 */
private ArrayList<WordParts> NewPartList(Context context) {
ArrayList<WordParts> newParts = new ArrayList<WordParts>();
newParts.add(new WordParts(context, new Point(400, 100), "foo"));
newParts.add(new WordParts(context, new Point(200, 100), "bar");
return newParts;
}
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "inside onDraw");
canvas.drawColor(Color.BLACK);
if (activeImage != null) {
activeImage.NewPosition(activePosition);
}
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(100, 300));
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(200, 300));
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(300, 300));
for (WordParts wp : wordPartList) {
Point center = wp.getCenter();
Log.d(TAG, String.format("Word part %s at x=%d, y=%d", wp.mWordPartString, center.x, center.y));
draw(canvas, wp.getBitMap(), center);
}

}     
@Override    
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, String.format("inside on touch, event is %s", event.toString()));
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// if within wordPart, set as active
for (WordParts wp : wordPartList) {
if (wp.isInside(event)) {
activeImage = wp;
Log.d(TAG, String.format("Touch down inside word part %s", wp.mWordPartString));
break; // end iteration
}
}

break;
case MotionEvent.ACTION_UP:
for (WordParts wp : wordPartList) {
if (activeImage == wp){
wp.endTouch();
Log.d(TAG, String.format("Released word part %s", wp.mWordPartString));
}
}
activeImage = null;
break;

default:
break;
}
activePosition.x = (int) event.getX();
activePosition.y = (int) event.getY();
if (activeImage != null)
this.postInvalidate(); // force call to onDraw
return true;
}

/**
 * Draw the bitmap centered at specified point
 * @param canvas - target of drawing
 * @param bm - bitmap to draw
 * @param centerPt
 */
public void draw(Canvas canvas, Bitmap bm, Point centerPt) {
canvas.drawBitmap( bm, 
   centerPt.x - (bm.getWidth() / 2), 
   centerPt.y - (bm.getHeight() / 2), null); 
Log.d(TAG, String.format("canvas %s, bm %s, point %s", canvas.toString(), bm.toString(), centerPt.toString()));
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) 
{     

}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("surface", "Surface created");        
setWillNotDraw(false);
            }
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("surface", "Surface destroyed");
}
}
}
  • 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-12T15:23:40+00:00Added an answer on June 12, 2026 at 3:23 pm

    I suspect this is the problem:

    if (activeImage != null) {
        activeImage.NewPosition(activePosition);
    }
    

    Unfortunately we don’t have the code for unconventionally-named NewPosition, but presumably it sets the value of a field in a WordParts. If you set the value of two fields within two separate WordParts objects to refer to the same Point, then later when you change the values within that Point like this:

    activePosition.x = (int) event.getX();
    activePosition.y = (int) event.getY();
    

    then yes, those changes will be visible via both WordParts objects, as they both refer to the same object.

    If you’re new to Java, I would strongly advise you to learn the basics of the language through simple console apps. Learn about objects, references and primitives, inheritance, the core collections and IO etc, then start working in the trickier environment of UIs and mobile.

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

Sidebar

Related Questions

I have a problem with my cfml code. The ListAppend() function doesn't seem to
I seem to have a problem passing some strings on from one form to
I seem to have a problem with getting MVC to fill in my custom
I seem to have a problem understanding how to conditionally test a boolean value
I seem to have a tricky problem since the latest ADT update to release
I have a problem which I cant seem to find answer to through searches
I have a problem which i can't seem to find the solution to. I
I seem to have the exact opposite problem than this question on stopping dock
I have a small problem which i can't seem to figure out. I tried
I have a small problem which i can't seem to solve myself. Look at

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.