I am trying to create a drawing app and so far i am able to choose from two colors(Green and Black) and two shapes (Circle and Square). I want to add more colors to paint with but i couldn't figure out how to add more than 2 colors.
These are the classes I'm using.
public class DrawLinesActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawshapes);
DrawLines d = new DrawLines(this);
setContentView (d);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem menu1 = menu.add(0, 0, Menu.NONE, "Green");
MenuItem menu2 = menu.add(0, 1, Menu.NONE, "Black");
MenuItem menu3 = menu.add(0,2, Menu.NONE, "Pixel Brush");
MenuItem menu4 = menu.add(0,3, Menu.NONE, "Smooth Brush");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case 0:
colours.fill = 0;
return true;
case 1:
colours.fill = 1;
return true;
case 2:
colours.shape = 0;
return true;
case 3:
colours.shape = 1;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
...
public class DrawLines extends View
{
Canvas c;
Paint paint;
Bitmap bmp;
Random g;
float X, Y;
public DrawLines(Context context)
{
super(context);
g=new Random();
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bmp= Bitmap.createBitmap(1050,1650,conf);
paint = new Paint();
paint.setStyle (Paint.Style.STROKE);
paint.setColor (Color.WHITE);
this.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
float x, y;
c = new Canvas (bmp);
x = event.getX ();
y = event.getY ();
System.out.printf ("%f %f\n", X, Y);
paint.setAntiAlias (true);
if (colours.fill == 0)
{
paint.setStyle (Paint.Style.FILL);
paint.setColor (Color.GREEN);
if (colours.shape == 0)
c.drawRect (x, y, x + 50, y + 50, paint);
else
c.drawOval(new RectF (x, y, x + 50, y + 50), paint);
}
else
{
paint.setStyle (Paint.Style.FILL);
paint.setColor (Color.BLACK);
if (colours.shape == 0)
c.drawRect (x, y, x + 50, y + 50, paint);
else
c.drawOval(new RectF (x, y, x + 50, y + 50), paint);
}
paint.setColor (Color.WHITE);
invalidate ();
return true;
}
});
}
@Override
protected void onDraw (Canvas c)
{
super.onDraw (c);
c.drawBitmap (bmp, 0, 0, paint);
}
}
...
public class colours
{
static public int shape = 0;
static public int fill = 0;
}
First off, make sure the user can access other colors. Let's try adding red.
MenuItem menu1 = menu.add(0, 0, Menu.NONE, "Green");
MenuItem menu2 = menu.add(0, 1, Menu.NONE, "Black");
MenuItem menu2 = menu.add(0, 2, Menu.NONE, "Red"); //Make an item for red.
MenuItem menu3 = menu.add(0, 3, Menu.NONE, "Pixel Brush");
MenuItem menu4 = menu.add(0, 4, Menu.NONE, "Smooth Brush");
Next, assuming from how your code works, it handles it via the fill
attribute of colours
. If fill
is 0, it's green, else, it's black. Lets add a case 1
, which will be red.
if (colours.fill == 1)
{
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
...
}
else if (colours.fill == 0)
{
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.GREEN);
...
}
else
{
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
...
}
We should also make sure to add a case for Red
in the button tap code area:
...
case 0:
colours.fill = 0;
return true;
case 1:
colours.fill = 1;
return true;
case 2:
colours.fill = 2;
return true;
case 3:
colours.shape = 0;
return true;
case 4:
colours.shape = 1;
return true;
...
You might see i put Red
as 2 and pushed the brush settings further. This is to make sure the colors are grouped.
Now, this is all I can suggest from the code you supplied. Maybe there's more to it. But you should take a look at this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.