AidanSalvatore
AidanSalvatore

Reputation: 53

Updating X & Y coordinates when clicking a button

I would like to build an application where user can build his own path using buttons. I have problems with updating X and Y coordinates, basically i don't know how can i make it. What i have done so far - lines are drawing in the same point because my coordinates are not updating yet. I made some tests with get and set methods but my thinking was not correct so i did not put this in a code to avoid errors. What i want to achieve: lines got two points - starting point and ending point, when i press the button, line is drawing on canvas, when i press another button, another line should be drawn but not in the same coordinates as the first one but in ending point of previous one etc.

For example: when i press top button:

   canvas.drawLine(300,300,300, 250,paint);

when i press right button:

canvas.drawLine(300,250,350, 250,paint);

when i press top button again:

canvas.drawLine(350,250,350, 200,paint);

Ending point of one line should be starting point of another one and button click adding or substracting value 50 to X or Y coordinate depending on the direction.

I am adding screens showing how it works now and how i would like to make it. I am looking for any help or tips.

Buttons right and left are in wrong places but it is not important here

public class MainActivity extends ActionBarActivity {

 Button top;
 Button bottom;
 Button left;
Button right;
 Button clear;
ImageView img;

int x=300;
int y=300;

int x1=300;
int y1=300;

Integer gettingX() {
    return x;
}

void settingX(Integer x) {
    this.x = x;
}

Integer gettingY() {
    return y;
}

void settingY(Integer y) {
    this.y = y;
}


Integer gettingY1() {
    return y1;
}

void settingY1(Integer y1) {
    this.y1 = y1;
}

Integer gettingX1() {
    return x1;
}

void settingX1(Integer x1) {
    this.x1 = x1;
}
Paint paint = new Paint();
Bitmap bmp = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img = (ImageView) findViewById(R.id.img);
    top = (Button) findViewById(R.id.top);
    bottom = (Button) findViewById(R.id.bottom);
    left = (Button) findViewById(R.id.left);
    right = (Button) findViewById(R.id.right);
    clear = (Button) findViewById(R.id.clear);


    top.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            paint.setAntiAlias(true);

            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(25);
            canvas.drawLine(x,y,x1, y1-50,paint);
            img.setImageBitmap(bmp);
        }
    });

    bottom.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            paint.setAntiAlias(true);

            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(25);
            canvas.drawLine(x,y,x1, y1+50,paint);
            img.setImageBitmap(bmp);
        }
    });

    left.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            paint.setAntiAlias(true);

            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(25);
            canvas.drawLine(x,y,x1-50, y1,paint);
            img.setImageBitmap(bmp);
        }
    });

    right.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            paint.setAntiAlias(true);

            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(25);
            canvas.drawLine(x,y,x1+50, y1,paint);
            img.setImageBitmap(bmp);
        }
    });

    clear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            canvas.drawColor(Color.WHITE);
            img.setImageBitmap(bmp);
        }
    });

}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res                   
/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top"
android:text="top"
android:layout_gravity="center_horizontal"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="right"
    android:id="@+id/right"
    android:layout_gravity="right"
    android:layout_alignTop="@+id/left"
    android:layout_toLeftOf="@+id/bottom"
    android:layout_toStartOf="@+id/bottom" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="left"
    android:id="@+id/left"
    android:layout_below="@+id/top"
    android:layout_toRightOf="@+id/top"
    android:layout_toEndOf="@+id/top" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bottom"
    android:id="@+id/bottom"
    android:layout_gravity="center_horizontal"
    android:layout_below="@+id/right"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear"
    android:id="@+id/clear"
    android:layout_gravity="center_horizontal"
    android:layout_below="@+id/bottom"
    android:layout_alignRight="@+id/bottom"
    android:layout_alignEnd="@+id/bottom" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:id="@+id/img"
    android:layout_gravity="center_horizontal"
    android:layout_below="@+id/clear"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</RelativeLayout>

current solution expected solution

Upvotes: 1

Views: 57

Answers (1)

Ben P.
Ben P.

Reputation: 54274

The problem is that your code never updates the values of your x or y variables when you click the button.

First, delete these two variables:

int x1=300;
int y1=300;

and delete all of these methods:

Integer gettingX() { ... }
void settingX(Integer x) { ... }
Integer gettingY() { ... }
void settingY(Integer y) { ... }
Integer gettingY1() { ... }
void settingY1(Integer y1) { ... }
Integer gettingX1() { ... }
void settingX1(Integer x1) { ... }

Next, update all of your references to x1 or y1 to just use x and y. For example:

            canvas.drawLine(x, y, x, y - 50, paint);

Finally, save the "new" values to x or y after drawing the line:

            y -= 50;

Here's the updated code for your "top" button's click listener:

    top.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(25);

            canvas.drawLine(x, y, x, y - 50, paint);
            img.setImageBitmap(bmp);

            y -= 50;
        }
    });

Upvotes: 1

Related Questions