使用Java闪烁对象(Java.awt Graphics)

时间:2016-05-11 16:59:38

标签: java graphics

我在网上找到了一个agar.io java单人游戏,我想改进它。我面临的当前问题是点对象在屏幕上闪烁。我相信这与刷新率有关,虽然我不确定为什么/如何解决这个问题。下面主要类的代码。谢谢你的帮助。

package agario;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Controller {
   ArrayList<Blob> blobs = new ArrayList<Blob>();
   public ArrayList<Dot> dots = new ArrayList<Dot>(); 
   Blob b = new Blob(50,50,30);
   MyFrame mf = new MyFrame("Agario");
   int mouseX = 0;
   int mouseY = 0;
   static int score = 30;
   static int xDis = 0;
   static int yDis = 0;
   public static void main(String[] args){

      new Controller().startGame();
   }
   public void startGame(){
      mf.addMouseMotionListener(new MyMouseMoveListener());

      Refresh rf = new Refresh();
      Thread t = new Thread(rf);
      t.start();

      while(true){
         try{
            Random r = new Random();
            Thread.sleep(r.nextInt(100));

            double dis = Math.sqrt(xDis*xDis + yDis*yDis);
            double easingAmount = 180/b.size;
            if(dis > 1){
               b.x += easingAmount*xDis/dis;
               b.y += easingAmount*yDis/dis;
            }

            if(r.nextInt(10) == 5){
               int randX = r.nextInt(1920);
               int randY = r.nextInt(1080);
               Dot d = new Dot(randX,randY);
               synchronized(dots){
                  dots.add(d);
               }
               mf.add(d);
               mf.repaint();
               //System.out.println(score);
            }
         }
         catch(Exception e){

         }
      }
   }
   class Refresh implements Runnable{

      public void run() {
         while(true){
            Random ran = new Random();
            try{
               Thread.sleep(ran.nextInt(20));
            }
            catch(Exception e){
               System.out.println("error");
            }
            Rectangle r = new Rectangle(b.x,b.y,b.size,b.size);
            synchronized(dots){
               Iterator i = dots.iterator();
               while(i.hasNext()){
                  Dot d = (Dot) i.next();
                  Rectangle r1 = new Rectangle(d.x,d.y,12,12);
                  if(r1.intersects(r)){
                     i.remove();
                     b.size += 1;
                     score += 1;
                  }
               }
            }
            mf.repaint();
         }

      }
   }
   class MyMouseMoveListener extends MouseMotionAdapter{
      public void mouseMoved(MouseEvent m){
         mouseX = m.getX();
         mouseY = m.getY();
         xDis = mouseX-b.x;
         yDis = mouseY-b.y;
      }
   }
   class MyFrame extends Frame{
      MyFrame(String s){
         super(s);
         setBounds(0,0,1920,1080);
         add(b);
         blobs.add(b);
         setVisible(true);
         setIgnoreRepaint(true);
      }
      public void paint(Graphics g){
         for(Blob b : blobs)
            b.paint(g);
         synchronized(dots){
            Iterator i = dots.iterator();
            for(Dot d: dots){
               d.paint(g);
            }
         }
      }
   }
}

package agario;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Random;

public class Dot extends Component{
   public int x;
   public int y;
   public Color c;

   Dot(int x, int y){
      this.x = x;
      this.y = y;
      Random rand = new Random();
      int r = rand.nextInt(255);
      int green = rand.nextInt(255);
      int b = rand.nextInt(255);
      this.c = new Color(r,green,b);
   }
 public void paint(Graphics g){
      g.setColor(c);
      g.fillOval(this.x, this.y, 10, 10);
   }
}
package agario;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Random;

public class Blob extends Component{
   public int x;
   public int y;
   public int size;
   public Color color;
   public static Blob blob = new Blob(800,100,40);
   Blob(int x, int y, int size){
      Random rand = new Random();
      float r = rand.nextFloat();
      float g = rand.nextFloat();
      float b = rand.nextFloat();
      Color c = new Color(r,g,b);
      this.x = x;
      this.y = y;
      this.size = size;
      this.color = c;
   }
   public void paint(Graphics g){

      g.setColor(color);
      g.fillOval(x, y, size, size);
   }
}

0 个答案:

没有答案