📄 demothread.java~57~
字号:
package multimediademo;
import java.net.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.image.*;
import java.awt.*;
import multimediademo.ImageFilter;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class DemoThread extends Thread{
//********************************类变量**********************************
//四个私有变量:滚动面板对象、图像类对象、图形对象和动画类型值
private JScrollPane m_scroll;
private Image sourceImage, filteredImage;
private Graphics m_graph;
//1表示缩放平移动画,2表示不擦除平移动画,3表示淡入淡出动画
private int m_demoType;
//申明一个图像过滤器
ImageFilter imageFilter;
//定义初始点
int startX = 50;
int startY = 50;
boolean REPAINT_MARK;
int imageWidth,imageHeight;
int panelWidth,panelHeight;
int m_imageWidth,m_imageHeight;
int STEP = 10;
//********************************类方法**********************************
//默认构造函数
public DemoThread() {
}
//重载构造函数
public DemoThread(Image image,JScrollPane scrollPanel,int demoType){
//给私有变量赋值
sourceImage = image;
filteredImage = image;
m_scroll = scrollPanel;
m_demoType = demoType;
//获取图形对象
m_graph = m_scroll.getGraphics();
//获取面板对象和图像的尺寸
imageWidth = filteredImage.getWidth((Component)m_scroll);
imageHeight = filteredImage.getHeight((Component)m_scroll);
panelWidth = m_scroll.getWidth();
panelHeight = m_scroll.getHeight();
}
//重载线程操作函数
public void run(){
initializeRun();
//moveDirect表示图像水平移动的方向:取true表示由左向右移动,false则表示相反的方向
boolean moveDirect = true;
while(true){
//画动画的第一副图像
if(!REPAINT_MARK){
m_graph.drawImage(filteredImage,startX,startY,m_imageWidth,m_imageHeight,m_scroll);
try{
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
REPAINT_MARK = true;
}
//画动画的其他图像:
//m_demoType表示动画类型,1表示缩放平移动画,2表示不擦除平移动画,3表示淡入淡出动画
else{
switch (m_demoType) {
//水平移动缩放动画
case 1:
if(moveDirect){ //图像由左向右移动并缩小
m_graph.clearRect(startX, startY, m_imageWidth, m_imageHeight);
startX += STEP;
//判断动画移动是否反向
if ((startX + m_imageWidth) > panelWidth) {
startX -= STEP;
moveDirect = false;
}
else {
m_imageWidth = (int) (m_imageWidth * 0.9);
m_imageHeight = (int) (m_imageHeight * 0.9);
startY = (panelHeight - m_imageHeight) / 2;
m_graph.drawImage(filteredImage, startX, startY, m_imageWidth,
m_imageHeight, m_scroll);
}
}
//图像由右向左移动并放大
else{
m_graph.clearRect(startX, startY, m_imageWidth, m_imageHeight);
startX -= STEP;
//判断动画移动是否反向
if(startX < 10){
moveDirect = true;
startX += STEP;
}
else{
m_imageWidth = (int) (m_imageWidth / 0.9);
m_imageHeight = (int) (m_imageHeight / 0.9);
startY = (panelHeight - m_imageHeight) / 2;
m_graph.drawImage(filteredImage, startX, startY, m_imageWidth,
m_imageHeight, m_scroll);
}
}
break;
//不擦除平移动画
case 2:
//不擦除显示动画
if(moveDirect){
STEP = m_imageWidth + 10;
startX += STEP;
if ( (startX + m_imageWidth) <= panelWidth) {
m_imageWidth = (int) (m_imageWidth * 0.9);
m_imageHeight = (int) (m_imageHeight * 0.9);
startY = (panelHeight - m_imageHeight) / 2;
m_graph.drawImage(filteredImage, startX, startY, m_imageWidth,
m_imageHeight, m_scroll);
}
else{
startX -= STEP;
moveDirect = false;
}
}
//逐一擦除图像
else{
m_graph.clearRect(startX-10,startY-10,m_imageWidth+15,
m_imageHeight+15);
m_imageWidth = (int) (m_imageWidth / 0.9) ;
m_imageHeight = (int) (m_imageHeight / 0.9);
startY = (panelHeight - m_imageHeight) / 2;
STEP = m_imageWidth + 10;
startX -= STEP;
if(startX < 0){
//擦除完所有图像后,在显示第一副图像前停留1秒后画第一副图像
try{
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
moveDirect = true;
startX = 10;
m_imageWidth = 200;
m_imageHeight = 200;
startY = (panelHeight - m_imageHeight)/2;
m_graph.drawImage(filteredImage,startX,startY,m_imageWidth,
m_imageHeight,m_scroll);
}
}
break;
//淡入淡出动画
case 3:
break;
//
default:
break;
}
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {}
}
}
}
//图像过虑函数
private void filteringImage(){
ImageProducer producer = sourceImage.getSource();
producer = new FilteredImageSource(producer,imageFilter);
filteredImage = m_scroll.createImage(producer);
}
//初始化线程运行参数函数
public void initializeRun(){
m_imageWidth = imageWidth;
m_imageHeight = imageHeight;
if(m_imageWidth > 200)
m_imageWidth = 200;
if(m_imageHeight > 200)
m_imageHeight = 200;
//m_demoType表示动画类型,1表示缩放平移动画,2表示不擦除平移动画,3表示淡入淡出动画
switch(m_demoType){
case 1:
startX = 10;
startY = (panelHeight - m_imageHeight)/2;
STEP = 50;
break;
case 2:
startX = 10;
startY = (panelHeight - m_imageHeight)/2;
break;
case 3:
startX = (panelWidth - m_imageWidth)/2;
startY = (panelHeight - m_imageHeight)/2;
break;
}
m_graph.clearRect(0,0,panelWidth,panelHeight);
REPAINT_MARK = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -