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

📄 sharkattack.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import javax.swing.*;
import java.awt.event.*;
import java.applet.AudioClip;
import java.awt.MediaTracker;
import java.awt.Image;
import java.awt.Graphics;

public class SharkAttack
        extends JApplet
        implements Runnable
{
    private Shark shark = null;
    private SharkPrey prey[] = null;
    private SharkControls guide = null;
    private SwanCounter count = null;
    private class Arena
            extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(river, 0, 0,
                        getSize().width, getSize().height, this);
            shark.paintComponent(g);
            for (int i = 0; i < prey.length; i++)
            {
                if (prey[i] != null)
                {
                    prey[i].paintComponent(g);
                }
            }
        }
    }

    private class Clicker
            extends MouseAdapter
    {
        public void mouseClicked(MouseEvent me)
        {
            guide.setVisible(true);
            guide.toFront();
        }
    }

    private Image river = null, aSwan = null;
    private AudioClip music = null, splash = null;
    private Thread thread = null;
    private Arena arena = new Arena();

    public void init()
    {
        river = getImage(getDocumentBase(), "Images/river.jpg");
        aSwan = getImage(getDocumentBase(), "Images/swan.gif");
        music = getAudioClip(getDocumentBase(), "Audio/jaws.wav");
        splash = getAudioClip(getDocumentBase(), "Audio/splash.wav");
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(river, 0);
        tracker.addImage(aSwan, 1);
        try
        {
            tracker.waitForAll();
        }
        catch (InterruptedException ie)
        {
            JOptionPane.showMessageDialog(this, "Error loading images");
        }
        shark = new Shark(this);
        prey = new SharkPrey[10];
        count = new SwanCounter(new ImageIcon(aSwan), prey.length);
        getContentPane().add("Center",
                             new JPanelBox(arena, "Shark Attack"));
        getContentPane().add("South", count);
        guide = new SharkControls(this, shark);
        arena.setToolTipText("Click to show controls");
        arena.addMouseListener(new Clicker());
    }

    public void start()
    {
        thread = new Thread(this);
        count.reset();
        thread.start();
        music.loop();
        for (int i = 0; i < prey.length; i++)
        {
            prey[i] = new SharkPrey(aSwan, this);
        }
    }

    public void stop()
    {
        music.stop();
        count.stop();
        thread = null;
    }

    public void run()
    {
        Thread currentThread = Thread.currentThread();
        while (thread == currentThread)
        {
            shark.move();
            for (int i = 0; i < prey.length; i++)
            {
                if (prey[i] != null)
                {
                    prey[i].move();
                }
            }
            try
            {
                thread.sleep(200);
            }
            catch (InterruptedException ie)
            {
                System.err.println("Error: " + ie);
            }
            for (int i = 0; i < prey.length; i++)
            {
                if ( (prey[i] != null) && (prey[i].isEatenBy(shark)))
                {
                    splash.play();
                    prey[i] = null;
                    count.remove();
                    if (count.getCount() == 0)
                    {
                        stop();
                    }
                }
            }
            arena.repaint();
        }
    }
}

⌨️ 快捷键说明

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