import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Window extends Applet implements Runnable
{
/**
*
*/
private static final long serialVersionUID = -8255319694373975038L;
private static final float MAX_ALPHA = 3;
private static final float MIN_ALPHA = -.5f;
private float alpha;
private float changeOfAlpha;
private int wordYPosition;
private int wordXPosition;
private int wordYSpeed;
private int wordXSpeed;
private PictureGenerator myImageGenerator;
private WordGenerator myWordGenerator;
private Thread myThread;
private BufferedImage myImage;
private Image doubleImage;
private Graphics doubleGraphic;
private boolean isDrawingPicture;
private float[] scales = { 1f, 1f, 1f, 0.5f };
private float[] offsets = new float[4];
private RescaleOp rop;
public void init()
{
this.setSize(400, 300);
this.setBackground(Color.WHITE);
alpha = MAX_ALPHA;
changeOfAlpha = -.001f;
isDrawingPicture = true;
myImageGenerator = new PictureGenerator();
myWordGenerator = new WordGenerator();
wordXPosition = 0;
wordYPosition = (int)((Math.random() * 200) + 100);
wordXSpeed = 1;
wordYSpeed = 1;
this.setupImage();
this.start();
}
public void start(){
myThread = new Thread(this);
myThread.start();
}
public void stop()
{
myThread = null;
}
public void run()
{
while(true)
{
while(isDrawingPicture)
{
try {
Thread.sleep(10);
if(alpha < MIN_ALPHA)
{
this.setupImage();
changeOfAlpha = changeOfAlpha * -1;
isDrawingPicture = false;
alpha = -.4f;
}
else if(alpha > MAX_ALPHA)
{
changeOfAlpha = changeOfAlpha * -1;
}
if(isDrawingPicture)
{
alpha += changeOfAlpha;
setOpacity(alpha);
this.repaint();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
while(!isDrawingPicture)
{
try
{
Thread.sleep(10);
this.checkWordPosition();
if(!isDrawingPicture)
{
wordXPosition += wordXSpeed;
wordYPosition += wordYSpeed;
this.repaint();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
private void checkWordPosition()
{
if(wordXPosition > 400)
{
this.resetWord();
}
if(wordYPosition > 300)
{
wordYSpeed = wordYSpeed * -1;
}
else if(wordYPosition < 0)
{
wordYSpeed = wordYSpeed * -1;
}
}
public void update(Graphics g)
{
if (doubleImage == null)
{
doubleImage = createImage (this.getSize().width, this.getSize().height);
doubleGraphic = doubleImage.getGraphics ();
}
// clear screen in background
doubleGraphic.setColor (getBackground ());
doubleGraphic.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
doubleGraphic.setColor (getForeground());
paint (doubleGraphic);
// draw image on the screen
g.drawImage (doubleImage, 0, 0, this);
}
public void paint(Graphics g)
{
if(isDrawingPicture)
{
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(myImage, rop, 0, 0);
}
else
{
g.drawString("Hello World", wordXPosition, wordYPosition);
}
}
public void resetWord()
{
wordXPosition = 0;
wordYPosition = (int)((Math.random() * 200) + 100);
isDrawingPicture = true;
}
private void setupImage()
{
try
{
BufferedImage tempImage = ImageIO.read(new File(myImageGenerator.getNewPicture()));
int width = tempImage.getWidth(null);
int height = tempImage.getHeight(null);
myImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics tempGraphics = myImage.getGraphics();
tempGraphics.drawImage(tempImage, 0, 0, null);
} catch (IOException e) {
System.out.println("Image could not be read");
System.exit(1);
}
this.setOpacity(alpha);
}
private void setOpacity(float opacity)
{
scales[3] = opacity;
rop = new RescaleOp(scales, offsets, null);
}
}