1

My basic app idea is drawing on screen. I've got a Spinner object to use to select the color of the "pen". I have them set up for a switch case to change "pen" color. The Spinner is in my MainActivity class. I've got my "pen" code in a class called Brush_Color.

Here's the code I have for MainActivity that's related to the Spinner. Each case refers to a color in my arrays.xml. The commented out Paint paint = ... is what I was trying to do but had no luck.

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener{

//Paint paint = new Paint(Brush_Choices.this.paint, Brush_Choices.class);

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

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.color_selector, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
}


public void onItemSelected(AdapterView<?> parent, View v, int position, long id){
    switch(position) {
        case 0:
            //paint.setColor(Color.BLACK);
            break;
        case 1:
            //paint.setColor(Color.BLUE);
            break;

public void onNothingSelected(AdapterView<?> parent){

}

And then here is the code of my Brush_Color class. I'm trying to access the Paint object from here and use it in my MainActivity class. I've got no idea how to do this though.

Path path = new Path();
SparseArray<PointF> points = new SparseArray<>();
Paint paint = new Paint();

public void onDraw(Canvas canvas){
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(10);
    canvas.drawPath(path,paint);
}
BradleyIW
  • 1,338
  • 2
  • 20
  • 37
Mike
  • 11
  • 2

1 Answers1

1
public void onDraw(Canvas canvas){
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(10);
    canvas.drawPath(path,paint);
}

In this segment of the code you already retrieve the stroke width from the paint class. Before implementing such code or copying such code from an existing source you SHOULD understand it in it's entirety. the different approaches to accessing variables from one class to another can vary but i'll expand on the one you've already used.

you should have a variable inside the paint class for stroke width. This should be a private variable. The reason it's private is because you don't want classes to access that variable directly. In Java, difference between default, public, protected, and private

Inside the paint class you should have two functions existing they are known as Getters and Setters. Documentation for getters and setters here

I'm assuming it looks a little something like this:

A Getter for stroke width:

    public static int getStrokeWidth() {
    return strokeWidth;
}

A Setter for stroke width:

    public static void setStrokeWidth(int sWidth) {
    this.strokeWidth = sWidth;
}

These two functions allow access to the paint class from another existing class, the variables are defined within these functions to ensue that the variable you want to be collected is collected properly. All you have to do is call a static callback on either of these functions to get the value of the variable inside.

So using the current examples, if you were to call:

Paint.getStrokeWidth(); 

You would retrieve the value of that private int strokeWidth;

and the same for the other way round but this time instead of getting the value, you're setting the value for that current point in the program.

Paint.setStrokeWidth(10);

This function will allow you to define a value to the variable inside paint class.

see es6 call static methods for help on static method callbacks.

Community
  • 1
  • 1
BradleyIW
  • 1,338
  • 2
  • 20
  • 37