⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 water.java

📁 包含6个源码。主要是使用APPLET对图形图像进行处理。 有旋转
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
import java.util.*;

public class Water extends Applet implements Runnable,MouseListener
{
	Thread waterThread;
	MediaTracker mediaTrack;
	String ca = "WWW.YNU.EDU.CN-LIWEI";
	int width,height;
	int pixels[];//与图象象素大小相同的数组
	int p[];
	Image image,offImage;
	int [] buf1;
	int [] buf2;
	int [] bufTemp;
	int imageX,imageY;
	int i,j,k,x,y,posx,posy;
	int stoneSize = 3;
	int s = 0;
	int frameDelay = 10;
	int resistance;
	int pulse;
	int autoDelay,autoCount;
	boolean autoStimulation;
	boolean registered;
	Random r; 
	
	public void init()
    {
    	r = new Random();
    	mediaTrack = new MediaTracker(this);
    	if(getDocumentBase().toString().startsWith("file"))//本地使用则注册标志为真
            registered = true;
    	if(!ca.equals(getParameter("REGISTER")))
        	registered =false;
        stoneSize = Integer.parseInt(getParameter("stoneSize"));
    	frameDelay = Integer.parseInt(getParameter("frameDelay"));
    	resistance = Integer.parseInt(getParameter("resistance"));
    	pulse = Integer.parseInt(getParameter("pulse"));
    	imageX = Integer.parseInt(getParameter("imageX"));
    	imageY = Integer.parseInt(getParameter("imageY"));
    	image = getImage(getDocumentBase(),getParameter("image"));
    	if(getParameter("autoStimulation").equals("1"))
    		autoStimulation = true;
    	autoDelay = Integer.parseInt(getParameter("autoDelay")) / frameDelay;
    	mediaTrack.addImage(image,0);
    	try
    	{
    		mediaTrack.waitForID(0);
    	}catch(InterruptedException ie)
    	{
    		ie.printStackTrace();
    	}
    	width = image.getWidth(this);
    	height = image.getHeight(this);
    	pixels = new int[width * height];
    	p = new int[width * height];
    	buf1 = new int[width * height];
    	buf2 = new int[width * height];
    	bufTemp = new int[width * height];
    	try
    	{
    		PixelGrabber pg = new PixelGrabber(image,0,0,width,height,pixels,0,width);
    		pg.grabPixels();
    	}catch(InterruptedException ie)
    	{
    		ie.printStackTrace();
    	}
    	for(k = 0;k < width * height - 2 * width;k++)
    	{
    		buf1[k] = 0;
    		buf2[k] = 0;
    	}
    	addMouseListener(this);
    }
    
    	public void mouseClicked(MouseEvent e){}
    	public void mouseExited(MouseEvent e){}
    	public void mouseEntered(MouseEvent e){}
    	public void mouseReleased(MouseEvent e){}
    	public void mousePressed(MouseEvent e)
    	{
    		x = e.getX() - imageX;
    		y = e.getY() - imageY;
    		for(posx = x - stoneSize;posx < x + stoneSize;posx++)
    		{
    			for(posy = y - stoneSize;posy < y + stoneSize;posy++)
    			{
    				if((posx - x) * (posx - x) + (posy - y) * (posy -y) < stoneSize * stoneSize)
    					buf1[width * posy + posx] = -pulse;
    			}
    		}
    	}	
    
  
    public int [] Spread(int [] buf1)
    {
    	for(i = width;i < width * height - 2 * width;i++)
    	{
    		buf2[i] = ((buf1[i - 1] + buf1[i + 1] + buf1[i - width] + buf1[i + width]) >> 1) - buf2[i];
    		buf2[i] -= buf2[i] >> resistance;
    	}
    	return buf2;
    }
    
    public int [] Render(int [] buf1)
    {
		int xoff,yoff;
		int n = width;
		for(j = 1;j < width * height - 2 * width;j++)
		{
			xoff = buf1[n - 1] - buf1[n + 1];
			yoff = buf1[n - width] - buf1[n + width];
			int z = j + width * yoff + xoff;
			if(j + width * yoff + xoff > 0 && j + width * yoff + xoff < width * height)
				p[j] = pixels[j + width * yoff + xoff];
			n++;
		}
		return p;
    }
 	
 	public void paint(Graphics g)
    {
    	if(autoStimulation)
    	{
    		autoCount++;
    		if(autoCount >= autoDelay)
    		{
    			int i = r.nextInt(width * height - 2 * width);
    			buf1[i] = -pulse;
    			autoCount = 0;
    		}
    	}
    	buf2 = Spread(buf1);
    	for(i = 0;i < width * height;i++)
    	{
    		bufTemp[i] = buf1[i];
    		buf1[i] = buf2[i];
    		buf2[i] = bufTemp[i];
    	}
    	p = Render(buf1);
    	ImageProducer ip = new MemoryImageSource(width,height,p,0,width);
    	offImage = createImage(ip);
    	g.drawImage(offImage,imageX,imageY,this);
    	/*try
    	{
    		Thread.sleep(10);
    	}catch(InterruptedException ie)
    	{
    		ie.print
    	}*/
    	//repaint();
	}
	
    public void stop()
    {
        waterThread = null;
    }

    public void start()
    {
        if(waterThread == null)
        {
            waterThread = new Thread(this);
            Thread.currentThread().setPriority(10);
            waterThread.start();
        }
    }

    public void run()
    {
        while(waterThread != null) 
        {
            try
            {
                Thread.sleep(frameDelay);
            }
            catch(InterruptedException _ex) { }
            repaint();
        }
    }


    public void update(Graphics g)
    {
        paint(g);
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -