开发者

Java applet methods [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

This is my program:

import java.applet.*; // imports basic applet package
import java.awt.*; // imports the abstract windows toolkit
import java.awt.event.*; // imports the events package from the abstract windows toolking
import java.util.Random; // imports package for randomizing numbers

public class Game extends Applet implements KeyListener
{

    public int x = 240; // sets the x variable
    public int y = 230; // sets the y variable
    private Image dbImage; // Variable for doublebuffering
    private Graphics dbg; // Variable for doublebuffering
    Image startscreen; // declares startscreen as an image
    Image instructions; // declares insturctions as an image
    Image background; // declares background as an image
    Random rand = new Random (); // declares the random variable


    public void init ()
    {
        Game b = new Game ();
        addKeyListener (this);
        setSize (500, 500); // sets the size of the screen
        //setBackground (Color.WHITE); // sets the backround color
        startscreen = getImage (getDocumentBase (), "startscreen.png"); // gets the "startscreen.png" image
        for (int p = 0 ; p < 1000 ; p++)
        {
        }
        instructions = getImage (getDocumentBase (), "instructions.png"); // gets the "instructions.png" image
        for (int o = 0 ; o < 1000 ; o++)
        {
        }
        background = getImage (getDocumentBase (), "background.png"); // gets the "background.png" image
        Game.bombs ();
    }


    public void backgrounds (Graphics g)
    {
        setSize (500, 500);
        g.drawImage (startscreen, 0, 0, this); // draws the startscreen
        g.drawImage (instructions, 0, 0, this); // draws the instructions
        g.drawImage (background, 0, 0, this); // draws the background
    }


    public void paint (Graphics g)
    {
        g.drawImage (startscreen, 0, 0, this); // draws the startscreen
        repaint ();
        g.drawImage (instructions, 0, 0, this); // draws the instructions
        repaint ();
        g.drawImage (background, 0, 0, this);
        g.setColor (Color.GRAY); // sets the body of the tank to GRAY
        g.fillRect (x, y, 30, 50); // body of tank
        g.setColor (Color.BLACK); // sets the rest of the tank to BLACK
        g.fillRect (x - 10, y - 10, 10, 70); // left wheel of tank
        g.fillRect (x + 30, y - 10, 10, 70); // right wheel of tank
        g.fillRect (x + 5, y + 20, 20, 20); // top of tank
        g.fillRect (x + 10, y - 20, 10, 50); // barrel of tank
    }


    public void keyTyped (KeyEvent e)
    {
    }


    public void update (Graphics g)
    {

        // initialize buffer
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height); // sets the width, height, etc of doublebuffer
            dbg = dbImage.getGraphics (); // gets the image
        }

        // clear screen in background
        dbg.setColor (getBackground ()); // gets the background for the double buffer
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height); // gets teh size of the width, height, etc of background

        // draw elements in background
        dbg.setColor (getForeground ()); // sets the forground for the doulbebuffer
        paint (dbg); // draws the image for the double buffer

        g.drawImage (dbImage, 0, 0, this); // draw image on the screen

    }


    public void keyPressed (KeyEvent e)
    {

        if (e.getKeyCode () == 37) // checks if left arrow is pressed
        {
            x -= 10; // moves the tank over 4 to the left
            repaint ();
        } // end if for left arrow
        if (e.getKeyCode () == 39) // checks if up arrow is pressed
        {
            x += 10; 开发者_如何转开发// moves the tank over 4 to the right
            repaint ();
        } // end if for up arrow
        if (e.getKeyCode () == 38) // checks if right arrow is pressed
        {
            y -= 10;
            repaint ();
        } // end if for right arrow
        if (e.getKeyCode () == 40) // checks if down arrow is pressed
        {
            y += 10;
            repaint ();
        } // end if for down arrow
        if (e.getKeyCode () == 32) // checks if space is pressed
        {
            repaint ();
        } // end if for space
        if (x > 460) // makes sure that the tank does not pass the right wall
        {
            x -= 10;
        }
        if (x < 10) // makes sure that the tank does not pass the left wall
        {
            x += 10;
        }
        if (y > 440) // makes sure that the tank does not pass the bottom wall
        {
            y -= 10;
        }
        if (y < 20) // makes sure that the tank does not pass the top wall
        {
            y += 10;
        }
    }


    public void keyReleased (KeyEvent e)
    {
    }


    public void bombs (Graphics g)
    {
        int pick = 0;
        int pick2 = 0;

        for (int j = 0 ; j < 1 ; j++) // for loop for counter
        {
            pick = rand.nextInt (500); // randomizes random integers from 0 to 500
            //System.out.println (pick); // prints the integers
        } // end for loop for randomize

        for (int m = 0 ; m < 1 ; m++) // for loop for randomize integer
        {
            pick2 = rand.nextInt (500); // randomizes intger from 0 to 500
        }
        g.fillRect (pick, pick2, 100, 100); // draws the bombs
        if (x < pick + 10 || x > pick + 10) // collision detection for x
        {
        }
        if (y < pick2 + 10 || y > pick2 + 10) // collision detection for y
        {
        }

    }
}

can you please tell me how to call in "bombs" in a different place? i want to call bombs in inside KeyPressed


The right way to do it: have your paint() method call bombs() if a boolean flag member variable is set. Then in your key listener, set the flag and call repaint(). The paint() method can clear the flag after calling the method. That way you can call it from anywhere; when paint() is called by repaint(), it will be able to supply the Graphics argument and call the method correctly.

So in paint() you might have

public void paint(Graphics g) {
    ...
    if (shouldShowBombs) {
        bombs(g);
        shouldShowBombs = false;
    }

Then if you want to paint the bombs anywhere in your applet, you just write

shouldShowBombs = true;
repaint();

Of course, you have to declare shouldShowBombs

public class Game {
    private boolean shouldShowBombs = false;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜