当我按一个键时,我的mainBall类不会重新绘制

时间:2012-08-13 00:05:38

标签: java applet awt keypress keydown

我创建了一个在屏幕上绘制球的类,我的目标是用键移动它,但球保持在一个位置。球的坐标根据按键而不是实际的球本身而改变

import java.applet.Applet;
import java.awt.Color;
import java.awt.Component;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class game extends Applet implements Runnable
{
    static final int WIDTH = 450;
    static final int HEIGHT =  450;
    private Image dbImage;
    private Graphics dbg;

    public static long NEW_DOT_FREQ = TimeUnit.SECONDS.toMillis(3);
    public long lastUpdateTime;
    public long timeSinceLastNewDot;


    public ArrayList<Ball> BALLS;

    Color[] color = {Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.black};
    int colorIndex;

    static final int NUM_OF_BALLS = 4;

    int i;
    int t;

    MainBall mainBall = new MainBall(100, 100, 10, 10, 100, 100, 0, 0);

    Thread updateTime = new updateTime();

    public void start()
    {
        lastUpdateTime = System.currentTimeMillis();

        Thread th = new Thread(this);
        th.start();//start main game

        updateTime.start();
    }

    public void updateGame()
    {
        //Get the current time
        long currentTime = System.currentTimeMillis();
        //Calculate how much time has passed since the last update
        long elapsedTime = currentTime - lastUpdateTime;
        //Store this as the most recent update time
        lastUpdateTime = currentTime;

        //Create a new dot if enough time has passed
        //Update the time since last new dot was drawn
        timeSinceLastNewDot += elapsedTime;

        if (timeSinceLastNewDot >= NEW_DOT_FREQ)
        {
            int newX = randomNumber();
            int newY = randomNumber();

            debugPrint("New dot created at x:" + newX + ", y:" + newY + ".");

            BALLS.add(new Ball(newX, newY, 20, 20));

            timeSinceLastNewDot = 0;
        }
    }

    private void debugPrint(String value)
    {
        System.out.println(value);
    }

    public class updateTime extends Thread implements Runnable
    {
        public void run()
        {
            for(t = 0; ; t++)
            {
                try
                {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e){}
            }
        }
    }

    public int randomNumber()
    {
        return (int)(Math.random() * 400);
    }

    public int getRandomColor()
    {
        return (int)(Math.random() * 6);
    }

    public class MainBall
    {
        int x;
        int y;
        int width;
        int height;
        int xpos = 100;
        int ypos = 100;
        int xspeed = 0;
        int yspeed = 0;

        public MainBall(int x, int y, int width, int height, int xpos, int ypos, int xspeed, int yspeed)
        {
            this.x = 100;
            this.y = 100;
            this.width = 10;
            this.height = 10;
            this.xpos = 100;
            this.ypos = 100;
            this.xspeed = 0;
            this.yspeed = 0;
        }

        public void paintMainBall(Graphics g)
        {
            g.setColor(Color.black);
            g.fillOval(x, y, width, height);
            g.drawString(xpos + ", " + ypos, 20, 40);
        }
    }//mainBall

    class Ball
    {
        int x;
        int y;
        int width;
        int height;

        public Ball(int x, int y, int width, int height)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }//end ball

        public void paint(Graphics g)
        {
            g.setColor(color[getRandomColor()]);
            g.fillOval(x, y, width, height);
        }//end paint


    }//ball class

    public void update(Graphics g)//double buffer don't touch!!
    {
        if(dbImage == null)
        {
            dbImage = createImage(this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics();
        }

        dbg.setColor(getBackground());
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

        dbg.setColor(getForeground());
        paint(dbg);

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

    public boolean keyDown (Event e, int key)
    {
        if(key == Event.LEFT)
        {
            mainBall.xspeed = -5;
            mainBall.yspeed = 0;
        }

        if(key == Event.RIGHT)
        {
            mainBall.xspeed = 5;
            mainBall.yspeed = 0;
        }

        if(key == Event.UP)
        {
            mainBall.yspeed = -5;
            mainBall.xspeed = 0;
        }

        if(key == Event.DOWN)
        {
            mainBall.yspeed = 5;
            mainBall.xspeed = 0;
        }
        return true;
    }

    public void run()
    {
        while(true)
        {
            repaint();

            if (mainBall.xpos < 1)
            {
                mainBall.xpos = 449;
            }

            if (mainBall.xpos > 449)
            {
                mainBall.xpos = 1;
            }
            if (mainBall.ypos < 1)
            {
                mainBall.ypos = 449;
            }

            if (mainBall.ypos > 449)
            {
                mainBall.ypos = 1;
            }
            mainBall.ypos += mainBall.yspeed;
            mainBall.xpos += mainBall.xspeed;
            try
            {
                Thread.sleep(20);
            }
            catch(InterruptedException ex){}
        }
      }

    public void init()
    {
        this.setSize(WIDTH, HEIGHT);

        BALLS = new ArrayList<Ball>();
    }

    public void paint(Graphics g)
    {

        g.drawString("time: " + t, 20, 20);

        mainBall.paintMainBall(g);

        for (Ball ball : BALLS)
        {
            ball.paint(g);
        }

        updateGame();
    }

}

1 个答案:

答案 0 :(得分:1)

您更新球的xPos / yPos值,但不更新x / y值

public void paintMainBall(Graphics g) {
    System.out.println("PaintMainBall");
    g.setColor(Color.RED);
    g.fillOval(x, y, width, height);
    g.drawString(xpos + ", " + ypos, 20, 40);
}