package com.google.code.timetrail.gui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.backend.Place;
public class MapPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private Control gameControl;
private BufferedImage background;
private BufferedImage temp;
private Image spaceship;
public MapPanel(String filename1,String filename2,Control gameControl) {
try {
background = ImageIO.read(new File(filename1));
temp = ImageIO.read(new File(filename2));
spaceship = this.makeColorTransparent(temp, Color.black);
} catch (IOException e) {
e.printStackTrace();
}
this.gameControl=gameControl;
setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
int height=this.getSize().height;
int width=this.getSize().width;
g.drawImage(background, 0, 0,width,height,null);
g.fillRect(0, height/2, width, 4);
for(Place p:Place.values())
{
if(!p.equals(Place.TRANSIT))
{
g.drawOval((int)((p.getLocation()/((double)(Place.OREGON.getLocation()))*width-30)) + 20, (height/2)-4, 10, 10);
}
}
if(gameControl.getCurrentPlace().equals(Place.TRANSIT) ||gameControl.getCurrentPlace().equals(Place.INDEPENDENCE) )
{
g.setColor(Color.WHITE);
} else {
g.setColor(Color.RED);
}
g.fillOval((int) ((gameControl.getTime().getTimeTraveled()/((double)(Place.OREGON.getLocation()))*width-30)) + 20, (height/2)-4, 10, 10);
g.drawImage(spaceship, (int) ((gameControl.getTime().getTimeTraveled()/((double)(Place.OREGON.getLocation()))*width)-30) + 12, (height/2)-50, 30, 30,null);
}
private Image makeColorTransparent (BufferedImage im, final Color color) {
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
} else {
// nothing to do
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
}