[英]Java. How do I add more colours to paint?
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.
我想添加更多颜色来绘画,但我无法弄清楚如何添加超过2种颜色。
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
. 接下来,假设您的代码如何工作,它通过
colours
的fill
属性处理它。 If fill
is 0, it's green, else, it's black. 如果
fill
为0,则为绿色,否则为黑色。 Lets add a case 1
, which will be red. 让我们添加一个案例
1
,它将是红色的。
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: 我们还应确保在按钮点击代码区域中添加
Red
的案例:
...
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. 您可能会看到我将
Red
为2并进一步推动了画笔设置。 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.
但你应该看看这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.