将对象从A点移动到最近的对象

时间:2015-11-13 23:30:58

标签: java object move

基本上,我正在开发一个Java游戏,我必须让部队移动到面板内最近的大厅(如Clash of Clans)。

我有一个BasePanel类,用于初始化随机放置在面板中任意位置的5个对象(Hall)。 BasePanel有一个mouselistener,当单击时,将另一个对象(Troop)拖放到x和y坐标。 Troop对象具有以线性模式移动的属性。我的问题是部队对象只是向下移动。

问题是如何让Troop对象移动到BasePanel中最近的Hall对象?

这是我的代码:

import javax.swing.*;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;

public class Test extends JFrame{
    public static BasePanel panel1;
    public static JButton baseClick, buttonClick, barbarian, archer, goblin;
    public static int clicks=0, count1=10, count2=5, count3=7, hallCount=5, basePanelW=600, basePanelH=420;
    public List<Troop> troops = new ArrayList<Troop>(20);
    public List<Hall> halls = new ArrayList<Hall>(5);
    public Thread gameThread;
    Random rand = new Random();
    float mouseX, mouseY; 
    float x = mouseX; // troop's center (x, y)
    float y = mouseY; 
    double troopSpeedX = 1;
    double troopSpeedY = 1;
    float troopRadius = 5;

    public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Test Test = new Test();
            }
        });
    }

    public void update() {
        for (Troop troop : troops) {
            troop.move(panel1);
        }
    }

    class Hall{
        int xVal = rand.nextInt(basePanelW);
        int yVal = rand.nextInt(basePanelH);

        public Hall(){}

        public void paint(Graphics g){
            g.setColor(Color.BLACK);
            g.fillOval(xVal, yVal, 50, 50);
        }

        public int getX(){
            System.out.println("xVal: "+xVal);
            return xVal;
        }
        public int getY(){
            System.out.println("yVal: "+yVal);
            return yVal;
        }
    }

    class Troop{
        float x = mouseX; 
        float y = mouseY; 
        double troopSpeedX = 1;  
        double troopSpeedY = 1;
        float troopRadius = 5; 
        Hall h = new Hall();
        public Troop(){}

        public void paint(Graphics g){
            g.setColor(Color.BLUE);
            g.fillOval((int) (x - troopRadius), (int) (y - troopRadius), (int)(2 * troopRadius), (int)(2 * troopRadius));
        }

        public void move(JPanel panel){
            x += troopSpeedX;
            y += troopSpeedY;


            if (x - troopRadius < 0) {
                troopSpeedX = -troopSpeedX; 
                x = troopRadius;
            }else if (x + troopRadius > basePanelW) {
                troopSpeedX = -troopSpeedX;
                x = basePanelW - troopRadius;
            }else if (y - troopRadius < 0) {
                troopSpeedY = -troopSpeedY;
                y = troopRadius;
            }else if (y + troopRadius > basePanelH) {
                troopSpeedY = -troopSpeedY;
                y = basePanelH - troopRadius;
            }repaint();
        }
    }

    public void start(){
        gameThread = new Thread() {
            public void run() {
                while (true) {
                    update();
                    repaint();
                    try {Thread.sleep(100/3);}catch(InterruptedException ex){}
                }
            }
        }; gameThread.start();
    }

    class BasePanel extends JPanel{
        private BufferedImage image;
        BasePanel(){
            setPreferredSize(new Dimension(basePanelW,basePanelH));
            setBackground(Color.green);

            while(hallCount>0){
                halls.add(new Hall());
                hallCount--;
            }


            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    mouseX = e.getX();
                    mouseY = e.getY();
                    clicks++;
                }
                public void mouseReleased(MouseEvent e){
                    troops.add(new Troop());
                }
            });
        }

        private void msgbox(String s){
           JOptionPane.showMessageDialog(null, s);
        }
        public void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (Troop troop : troops){
            troop.paint(g);
          }
        }
        public void paint(Graphics g){
          super.paint(g);
          for (Hall hall : halls){
            hall.paint(g);
          }
        }
    }

    public Test(){
        panel1 = new BasePanel();
        add(panel1, BorderLayout.CENTER);

        setVisible(true);
        setSize(700,420);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        start();
    }
}

0 个答案:

没有答案