簡體   English   中英

如何修復此繪制程序,以使從一條線繪制的最后一個像素在選擇另一個時不會更改為其他顏色?

[英]How can I fix this paint program so that the last pixel drawn from a line does not change to a different color when another is selected?

    import java.awt.*;
    import java.applet.*;


    public class lab13 extends Applet 
    {

    Rectangle pencil;
    Rectangle red;
    Rectangle blue;
    Rectangle green;
    Rectangle purple;
    Rectangle drawArea;


    Image virtualMem;
    Graphics gBuffer;

    int newX; 
    int newY;


    int appletWidth;
    int appletHeight;

    int drawnPoint;
    int choice;

    public void init()
    {
        appletWidth = getWidth();
        appletHeight = getHeight();
        virtualMem = createImage(appletWidth,appletHeight);
        gBuffer = virtualMem.getGraphics();
        gBuffer.setColor(Color.WHITE);
        gBuffer.fillRect(0,0,appletWidth,appletHeight);

        pencil = new Rectangle(10,10,50,50);
        red = new Rectangle(10,70,50,50);
        blue = new Rectangle(10,130,50,50);
        green = new Rectangle(10,190,50,50);
        purple = new Rectangle(10,250,50,50);

        drawArea = new Rectangle(85,10,290,380);

        gBuffer.drawRect(85,10,290,380);

        gBuffer.setColor(Color.BLACK);
        gBuffer.fillRect(10,10,50,50);

        gBuffer.setColor(Color.RED);
        gBuffer.fillRect(10,70,50,50);

        gBuffer.setColor(Color.BLUE);
        gBuffer.fillRect(10,130,50,50);

        gBuffer.setColor(Color.GREEN);
        gBuffer.fillRect(10,190,50,50);

        gBuffer.setColor(new Color(218,112,214));
        gBuffer.fillRect(10,250,50,50);

        choice = 0;
    }

    public void paint(Graphics g)
    {

        gBuffer.setColor(Color.WHITE);          //This is a quick fix for a colored pixel in the top left hand corner
        gBuffer.fillRect(0,0,3,3);


        switch(choice)
        {
            case 1:
                gBuffer.setColor(Color.BLACK);
                break;
            case 2:
                gBuffer.setColor(Color.red);
                break;
            case 3:
                gBuffer.setColor(Color.BLUE);
                break;
            case 4:
                gBuffer.setColor(Color.GREEN);
                break;
            case 5:
                gBuffer.setColor(new Color(218,112,214));
                break;
        }

            gBuffer.fillRect(newX,newY,3,3);
            g.drawImage(virtualMem,0,0,this);

    }





    public boolean mouseDown(Event e, int x, int y)
    {
        if (pencil.inside(x,y))
            choice = 1;
        else if (red.inside(x,y))
            choice = 2;
        else if (blue.inside(x,y))
            choice = 3;
        else if (green.inside(x,y))
            choice = 4;
        else if (purple.inside(x,y))
            choice = 5;
        repaint();
        return true;

    }

    public boolean mouseDrag(Event e, int x, int y)
    {
        if(drawArea.inside(x,y))
        {
            newX = x;
            newY = y;
            repaint();
        }
        return true;

    }




    public void update(Graphics g)
     {
        paint(g);
     }



}

這是我們正在學校為實驗室做的繪畫程序。 我遇到的問題是,在繪制一條線並選擇了另一種顏色之后,上一條線的最后一部分會將顏色更改為被選擇的顏色,而不是保持統一的顏色。 我真的不知道在哪里尋找問題。 我嘗試改變。 我懷疑這可能是我的paint方法結尾處的drawImage。 發布此內容后,我將嘗試使用該功能。 任何幫助將不勝感激!

您需要設置一些標志來確定顏色已更改,而不是用新顏色重新繪制矩形。 您可以通過多種方式完成此操作,這是一種:

如果顏色發生變化,請將newXnewY設置為某個無效值:

@Override
public boolean mouseDown(final Event e, final int x, final int y) {
    int choiceNew = 0;

    if (pencil.inside(x, y)) {
        choiceNew = 1;
    } else if (red.inside(x, y)) {
        choiceNew = 2;
    } else if (blue.inside(x, y)) {
        choiceNew = 3;
    } else if (green.inside(x, y)) {
        choiceNew = 4;
    } else if (purple.inside(x, y)) {
        choiceNew = 5;
    }

    if ((choiceNew > 0) && (choice != choiceNew)) {
        choice = choiceNew;
        newX = -1;
        newY = -1;
    } else {
        newX = x;
        newY = y;
    }

    repaint();
    return true;
}

然后在繪畫時,檢查該標志:

@Override
public void paint(final Graphics g) {
    gBuffer.setColor(Color.WHITE); //This is a quick fix for a colored pixel in the top left hand corner
    gBuffer.fillRect(0, 0, 3, 3);

    switch (choice) {
        case 1:
            gBuffer.setColor(Color.BLACK);
            break;
        case 2:
            gBuffer.setColor(Color.red);
            break;
        case 3:
            gBuffer.setColor(Color.BLUE);
            break;
        case 4:
            gBuffer.setColor(Color.GREEN);
            break;
        case 5:
            gBuffer.setColor(new Color(218, 112, 214));
            break;
    }

    if ((newX != -1) && (newY != -1)) {
        gBuffer.fillRect(newX, newY, 3, 3);
    }

    g.drawImage(virtualMem, 0, 0, this);
}

暫無
暫無

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

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