Thursday, December 24, 2009

Help with some Java coding. ?

import java.awt.Canvas;


import java.awt.Dimension;


import java.awt.Graphics;


import java.awt.event.WindowAdapter;


import java.awt.event.WindowEvent;


import java.awt.image.BufferedImage;


import java.net.URL;


import java.util.HashMap;





import javax.imageio.ImageIO;


import javax.swing.JFrame;


import javax.swing.JPanel;








public class Invaders extends Canvas {


public static final int WIDTH = 800;


public static final int HEIGHT = 600;





public HashMap sprites;


public int posX,posY;








public Invaders() {


sprites = new HashMap();








JFrame ventana = new JFrame(';Invaders';);


JPanel panel = (JPanel)ventana.getContentPane();


setBounds(0,0,WIDTH,HEIGHT);


panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));


panel.setLayout(null);


panel.add(this);





ventana.setBounds(0,0,WIDTH,HEIGHT);


ventana.setVisible(true);


ventana.addWindowListener( new WindowAdapter() {


public void windowClosing(WindowEvent e) {


System.exit(0);


}


});


}





public BufferedImage loadImage(String number){


URL url=null;


try{


url = getClass().getClassLoader().getResource(鈥?br>

return ImageIO.read(url);


} catch (Exception e){


System.out.print(';Error Loading File bicho.gif';);





System.exit(0);


return null;


}


}





public BufferedImage getSprite(String number){


BufferedImage img = (BufferedImage)sprites.get(number);


if (img == null){


img = loadImage(';res/';+number);


sprites.put(number,img);


}


return img;


}


public void paint(Graphics g) {





g.drawImage(getSprite(';bicho.gif';), 568, 54,this);


}





public void updateWorld(){


posX = (int)(Math.random()*WIDTH);


posY = (int)(Math.random()*HEIGHT);





}


public void game(){


while (isVisible()){


updateWorld();


paint(getGraphics());


}


}








public static void main(String[] args) {


Invaders inv = new Invaders();





}


}





Now, I keep getting the error message [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.HashMap , I don't know how to fix it, I think it has something to do with generic's, but how would I go about correcting the problem? Help with some Java coding. ?
Try replacing the line





public HashMap sprites;





with





public HashMap%26lt;String, BufferedImage%26gt; sprites;





Alternatively when you compile you could add the parameter -source 1.4

No comments:

Post a Comment