0

尽管我导入了小程序库,但 NetBeans 无法在我的代码中识别和使用 getDocumentBase(),我的项目中有我的 html 文件以及所有其他必需的文件,这是我的代码的一部分:

import java.awt.*;
import java.applet.*;


public class AirportCanvas extends Canvas 
{
    SingleLaneAirport controller;

    Image  redPlane;
    Image  bluePlane;
    Image  airport;

    //AudioClip crashSound;

    int[] redX,redY,blueX,blueY;

    int maxCar = 2;

    final static int initredX  = 5;
    final static int initredY  = 55;
    final static int initblueX = 410;
    final static int initblueY = 130;
    final static int bridgeY   = 90;

    boolean frozen = false;
    int cycleTime = 20;

    AirportCanvas(SingleLaneAirport controller)
    {
        super();
        this.controller = controller;

//        crashSound=controller.getAudioClip(controller.getDocumentBase(),"crash.au");

        MediaTracker mt;
        mt = new MediaTracker(this);

        redPlane = controller.getImage(controller.getDocumentBase(), "redplane.png");
        mt.addImage(redPlane, 0);

        bluePlane = controller.getImage(controller.getDocumentBase(), "blueplane.png");
        mt.addImage(bluePlane, 1);

        airport = controller.getImage(controller.getDocumentBase(), "airport.png");
        mt.addImage(airport, 2);

        try
        {
            mt.waitForID(0);
            mt.waitForID(1);
            mt.waitForID(2);
        }
        catch (java.lang.InterruptedException e)
        {
            System.out.println("Couldn't load one of the images");
        }

        setSize(airport.getWidth(null),airport.getHeight(null));
        init(1);
    }

    public final void init(int ncars)
    { //set number of cars
        maxCar = ncars;
        frozen = false;
        redX  = new int[maxCar];
        redY  = new int[maxCar];
        blueX = new int[maxCar];
        blueY = new int[maxCar];

        for (int i = 0; i<maxCar ; i++)
        {
            redX[i] = initredX - i*85;
            redY[i] = initredY;
            blueX[i] =initblueX + i*85;
            blueY[i] =initblueY;
        }

        repaint();
    }

    Image offscreen;
    Dimension offscreensize;
    Graphics offgraphics;

    public void backdrop()
    {
        Dimension d = getSize();

        if ((offscreen == null) || (d.width != offscreensize.width)
                                 || (d.height != offscreensize.height))
        {
            offscreen = createImage(d.width, d.height);
            offscreensize = d;
            offgraphics = offscreen.getGraphics();
            offgraphics.setFont(new Font("Helvetica",Font.BOLD,36));
         }

        offgraphics.setColor(Color.lightGray);
        offgraphics.drawImage(airport,0,0,this);
    }

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

    @Override
    public void update(Graphics g)
    {
        backdrop();

        for (int i=0; i<maxCar; i++)
        {
            offgraphics.drawImage(redPlane,redX[i],redY[i],this);
            offgraphics.drawImage(bluePlane,blueX[i],blueY[i],this);
        }

        if (blueY[0]==redY[0] && Math.abs(redX[0]+80 - blueX[0])<5)
        {
            offgraphics.setColor(Color.red);
            offgraphics.drawString("Crunch!",200,100);
            frozen=true;
//            crashSound.play();
        }

        g.drawImage(offscreen, 0, 0, null);
    }

    //returns true for the period from just before until just after car on bridge
    public  boolean moveRed(int i) throws InterruptedException
    {
        int X = redX[i];
        int Y = redY[i];

        synchronized (this)
        {
            while (frozen )
               wait();

            if (i==0 || Math.abs(redX[i-1] - X) > 120)
            {
                X += 2;

                if (X >=500) 
                {
                   X = -80; Y = initredY;
                }

                if (X >=60 && X < 290 && Y<bridgeY) 
                   ++Y;

                if (X >=290 && Y>initredY)
                   --Y;
            }

            redX[i]=X;
            redY[i]=Y;
            repaint();
        }

        Thread.sleep(cycleTime);

        return (X>25 && X<400);
     }

    //returns true for the period from just before until just after car on bridge
    public  boolean moveBlue(int i) throws InterruptedException
    {
        int X = blueX[i];
        int Y = blueY[i];

        synchronized (this)
        {
            while (frozen )
               wait();

            if (i==0 || Math.abs(blueX[i-1] - X) > 120)
            {
                X -= 2;

                if (X <=-80)
                {
                   X = 500; Y = initblueY;
                }

                if (X <=370 && X > 130 && Y>bridgeY)
                   --Y;

                if (X <=130 && Y<initblueY)
                   ++Y;

                blueX[i]=X;
            }

            blueY[i]=Y;
            repaint();
        }

        Thread.sleep(cycleTime);
        repaint();

        return (X>25 && X<400);
    }

    public synchronized void freeze()
    {
        frozen = true;
    }

    public synchronized void thaw()
    {
        frozen = false;
        notifyAll();
    }
}

单车道机场等级:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class SingleLaneAirport {
    AirportCanvas display;
    Button restart;
    Button freeze;
    Button onecar;
    Button twocar;
    Button threecar;
    Checkbox fair;
    Checkbox safe;
    boolean fixed = false;
    int maxCar = 1;

    Thread red[];
    Thread blue[];

    @Override
    public void init()
    {
        setLayout(new BorderLayout());
        display = new AirportCanvas(this);
        add("Center",display);
        restart = new Button("Restart");

        restart.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
             display.thaw();
           }
        });

        freeze = new Button("Freeze");

        freeze.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
             display.freeze();
           }
        });

        onecar = new Button("One Car");

        onecar.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
             stop();
             maxCar = 1;
             start();
           }
        });

        twocar = new Button("Two Cars");

        twocar.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              stop();
              maxCar = 2;
              start();
            }
         });

        threecar = new Button("Three Cars");

        threecar.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              stop();
              maxCar = 3;
              start();
            }
         });

        safe = new Checkbox("Safe",null,true);
        safe.setBackground(Color.lightGray);

        safe.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
              stop();
              start();
            }
         });

        fair = new Checkbox("Fair",null,false);
          fair.setBackground(Color.lightGray);

        fair.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
              stop();
              start();
            }
         });

        Panel p1 = new Panel();
        p1.setLayout(new FlowLayout());
        p1.add(freeze);
        p1.add(restart);
        p1.add(onecar);
        p1.add(twocar);
        p1.add(threecar);
        p1.add(safe);
        p1.add(fair);
        add("South",p1);
        setBackground(Color.lightGray);
   }

    @Override
    public void start()
    {
        red = new Thread[maxCar];
        blue = new Thread[maxCar];
        display.init(maxCar);
        Airport b;

        if (fair.getState() && safe.getState())
            b = new FairAirport();
        else if ( safe.getState())
            b = new SafeAirport();
        else
            b = new Airport();

        for (int i = 0; i<maxCar; i++)
        {
            red[i] = new Thread(new RedPlane(b,display,i));
            blue[i] = new Thread(new BluePlane(b,display,i));
        }

        for (int i = 0; i<maxCar; i++)
        {
            red[i].start();
            blue[i].start();
        }
    }

    @Override
    public void stop()
    {
        for (int i = 0; i<maxCar; i++)
        {
            red[i].interrupt();
            blue[i].interrupt();
        }
    }
}
4

1 回答 1

2

您的类SingleLaneAirport应该扩展Applet类以使用getDocumentBase()功能。

因为getDocumentBase()是类的功能Applet而不是Object

于 2013-05-12T09:12:32.813 回答