简体   繁体   中英

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



}

This is a paint program we are doing for a lab at school. The problem I am having is that after I draw a line, and choose a different color, the last part of the previous line changes color to the one that gets selected, rather than stay a uniform color. I don't really know where to look for the problem. I have tried changing. I suspect it could be the drawImage at the end of my paint method. That will be something I try and play with after I post this. Any help would be greatly appreciated!

You'll need to set some flag to determine that the color changed and not to redraw the rectangle in the new color. There are many different ways you accomplish this, here's one:

If the color changes, set newX and newY to some invalid value:

@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;
}

Then when painting, you check for that flag:

@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);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM