2012-08-13 76 views
0

我創建了一個在屏幕上畫球的類,我的目標是用鍵移動它,但球保持在一個點上。球變化的根據按鍵而不是實際的球本身當我按下一個鍵時,我的mainBall類不會重畫

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

*」我創建了一個類。「*這段代碼看起來更像'你從某個地方得到它',其中'某處'是​​1999年。它使用'Applet'和不推薦的方法,例如'keyDown(Event,int)'。我可以提供的最佳建議是 - 將代碼放回到找到它的位置,並訪問[Java教程](http://docs.oracle.com/javase/tutorial/),特別是[Swing](http: /docs.oracle.com/javase/tutorial/uiswing/)和[鍵綁定](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)部分。另外,開發桌面應用程序。第一,比applets簡單得多。 – 2012-08-13 00:39:58

回答

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); 
} 
相關問題