簡體   English   中英

Java的。 如何為繪畫添加更多顏色?

[英]Java. How do I add more colours to paint?

我正在嘗試創建一個繪圖應用程序,到目前為止,我可以選擇兩種顏色(綠色和黑色)和兩種形狀(圓形和方形)。 我想添加更多顏色來繪畫,但我無法弄清楚如何添加超過2種顏色。

這些是我正在使用的類。

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;

}

首先,確保用戶可以訪問其他顏色。 我們試着添加紅色。

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");

接下來,假設您的代碼如何工作,它通過coloursfill屬性處理它。 如果fill為0,則為綠色,否則為黑色。 讓我們添加一個案例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);
    ...
}

我們還應確保在按鈕點擊代碼區域中添加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;
...

您可能會看到我將Red為2並進一步推動了畫筆設置。 這是為了確保顏色分組。

現在,這是我可以從您提供的代碼中建議的全部內容。 也許還有更多。 但你應該看看這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM