2016-05-24 3 views
-2

私はJavaで基本的な迷路ゲームを作っています。 GraphicのdrawImageメソッドを使用してJPanelに迷路のシンプルなモノクロPNG画像をロードしました。描画された線がイメージの黒いピクセルと交差するか、または黒いピクセルに接触するかどうかを検出するにはどうすればよいですか?

JPanel(迷路を通過する人の経路を表す)上に線を描くかどうかは、迷路の壁(別名黒い線)に触れる必要があります。私は描画する線の開始点と終了点をすでに知っており、すべての黒いピクセル(壁を表す)の位置を見つけることができると思います。

私は、迷路の画像内のすべての黒いピクセルの配列を作成し、各黒いピクセルの位置に基づいて小さな矩形の配列を作成し、最初の線が任意の長方形。

これを行うには、より簡単で効率的な方法がありますか?迷路の黒い線の地図を作成する方法はありますか?

以下は、下の図の責任を負うコードのセクションです。

public class TurtlePlane extends JPanel 
{ 
    private TurtleController myTurtleController; 
    private BufferedImage myMaze; 
    private BufferedImage myMazeExplorer; 

    public TurtlePlane() throws IOException{ 
     setLayout (null); 
     setBorder(BorderFactory.createLoweredBevelBorder()); 
     setBackground(Color.decode("0xEDFFED")); 
     setPreferredSize(new Dimension(800, 800)); 

     myMaze = ImageIO.read(new File("src/mazeforkids/Mazeforkids.png")); 
     myMazeExplorer = ImageIO.read(new File("src/mazeforkids/FlyingKappa.gif")); 
    } 

    public void drawPlane(){ 
     repaint(); 
    } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 

     //The Turtle object stores the x and y coordinates of the maze explorer image 
     Turtle t = new Turtle(12, 753); 

     g.drawImage(myMaze, 0, 0, null);//loads the image of the maze onto the JPanel 
     myTurtleController.getTurtleProgram().execute(t);//updates the coordinates of the maze explorer image 
     g.drawImage(myMazeExplorer, t.getX(), t.getY(), null);//loads the image of the maze explorer onto the JPanel 
    } 
} 

Maze game

+0

一般的なアプローチは疑問に思えるかもしれませんが、質問自体に焦点を当てています。*「JPanelで描いた線」はどのように表現されていますか?このラインの始点と終点だけを持っていますか? – Marco13

+0

なぜ迷路は 'java.awt.Shape'とは対照的に画像ですか? –

+0

も参照してください[この回答](http://stackoverflow.com/questions/14574045/collision-detection-with-complex-shapes/14575043#14575043).. –

答えて

-1

あなたはjava.awt.Robotを使用することができます。これは、画面の任意の場所にピクセルの色を取得するメソッドを持っています。これを実装する方法はあなた次第です。

+0

スレッジハンマーを使用してナットを割る。 OPは明らかに画像に直接アクセスできるので、BufferedImageを作成して['getRGB(x、y)'](http://docs.oracle.com/javase/8/docs/api/java/)を使うことができます。 awt/image/BufferedImage.html#getRGB-int-int-) –

+0

私はsledgehammersが好きです – RobotKarel314

関連する問題