?? reflectionapplet.java
字號:
import java.applet.Applet;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
//圖片倒影
public class ReflectionApplet extends Applet implements Runnable {
Thread lakeThread; //圖片倒影線程
Graphics graphics; //該Applet的Graphics對象
Graphics waveGraphics; //倒影的Graphics對象
Image image; //Applet的Image對象
Image waveImage; //倒影的Graphics對象
int currentImage,imageWidth,imageHeight;
boolean imageLoaded; //圖片是否裝載
String imageName; //圖片名稱
public void init() {
imageName= getParameter("image"); //得到圖片名稱
}
public void start() {
if (lakeThread == null) {
lakeThread= new Thread(this); //實例化線程
lakeThread.start(); //運行線程
}
}
public void run() {
currentImage= 0;
if (!imageLoaded) { //如果圖片未裝載
repaint(); //得繪屏幕
graphics= getGraphics(); //得到Graphics對象
MediaTracker mediatracker= new MediaTracker(this); //實例化MediaTracker對象
image= getImage(getDocumentBase(), imageName); //得到Image實例
mediatracker.addImage(image, 0); //增加待加載的圖片
try {
mediatracker.waitForAll(); //裝載圖片
imageLoaded= !mediatracker.isErrorAny(); //是否有錯誤發生
} catch (InterruptedException ex) {
}
if (!imageLoaded) { //加載圖片失敗
stop(); //Applet停止運行
graphics.drawString("加載圖片錯誤", 10, 40); //輸出錯誤信息
return;
}
imageWidth= image.getWidth(this); //得到圖像寬度
imageHeight= image.getHeight(this); //得到圖像高度
createAnimation(); //創建動畫效果
}
repaint(); //重繪屏幕
try {
while (true) {
repaint(); //得繪屏幕
currentImage++;
if (currentImage == 12)
currentImage= 0;
Thread.sleep(50); //線程休眠
}
} catch (InterruptedException ex) {
stop();
}
}
public void createAnimation() {
Image img= createImage(imageWidth, imageHeight); //以圖像高度創建Image實例
Graphics g= img.getGraphics(); //得到Image對象的Graphics對象
g.drawImage(image, 0, 0, this); //繪制Image
for (int i= 0; i < imageHeight ; i++) {
g.copyArea(0,imageHeight-1-i,imageWidth,1,0,-imageHeight+1+(i*2)); //拷貝圖像區域
}
waveImage= createImage(13 * imageWidth, imageHeight); //得到波浪效果的Image實例
waveGraphics= waveImage.getGraphics(); //得到波浪效果的Graphics實例
waveGraphics.drawImage(img, 12 * imageWidth, 0, this); //繪制圖像
int j= 0;
while (j<12){
makeWaves(waveGraphics, j);
j++;
}
g.drawImage(image, 0, 0, this); //繪制圖像
}
public void makeWaves(Graphics g, int i) { //波浪效果模擬
double d= (6.2831853071795862 * i) / 12;
int j= (12 - i) * imageWidth;
int waveHeight=imageHeight / 16;
for (int l= 0; l < imageHeight; l++) {
int k=(int) ((waveHeight* (l + 28)* Math.sin(waveHeight * (imageHeight - l)/ (l + 1)+ d))/imageHeight);
if (l < -k)
g.copyArea(12 * imageWidth, l, imageWidth, 1, -j, 0); //拷貝圖像區域,形成波浪
else
g.copyArea(12 * imageWidth, l + k, imageWidth, 1, -j, -k);
}
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g){
if (waveImage != null) {
g.drawImage(waveImage,-currentImage * imageWidth,imageHeight-1, this); //繪制圖像
g.drawImage(waveImage,(12 - currentImage) * imageWidth, imageHeight-1,this);
}
g.drawImage(image, 0, 1, this);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -