package at.kai.applet.game.spaceView.planet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
/**
* Abstract class for planet. Handle mouseaction and covering.
* @author Kai
*/
abstract class AbstractPlanet extends JLabel implements MouseListener, MouseMotionListener
{
private static final long serialVersionUID = 1L;
/**
* Color to cover planet.
*/
private static final Color COVER_COLOR = new Color(127, 127, 127, 255 - 80);
/**
* Is true, if the mouse on non-alpha sector on label. False otherwise.
*/
private boolean mouseOn;
/**
* True, if the planet is painting covered, false otherwise.
*/
private boolean cover;
/**
* Called from subclasses per super.
*/
AbstractPlanet()
{
super();
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* Set covered or discovered.
* @param b
*/
public void setCover(boolean b)
{
cover = b;
repaint();
}
/**
* Paint the planet or his cover.
*/
public void paint(Graphics g)
{
super.paint(g);
if(cover)
{
g.setColor(COVER_COLOR);
g.fillOval(0, 0, getWidth(), getHeight());
}
}
/**
* Called, if the mouse has pressed on a avaible pixel.
* @param arg0
*/
abstract void mouseHasPressed(MouseEvent arg0);
/**
* Call mouseHasPressed, if the mouse is on a avaible pixel on label and is pressed.
*/
public void mousePressed(MouseEvent arg0)
{
if(mouseOn)
mouseHasPressed(arg0);
}
/**
* Method from MouseMotionListener, called if the mouse on label is moved.<br>
* Check wheter the mouse is on alpha pixel and set mouseOn on true or false.
*/
public void mouseMoved(MouseEvent arg0)
{
BufferedImage bufferedImage = bufferedImageFromLabel();
int x = arg0.getX();
int y = arg0.getY();
if(!mouseOnAlphaPixel(x, y, bufferedImage))
{
if(!mouseOn)
mouseOn = true;
}
else
{
if(mouseOn)
mouseOn = false;
}
}
/**
* Get the BufferedImage from Icon from label.<br>
* If the Icon contain not a BufferedImage, a new BufferedImage will be created<br>
* and set as Icon to Label.
* @return BufferedImage from Icon from label.
*/
private BufferedImage bufferedImageFromLabel()
{
boolean repeat = false;
BufferedImage bufferedImage = null;
do
{
repeat = false;
Image image = null;
try
{
ImageIcon imageIcon = (ImageIcon) getIcon();
image = imageIcon.getImage();
bufferedImage = (BufferedImage)image;
}
catch (ClassCastException e)
{
setIcon(new ImageIcon(imageToBufferedImage(image)));
repeat = true;
}
}
while(repeat);
return(bufferedImage);
}
/**
* Return true if the pixel on BufferedImage is full alpha, false otherwise.
* @param x X-Coordinates of pixel.
* @param y Y-Coordinates of pixel.
* @param image Image to check.
* @return true if the pixel on BufferedImage is full alpha, false otherwise.
*/
private boolean mouseOnAlphaPixel(int x, int y, BufferedImage image)
{
int rgb = image.getRGB(x, y);
return((alphaFromRgb(rgb) == 0 ? true : false));
}
/**
* Get alpha from rgb-value from image.
* @param rgb The rgb-value to check.
* @return The alpha from rgb.
*/
private int alphaFromRgb(int rgb)
{
return((rgb >> 24) & 0x000000FF);
}
/**
* Convert a image to a BufferedImage.
* @param image Image to paint on BufferedImage.
* @return A new BufferedImage.
*/
private BufferedImage imageToBufferedImage(Image image)
{
int width = image.getWidth(this);
int height = image.getWidth(this);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(image, 0, 0, this);
g.dispose();
return(bufferedImage);
}
/**
* Set mouseOn to false, if the label is exited.
*/
public void mouseExited(MouseEvent arg0)
{
if(mouseOn)
mouseOn = false;
}
public void mouseEntered(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
public void mouseClicked(MouseEvent arg0) {}
public void mouseDragged(MouseEvent arg0) {}
}
///////////////////////////////////////////////////////////////////////////////////////
package at.kai.applet.game.spaceView.planet;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import at.kai.applet.Applet;
import at.kai.applet.game.spaceView.planet.planetInfoFrame.PlanetInfoFrame;
public class Planet extends AbstractPlanet
{
private static final long serialVersionUID = 1L;
private long population;
private long iron;
private long copper;
private long steel;
private long deuterium;
private long hydrogen;
private PlanetInfoFrame planetInfoFrame;
public Planet(Applet applet, JPanel framePanel)
{
super();
planetInfoFrame = new PlanetInfoFrame(this);
framePanel.add(planetInfoFrame);
}
void mouseHasPressed(MouseEvent arg0)
{
planetInfoFrame.fadeIn();
}
public long getPopulation()
{
return population;
}
public long getIron()
{
return(iron);
}
public long getCopper()
{
return(copper);
}
public long getSteel()
{
return(steel);
}
public long getDeuterium()
{
return(deuterium);
}
public long getHydrogen()
{
return(hydrogen);
}
}
///////////////////////////////////////////////////////////////////////////////////////
package at.kai.applet.game.spaceView.menu.planetInfoFrame;
import java.util.concurrent.TimeUnit;
/**
* Fade the InfoFrame in and out.
* @author Kai
*/
class FadeThread extends Thread
{
private static final byte FADE_IN = -128;
private static final byte FADE_OUT = -127;
private byte fade;
private PlanetsInfoFrame planetsInfoFrame;
private Thread thread;
FadeThread(PlanetsInfoFrame planetsInfoFrame)
{
super();
this.planetsInfoFrame = planetsInfoFrame;
}
public void run()
{
int fullWidth = PlanetsInfoFrame.SIZE.width;
int fullHeight = PlanetsInfoFrame.SIZE.height;
if(fade == FADE_IN)
{
planetsInfoFrame.setSize(2, 2);
planetsInfoFrame.setVisible(true);
for(; planetsInfoFrame.getWidth() < fullWidth;)
{
planetsInfoFrame.setSize(planetsInfoFrame.getWidth() + 2, planetsInfoFrame.getHeight());
planetsInfoFrame.setLocation(planetsInfoFrame.getX() - 1, planetsInfoFrame.getY());
sleep(TimeUnit.NANOSECONDS, 500L);
}
for(; planetsInfoFrame.getHeight() < fullHeight;)
{
planetsInfoFrame.setSize(planetsInfoFrame.getWidth(), planetsInfoFrame.getHeight() + 2);
planetsInfoFrame.setLocation(planetsInfoFrame.getX(), planetsInfoFrame.getY() - 1);
sleep(TimeUnit.NANOSECONDS, 500L);
}
}
if(fade == FADE_OUT)
{
for(; planetsInfoFrame.getWidth() > 2;)
{
planetsInfoFrame.setSize(planetsInfoFrame.getWidth() - 2, planetsInfoFrame.getHeight());
planetsInfoFrame.setLocation(planetsInfoFrame.getX() + 1, planetsInfoFrame.getY());
sleep(TimeUnit.NANOSECONDS, 500L);
}
for(; planetsInfoFrame.getHeight() > 2;)
{
planetsInfoFrame.setSize(planetsInfoFrame.getWidth(), planetsInfoFrame.getHeight() - 2);
planetsInfoFrame.setLocation(planetsInfoFrame.getX(), planetsInfoFrame.getY() + 1);
sleep(TimeUnit.NANOSECONDS, 500L);
}
planetsInfoFrame.setVisible(false);
}
}
synchronized void faidIn()
{
fade = FADE_IN;
try
{
if(thread != null)
thread.join();
}
catch (InterruptedException e) {}
thread = new Thread(this);
thread.start();
}
synchronized void faidOut()
{
fade = FADE_OUT;
try
{
if(thread != null)
thread.join();
}
catch (InterruptedException e) {}
thread = new Thread(this);
thread.start();
}
public void sleep(TimeUnit tu, long timeFaktor)
{
try
{
tu.sleep(timeFaktor);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}