有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java图像在minerals=0后消失

在我的程序中,如果你有所需的矿物质,你可以在小程序上放置图片

你从400开始,每一个都是100种矿物质,所以你放3个,然后在第4个它会删除所有其他的

public int Minerals

    //Pylons
    int xCoord[];
    int yCoord[];
    int numSquare;
    boolean firstPaint;

public void init()
    {   

        Minerals = 400;

        pylon = getImage(getDocumentBase(),"image/pylon.png");
    //pylons
        xCoord = new int[100];
        yCoord = new int[100];
        numSquare = 0;
        firstPaint = true;
}

public void paint(Grapics g)
{


            pylons(g);
}

    public void pylons(Graphics g)
    {
        //Building the pylons
        if(Minerals >= 100)
        {

            for (int k = 0; k < numSquare; k++)
            {
                    g.drawImage(pylon,xCoord[k],yCoord[k],85,85,this);
                    //Makes cursor normal.
                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }

        }

        //Checks if buildPylon is 1 if so it will draw the infoScreen and then set the cursor to pylon
        if(buildPylon == 1)
        {
            g.drawImage(pylon,510,820,100,100,this);
            //Use the custom cursor  
            setCursor(cursor);  
        }
    }

    private void handlePylonPlacement()
    {
        //This takes away 100 minerals and will add 9 population and make buildPylon 0 again
        if(decrementMinerals(100))
            addPopMax(9);
            buildPylon = 0;

    } 

    private boolean decrementMinerals(int amount)
    { 
        //This Is where it takes away the minerals
        if(Minerals - amount >= 0) // prevent situation where you go into negative minerals
        {
             Minerals -= amount;
             return true;
        }
        else
             return false;
    }

    private void addPopMax(int amount)
    {
        //Add the population (9)
        if(popMax + amount <= 72) // restrict addition to pop-max to a sane upper bound
             popMax += amount;
    }


public boolean mouseDown(Event e, int x, int y) {
        //Makes the ints xCoord2 to equal x and same for the other
        xCoord2 = x;
        yCoord2 = y;
        repaint();

        if (numClicks == 10) {//Title screen        
            numClicks++;
            repaint();
        }

    //Checks to see if buildPylon == 1 then builds the pylon if its in the correct place.

    if(buildPylon == 1)
    {
        if(x > 1 && x < 1275 && y > 711 && y < 948) // Checks to see if the person clicked in this area
        {}
        else if(x > 378 && x < 876 && y < 568 && y < 705) // Checks to see if the person clicked in this area
            {}else if (Minerals >= 100) 
            {
            xCoord[numSquare] = x;
            yCoord[numSquare] = y;
            numSquare++;
            handlePylonPlacement(); // call your new handler
            repaint();
             }  
        }



        return true;
    } 

我不想删除它们,而是想让塔架留在屏幕上。。。有什么帮助吗


共 (1) 个答案

  1. # 1 楼答案

    你的意思是

        private void handlePylonPlacement()
        {
            //This takes away 100 minerals and will add 9 population and make buildPylon 0 again
            if (decrementMinerals(100)) {
                addPopMax(9);
                buildPylon = 0;
            }    
        }
    

    ?