How to navigate
Sorry about cross posting but no one didn't answer to this in game programming board.
Here is sample program that paints hearts randomly. Then it is possible to change selection using arrow keys. Currently the movement is based on random but I would like to make it working e.g. like windows desktop.
Could you help me with the navigation or provide some links to documentation where 2D navigation algorithm is explained?
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.List;
publicclass CanvasDemoextends JPanel
{
static String imageFileName ="hearts_original.png";
List<SpriteImage> images =new ArrayList<SpriteImage>();
Random rand =new Random();
boolean init =true;
public CanvasDemo ()
{
init();
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
{
publicvoid eventDispatched(AWTEvent event)
{
KeyEvent ke = (KeyEvent) event;
int direction = ke.getKeyCode();
if (ke.getID() == KeyEvent.KEY_PRESSED && (
direction == KeyEvent.VK_UP ||
direction == KeyEvent.VK_DOWN ||
direction == KeyEvent.VK_LEFT ||
direction == KeyEvent.VK_RIGHT ) )
{
changeSelection( direction );
}
}
}, AWTEvent.KEY_EVENT_MASK);
}
publicvoid init()
{
buildUI();
}
publicvoid paint(Graphics g)
{
super.paint( g );
for (SpriteImage image : images)
{
image.paint2( g );
}
}
publicvoid changeSelection(int direction )
{
if( images.size() == 0 )
{
return;
}
// find out selected image
SpriteImage selected =null;
for (SpriteImage image : images)
{
if( image.getSelect() )
{
selected = image;
break;
}
}
// hack to change selection, might return the original
SpriteImage random = images.get( rand.nextInt( images.size() ) );
selected.select(false );
random.select(true );
repaint();
// someone selected (should be always one)
if( selected !=null )
{
//get its location
int selectedX = selected.getXCoord();
int selectedY = selected.getYCoord();
if (direction == KeyEvent.VK_UP)
{
System.out.println("VK_KP_UP");
}
elseif (direction == KeyEvent.VK_DOWN)
{
System.out.println("VK_KP_DOWN");
}
elseif (direction == KeyEvent.VK_LEFT)
{
System.out.println("VK_KP_LEFT");
}
elseif (direction == KeyEvent.VK_RIGHT)
{
System.out.println("VK_KP_RIGHT");
}
}
}
publicvoid generate()
{
int x = rand.nextInt( 250 );
int y = rand.nextInt( 250 );
SpriteImage image =new SpriteImage(new File( imageFileName ), x, y );
image.select( init );
// first one is selected
init =false;
images.add(image);
repaint();
}
publicvoid buildUI(){
JButton jumbleButton =new JButton("Generate");
jumbleButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
CanvasDemo.this.generate();
};
});
add("South", jumbleButton);
}
publicstaticvoid main(String s[])
{
JFrame f =new JFrame("Jumbled Image");
f.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){System.exit(0);}
});
CanvasDemo jumbler =new CanvasDemo();
f.add("Center", jumbler);
f.setSize(new Dimension( 300, 300 ) );
f.setVisible(true);
}
}
class SpriteImage
{
privateint xCoord, yCoord;
private BufferedImage bi, select;
privateboolean isSelected;
public SpriteImage( File image,int x,int y )
{
this( image );
xCoord = x;
yCoord = y;
}
public SpriteImage(File imageSrc){
try{
bi = ImageIO.read(imageSrc);
select = ImageIO.read(new File("hearts_selection.png") );
}catch (IOException e){
System.out.println("Image could not be read");
System.exit(1);
}
}
publicint getXCoord()
{
return xCoord;
}
publicint getYCoord()
{
return yCoord;
}
publicvoid select(boolean state )
{
isSelected = state;
}
publicboolean getSelect()
{
return isSelected;
}
publicvoid paint2(Graphics g)
{
g.drawImage(isSelected ? select : bi, xCoord, yCoord,null);
}
}

