2016-12-31 68 views
-2

正如标题所说,就是不能这样做。试图很长时间,但仍然失败。 (我是新来的Java)
我的图片或fillRect转动一点点,但并不像它应该旋转
整个代码+ IMGS
无法实现对象旋转朝向鼠标JAVA


*MAIN game class* 
    package game.main; 

    import java.awt.Canvas; 
    import java.awt.Color; 
    import java.awt.Graphics; 
    import java.awt.image.BufferStrategy; 

    public class game extends Canvas implements Runnable{ 

     private static final long serialVersionUID = -392333887196083915L; 

     public static final int WIDTH = 640, HEIGHT = WIDTH/12 * 9; 
     private Thread thread; 
     private boolean running = false; 

     private Handler handler; 

     public game(){ 

      handler = new Handler(); 

      new window(WIDTH, HEIGHT,"Game",this); 

      handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player, handler)); 
     } 

     public synchronized void start(){ 
      thread = new Thread(this); 
      thread.start(); 
      running = true; 
     } 
     public synchronized void stop(){ 
      try{ 
       thread.join(); 
       running = false; 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
     // ↓ game loop for update 
     public void run(){ 
      this.requestFocus(); 
      long lastTime = System.nanoTime(); 
      double amountOfTicks = 60.0; 
      double ns = 1000000000/amountOfTicks; 
      double delta = 0; 
      long timer = System.currentTimeMillis(); 
      int frames = 0; 
      while(running){ 
       long now = System.nanoTime(); 
       delta += (now - lastTime)/ns; 
       lastTime = now; 
       while(delta >= 1){ 
        tick(); 
        delta--; 
       } 
       if(running) 
        render(); 
       frames++; 

       if(System.currentTimeMillis() - timer > 1000){ 
        timer += 1000; 
        System.out.print("FPS:" + frames); 
        frames = 0; 
       } 
      } 
      stop(); 
     } 

     private void tick(){ 
        handler.tick();    
       } 

     private void render(){ 
      BufferStrategy bs = this.getBufferStrategy(); 
      if(bs == null){ 
       this.createBufferStrategy(3); 
       return; 
      } 

      Graphics g = bs.getDrawGraphics(); 

      g.setColor(Color.black); 
      g.fillRect(0, 0, WIDTH, HEIGHT); 

      handler.render(g); 

      g.dispose(); 
      bs.show(); 
     } 



     public static void main(String args[]) { 
      new game(); 
     } 


    } 
<br> 
package game.main; 

import java.awt.Graphics; 

public abstract class GameObject { 

    protected float x, y; 
    protected ID id; 
    protected float velX, velY; 

    public GameObject(float x, float y, ID id){ 
      this.x = x; 
      this.y = y; 
      this.id = id; 
    } 

    public abstract void tick(); 
    public abstract void render(Graphics g); 

    public void setX(int x){ 
     this.x = x; 
    } 
    public void setY(int y){ 
     this.y = y; 
    } 
    public float getX(){ 
     return x; 
    } 
    public float getY(){ 
     return y; 
    } 
    public void setId(ID id){ 
     this.id = id; 
    } 
    public ID getId(){ 
     return id; 
    } 
    public void setVelX(int velX){ 
     this.velX = velX; 
    } 
    public void setVelY(int velY){ 
     this.velY = velY; 
    } 
    public float getVelX(){ 
     return velX; 
    } 
    public float getVelY(){ 
     return velY; 
    } 
} 


*GameObject class* 
package game.main; 

import java.awt.Graphics; 

public abstract class GameObject { 

    protected float x, y; 
    protected ID id; 
    protected float velX, velY; 

    public GameObject(float x, float y, ID id){ 
      this.x = x; 
      this.y = y; 
      this.id = id; 
    } 

    public abstract void tick(); 
    public abstract void render(Graphics g); 

    public void setX(int x){ 
     this.x = x; 
    } 
    public void setY(int y){ 
     this.y = y; 
    } 
    public float getX(){ 
     return x; 
    } 
    public float getY(){ 
     return y; 
    } 
    public void setId(ID id){ 
     this.id = id; 
    } 
    public ID getId(){ 
     return id; 
    } 
    public void setVelX(int velX){ 
     this.velX = velX; 
    } 
    public void setVelY(int velY){ 
     this.velY = velY; 
    } 
    public float getVelX(){ 
     return velX; 
    } 
    public float getVelY(){ 
     return velY; 
    } 
} 


*Handler class* 
package game.main; 

import java.awt.Graphics; 
import java.util.LinkedList; 

// render all objects 
public class Handler { 

    LinkedList<GameObject> object = new LinkedList<GameObject>(); 

    public void tick(){ 
     for(int i = 0; i < object.size(); i++){ 
      GameObject tempObject = object.get(i); 

      tempObject.tick();  
     } 
    } 
    public void render(Graphics g){ 
     for(int i = 0; i <object.size();i++){ 
      GameObject tempObject = object.get(i); 

      tempObject.render(g); 
     } 
    }// handling adding objects 
    public void addObject(GameObject object){ 
     this.object.add(object); 
    } 
} 


*window class* 
package game.main; 

import java.awt.Canvas; 
import java.awt.Dimension; 

import javax.swing.JFrame; 

public class window extends Canvas{ 

    private static final long serialVersionUID = 3010486623466540351L; 

    public window(int width, int height, String title, game game){ 
     JFrame frame = new JFrame(title); 

     frame.setPreferredSize(new Dimension(width, height)); 
     frame.setMaximumSize(new Dimension(width, height)); 
     frame.setMinimumSize(new Dimension(width, height)); 

     // X button 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // ¤ button maximize 
     frame.setResizable(false); 
     // window appear in middle of screen instead of top left corner 
     frame.setLocationRelativeTo(null); 
     // add game to window 
     frame.add(game); 
     frame.setVisible(true); 
     game.start(); 
    }  
} 


* ID class* 
package game.main; 

public enum ID { 

    Player(); 
} 


添加的代码只对这种类

*Player class* 
package game.main; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.MouseInfo; 
import java.awt.Point; 
import java.awt.PointerInfo; 

public class Player extends GameObject{ 

    Handler handler; 

    public Player(int x, int y, ID id, Handler handler) { 
     super(x, y, id); 
     this.handler = handler; 
    } 

    public void tick() { 
    } 
    public void render(Graphics g) { 
     ///////////ADDED////////////////// 
     PointerInfo a = MouseInfo.getPointerInfo(); 
     Point b = a.getLocation(); 
     int mouseX = (int) b.getX(); 
     int mouseY = (int) b.getY(); 
     int centerX = game.WIDTH/2; 
     int centerY = game.HEIGHT/2; 
     double angle = Math.atan2(centerY - mouseY, centerX - mouseX) - Math.PI/2; 
     ((Graphics2D)g).rotate(angle, centerX, centerY); 
     ////////////////////////////// 
     g.setColor(Color.white); 
     g.fillRect((int)x, (int)y, 32, 32); 

    } 
} 


它现在如何工作,以及我想如何。白色 - 原始/ GREEN - 我喜欢这样吃
Example 1
Example 2
我在这个来源看:
Get mouse possition (stackoverflow)
Java 2d rotation in direction mouse point (stackoverflow)
Rotating an object to point towards the mouse

+2

我不能说全部,但我可以说,对我来说,你可以做的一件最好的事情是帮助我理解你的问题,就是创建并发布一个体面的[mcve](请参阅链接) 。 –

+0

我狙击大部分代码并发布整个代码 – EJskater

+0

未来,请不要这样做。请怜悯我们,记住我们都是志愿者。相反,创造一个真正的善良真正的[mcve]。 –

回答

0

的问题是,MouseInfo.getPointerInfo().getLocation();回报的绝对鼠标位置。您需要相对于您的游戏画布的鼠标位置。您可以在game类修改render方法如下:

Point mouseLocation = MouseInfo.getPointerInfo().getLocation(); 
SwingUtilities.convertPointFromScreen(mouseLocation, this); 
handler.render(g, mouseLocation); 

这需要你相应地修改您的渲染方法的方法签名。这只是将mouseLocation从您的游戏画布传递给您的玩家渲染方法的唯一途径。 在玩家的渲染方法中使用mouseLocation而不是MouseInfo.getPointerInfo().getLocation();

有一些更多的东西,你必须改变放置玩家在画布的中心,让他绕着他的中心:

  • 应设置游戏画布的大小,而不是窗口大小(JFrame)。通过在游戏画布上调用setPreferredSize(new Dimension(width, height));并在frame.setVisible(true)之前致电frame.pack()来完成此操作。这将确保您的游戏画布具有由WIDTHHEIGHT指定的大小。
  • 您可以将两个字段refxrefy添加到您的GameObject类中,该类描述了您的GameObject(例如其中心)的参考点。然后,您可以通过致电new Player(WIDTH/2-16, HEIGHT/2-16, 16, 16, ID.Player, handler)构建一个新的Player,其中玩家的初始位置是(WIDTH/2-16,HEIGHT/2-16),其参考点是(16,16) - 当玩家是玩家时的玩家中心由32x32矩形表示。
  • 在玩家的渲染方法中初始化要旋转的中心int centerX = Math.round(x + refx); int centerY = Math.round(y + refy);其中(x,y)是要旋转的对象的位置和(refx,refy)要旋转的点相对于对象的位置(例如,x = WIDTH/2-16,y = HEIGTH/2-16,refx = 16,refy = 16)。
+0

仍然是idk。我无法将MouseMotionListener实现为游戏,因为我已经在那里运行了。你可以编辑它,并放下更多细节吗? – EJskater

+0

@EJskater我刚刚编辑了我的答案。我使用'SwingUtilities.convertPointFromScreen()'来代替'MouseMotionListener'。 – Calculator

+0

看看我张贴的结尾处的图像。它是如何工作的?如果是的话,那么我必须改变像img。 Btw ty非常多 – EJskater