如何检测两个图像之间的接近程度

时间:2015-04-27 01:02:31

标签: java

我正在尝试为学校项目制作游戏。我试图检测两个图像相隔5个像素的时间。我似乎无法找到一种算法来确定距离是多少。任何帮助表示赞赏。

import javax.swing.JFrame;


public class Main extends JFrame {
    Screen screen;

    public Main() {

        setSize(500, 400);
        setResizable(false);
        setTitle("The Adventures of BLANK") ;
        setLocationRelativeTo(null) ;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();

    }


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

    public void init() {
        screen = new Screen();
        add(screen);
        addKeyListener(screen);
        setVisible(true);
    }
}

第二类

public void loadImages() {
    try {
        awesomeFace = ImageIO.read(getClass().getResource("/AwesomeFace.png"));
        Trollface = ImageIO.read(getClass().getResource("/Trollface.png")) ;
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public Screen() {
    loadImages();
    Thread thread = new Thread(this);
    thread.start();
}

public void run() {
    while(running) {
        //Game Loop Start
        if (trolly+trollx > x + y) {
            proximity = trolly+trollx - x + y ;
        } else {
            proximity = x+y - trollx +trolly ;
        }

        if(left) {
            if (proximity <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (x == 0) {

            } else {
                x--;
            }
        }

        if(right) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (x >=450) {

            } else {
                x++;
            }
        }

        if(up) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (y == 0) {
            } else {
            y--;
            }
        }

        if(down) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (y == 325) {
            } else {
            y++;
            }
        }

        if (trollup) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
        if (trolly == 0) {
            } else {
            trolly -- ;
            }
    }
        if (trolldown) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (trolly == 325) {
                } else {
                trolly ++ ;
                }
        }
        if (trollleft) {
            if (proximity <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (trollx == 0) {
                } else {
                trollx -- ;
                }
        }
        if (trollright == true) {
            if (trollx - x <= 3) {
                y = 700 ;
                x = 700 ;
            }if (trollx == 450) {
                } else {
                trollx ++ ;
                }

        }




        //Game Loop End
        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    //Graphical loop start
    g.drawImage(awesomeFace, x, y, null);
    g.drawImage(Trollface, trollx, trolly, null) ;


    //Graphical loop end
    repaint();
}

@Override
public void keyPressed(KeyEvent e) {

    if(e.getKeyCode() == 87) {
        trollup = true;
    }

    if(e.getKeyCode() == 65){
        trollleft = true;
    }

    if(e.getKeyCode() == 83) {
        trolldown = true;
    }

    if(e.getKeyCode() == 68) {
        trollright = true;
    }

    if(e.getKeyCode()==37) {
        //Left
        left = true;
    }

    if(e.getKeyCode()==38) {
        //Up
        up = true;
    }

    if(e.getKeyCode()==39) {
        //Right
        right = true;
    }
    if(e.getKeyCode()==40) {
        //Down
        down = true;
    }
}

public void keyReleased(KeyEvent e) {
    if(e.getKeyCode()==37) {
        //Left
        left = false;
    }

    if(e.getKeyCode()==38) {
        //Up
        up = false;
    }

    if(e.getKeyCode()==39) {
        //Right
        right = false;
    }
    if(e.getKeyCode()==40) {
        //Down
        down = false;
    }
    if(e.getKeyCode() == 87) {
        trollup = false;
    }

    if(e.getKeyCode() == 65){
        trollleft = false;
    }

    if(e.getKeyCode() == 83) {
        trolldown = false;
    }

    if(e.getKeyCode() == 68) {
        trollright = false;
    }
}

@Override
public void keyTyped(KeyEvent e) {

}

0 个答案:

没有答案