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

📄 flashinggraphics1.java

📁 java applet编程,实现对相关图片声音的调用
💻 JAVA
字号:
import java.awt.*;
import java.applet.Applet;

/* 
 * This applet animates graphics that it generates.  This example
 * isn't a good one to copy -- it flashes.  The next couple of examples 
 * will show how to eliminate the flashing.
 */

public class FlashingGraphics1 extends Applet implements Runnable
{
    int frameNumber = -1;
    int delay;
    Thread animatorThread;
    boolean frozen = false;

    int squareSize = 20;
    boolean fillColumnTop = true;

    public void init()
	{
        String str;
        int fps = 10;

        //How many milliseconds between frames?
        str = getParameter("fps");
        try {
            if (str != null)
			{
                fps = Integer.parseInt(str);
            }
        } catch (Exception e) {}
        delay = (fps > 0) ? (1000 / fps) : 100;

        //How many pixels wide is each square?
        str = getParameter("squareWidth");
        try {
            if (str != null)
			{
                squareSize = Integer.parseInt(str);
            }
        } catch (Exception e) {}
    }

    public void start()
	{
        if (frozen) {
            //Do nothing.  The user has requested that we
            //stop changing the image.
        } else {
            //Start animating!
            if (animatorThread == null) {
                animatorThread = new Thread(this);
            }
            animatorThread.start();
        }
    }

    public void stop()
	{
        //Stop the animating thread.
        animatorThread = null;
    }

    public void run()
	{
        //Just to be nice, lower this thread's priority
        //so it can't interfere with other processing going on.
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
 
        //Remember the starting time.
        long startTime = System.currentTimeMillis();

        //This is the animation loop.
        while (Thread.currentThread() == animatorThread)
		{
            //Advance the animation frame.
            frameNumber++;

            //Display it.
            repaint();

            //Delay depending on how far we are behind.
            try {
                startTime += delay;
                Thread.sleep(Math.max(0, 
                                      startTime-System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    public void paint(Graphics g)
	{
		Dimension d = size();
		boolean fillSquare;
		boolean fillNextFrame;
		int rowWidth = 0;
		int x = 0, y = 0;
		int w, h;
		int tmp;

		//Set width of first "square". Decide whether to fill it.
		fillSquare = fillColumnTop;
		fillColumnTop = !fillColumnTop;
		tmp = frameNumber % squareSize;
		if (tmp == 0)
		{
			w = squareSize;
			fillNextFrame = !fillSquare;
		} else {
			w = tmp;
			fillNextFrame = fillSquare;
		}

		//Draw from left to right.
		while (x < d.width)
		{
			int colHeight = 0;

			//Draw the column.
			while (y < d.height)
			{
				colHeight += squareSize;

				//If we don't have room for a full square, cut if off.
				if (colHeight > d.height) {   //一列中最后一行
					h = d.height - y;
				} else {
					h = squareSize;
				}

				//Draw the rectangle if necessary.
				if (fillSquare) {
					g.fillRect(x, y, w, h);
					fillSquare = false;
				} else {
					fillSquare = true;
				} 

				y += h;
			} //while y

			//Determine x, y, and w for the next go around.
			x += w;
			y = 0;
			w = squareSize;
			rowWidth += w;
			if (rowWidth > d.width) {   //最后一列
				w = d.width - x;
			}
			fillSquare = fillColumnTop;
			fillColumnTop = !fillColumnTop;
		} //while x
		fillColumnTop = fillNextFrame;
	}
}

⌨️ 快捷键说明

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