advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Double


/*

        ##               ##     ##
        ##               ##     ##
     #####  ####  ##  ## #####  ##   ####
    ##  ## ##  ## ##  ## ##  ## ##  ##  ##
    ##  ## ##  ## ##  ## ##  ## ##  ######
    ##  ## ##  ## ##  ## ##  ## ##  ##
     #####  ####   ####  #####   ##  ####

  written by martin koerwien & michael kraus

                        1992-1998

*/

// Please send bug reports, comments and suggestions to:
// michael.kraus@informatik.uni-muenchen.de
// Or visit the homepage of double:
// www.informatik.uni-muenchen.de/~_michael.kraus/double.html

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

// ------------------------ Spiel ------------------------

class SpielCanvas extends Canvas
{
  // ------------------------ Konstanten ------------------------

        int feldbreite=20;
        int feldhoehe=10;

        int ikonenbreite;
        int ikonenhoehe;

        int balkenbreite;
        int balkenhoehe=16;

        int unbalkenbreite;

        int balkenx;
        int balkeny;

        int punktex;
        int punktey;

        int unpunktex;
        int unpunktey;

        int unpunktebreite;
        int unpunktehoehe;

  int loeschpunkteabstand=100;

  int startzeit=4000;
  int wartezeit=100;
  int dropzeit=startzeit-200;
  int nachspielzeit=2000;
  int istschleifezeit=25;
  int loeschzeit=100;
  int abbruchzeit=-1;

  int lr=0;  // leer
  int lu=1;  // links unten
  int lo=2;  // links oben
  int ro=3;  // rechts oben
  int ru=4;  // rechts unten
  int wg=5;  // waagrecht
  int sk=6;  // senkrecht

  int hell=6;  // Offset zu hellen Ikonen

  int oben=0;
  int unten=1;
  int links=2;
  int rechts=3;

  int ikonenausgang[/*ikone-lu*/][/*eingang/ausgang*/]=
  {
    {oben,rechts},
    {unten,rechts},
    {links,unten},
    {links,oben},
    {links,rechts},
    {oben,unten}
  };

  int eingang=0;
  int ausgang=1;

  int delta[/*richtung*/][/*deltax/deltay*/]=
  {
    {0,-1},
    {0,1},
    {-1,0},
    {1,0}
  };

  int deltax=0;
  int deltay=1;

  int anderes[/*richtung*/]=
  {
    unten,oben,rechts,links
  };

  int groesze[/*nummer*/][/*direktion*/][/*breite/hoehe*/]=
  {
    {{1,1},{1,1},{1,1},{1,1}},
    {{1,1},{1,1}},
    {{1,2},{2,1}},
    {{2,1},{1,2}},
    {{2,1},{1,2}},
    {{2,1},{1,2},{2,1},{1,2}},
    {{2,1},{1,2},{2,1},{1,2}},
    {{2,1},{1,2},{2,1},{1,2}},
    {{2,2}},
    {{2,2},{2,2},{2,2},{2,2}},
    {{3,3},{3,3},{3,3},{3,3}},
    {{2,2},{2,2},{2,2},{2,2}}
  };

  int breite=0;
  int hoehe=1;

  int teil[/*nummer*/][/*direktion*/][/*x*/][/*y*/]=
  {
    {
      {{lo,lr,lr},{lr,lr,lr},{lr,lr,lr}},  // Kleine Ecke
      {{ro,lr,lr},{lr,lr,lr},{lr,lr,lr}},
      {{ru,lr,lr},{lr,lr,lr},{lr,lr,lr}},
      {{lu,lr,lr},{lr,lr,lr},{lr,lr,lr}}
    },
    {
      {{sk,lr,lr},{lr,lr,lr},{lr,lr,lr}},  // Kurze Gerade
      {{wg,lr,lr},{lr,lr,lr},{lr,lr,lr}}
    },
    {
      {{sk,sk,lr},{lr,lr,lr},{lr,lr,lr}},  // Lange Gerade
      {{wg,lr,lr},{wg,lr,lr},{lr,lr,lr}}
    },
    {
      {{lu,lr,lr},{ro,lr,lr},{lr,lr,lr}},  // S-Teilchen 1
      {{lo,ru,lr},{lr,lr,lr},{lr,lr,lr}}
    },
    {
      {{lo,lr,lr},{ru,lr,lr},{lr,lr,lr}},  // S-Teilchen 2
      {{ro,lu,lr},{lr,lr,lr},{lr,lr,lr}}
    },
    {
      {{lo,lr,lr},{ro,lr,lr},{lr,lr,lr}},  // Umdrehteilchen
      {{ro,ru,lr},{lr,lr,lr},{lr,lr,lr}},
      {{lu,lr,lr},{ru,lr,lr},{lr,lr,lr}},
      {{lo,lu,lr},{lr,lr,lr},{lr,lr,lr}}
    },
    {
      {{lo,lr,lr},{wg,lr,lr},{lr,lr,lr}},  // Noname-Teilchen 1
      {{ro,sk,lr},{lr,lr,lr},{lr,lr,lr}},
      {{wg,lr,lr},{ru,lr,lr},{lr,lr,lr}},
      {{sk,lu,lr},{lr,lr,lr},{lr,lr,lr}}
    },
    {
      {{lu,lr,lr},{wg,lr,lr},{lr,lr,lr}},  // Noname-Teilchen 2
      {{lo,sk,lr},{lr,lr,lr},{lr,lr,lr}},
      {{wg,lr,lr},{ro,lr,lr},{lr,lr,lr}},
      {{sk,ru,lr},{lr,lr,lr},{lr,lr,lr}}
    },
    {
      {{lo,lu,lr},{ro,ru,lr},{lr,lr,lr}}  // Honeynut Loop
    },
    {
      {{lo,ru,lr},{ro,lu,lr},{lr,lr,lr}},  // Wulst
      {{lu,lo,lr},{ro,ru,lr},{lr,lr,lr}},
      {{ro,lu,lr},{lo,ru,lr},{lr,lr,lr}},
      {{lo,lu,lr},{ru,ro,lr},{lr,lr,lr}}
    },
    {
      {{lo,sk,lu},{wg,lr,lr},{ro,lr,lr}},  // Graffl
      {{lo,lr,lr},{wg,lr,lr},{ro,sk,ru}},
      {{lr,lr,lu},{lr,lr,wg},{ro,sk,ru}},
      {{lo,sk,lu},{lr,lr,wg},{lr,lr,ru}}
    },
    {
      {{lo,sk,lr},{wg,lr,lr},{lr,lr,lr}},  // Große Ecke
      {{wg,lr,lr},{ro,sk,lr},{lr,lr,lr}},
      {{lr,wg,lr},{sk,ru,lr},{lr,lr,lr}},
      {{sk,lu,lr},{lr,wg,lr},{lr,lr,lr}}
    }
  };

  // ------------------------ Variable ------------------------

  int feld[][]=new int[feldbreite][feldhoehe];

  int teilx,teily,teild,teiln;
  int unteilx,unteily,unteild,unteiln;

  int zeit;
        int punkte;
  int teilchen;

  int zuletztloeschpunkte;

  double_ applet;
  boolean catched;

  // ------------------------ Konstruktor ------------------------

  SpielCanvas(double_ applet)
  {
    this.applet=applet;

    punkte=applet.ohnepunkte;  // Initialisierung für TitelCanvas.HighscoreSchreiben()

                ikonenbreite=applet.bild[lr].getWidth(null);
                ikonenhoehe=applet.bild[lr].getHeight(null);

                applet.appletbreite=feldbreite*ikonenbreite;
                applet.applethoehe=feldhoehe*ikonenhoehe+balkenhoehe+applet.doublebildhoehe+applet.doublebildvspace*2;

                balkenx=0;
                balkeny=feldhoehe*ikonenhoehe;

                balkenbreite=applet.appletbreite;
                unbalkenbreite=balkenbreite*wartezeit/startzeit;

                applet.doublebildx=(applet.appletbreite-applet.doublebildbreite)/2;
                applet.doublebildmaxy=applet.applethoehe-applet.doublebildvspace-applet.doublebildhoehe;

                unpunktex=applet.doublebildx+140;
                unpunktey=applet.doublebildmaxy+15;

                unpunktebreite=applet.fontmetrics.stringWidth(applet.maxpunkte+applet.unloeschbarstring);
                unpunktehoehe=applet.fonthoehe;

                punktex=unpunktex;
                punktey=unpunktey+applet.fontaufstieg;

    setFont(applet.pearlfont);
    setForeground(applet.gelb);
    setBackground(applet.schwarz);
  }

  // ------------------------ Methoden ------------------------

  public void paint(Graphics graphics)
  {
    FeldZeichnen(graphics);
    TeilZeichnen(graphics);
    BalkenZeichnen(graphics);
                applet.DoublebildZeichnen(graphics,applet.doublebildmaxy);
    PunkteZeichnen(graphics);
  }

  void FeldZeichnen(Graphics graphics)
  {
    int x,y;

    for(y=0;y<feldhoehe;y++)
      for(x=0;x<feldbreite;x++)
        graphics.drawImage(applet.bild[feld[x][y]],x*ikonenbreite,y*ikonenhoehe,null);
  }

  void TeilZeichnen(Graphics graphics)
  {
    int x,y;
    int ikone;

    for(y=0;y<groesze[teiln][teild][hoehe];y++)
      for(x=0;x<groesze[teiln][teild][breite];x++)
        if((ikone=teil[teiln][teild][x][y])!=lr)
          graphics.drawImage(applet.bild[ikone+hell],(teilx+x)*ikonenbreite,(teily+y)*ikonenhoehe,null);
  }

  void TeilUnzeichnen(Graphics graphics)
  {
    int x,y;

    for(y=0;y<groesze[unteiln][unteild][hoehe];y++)
      for(x=0;x<groesze[unteiln][unteild][breite];x++)
        if(teil[unteiln][unteild][x][y]!=lr)
          graphics.drawImage(applet.bild[feld[unteilx+x][unteily+y]],(unteilx+x)*ikonenbreite,(unteily+y)*ikonenhoehe,null);
  }

  void IkoneUnzeichnen(Graphics graphics)
  {
    graphics.drawImage(applet.bild[lr],unteilx*ikonenbreite,unteily*ikonenhoehe,null);
  }

  void BalkenZeichnen(Graphics graphics)
  {
    graphics.fillRect(balkenx,balkeny,balkenbreite*zeit/startzeit,balkenhoehe);
  }

  void BalkenUnzeichnen(Graphics graphics)
  {
    graphics.clearRect(balkenx+balkenbreite*zeit/startzeit,balkeny,unbalkenbreite,balkenhoehe);
  }

  void PunkteZeichnen(Graphics graphics)
  {
    if(punkte>=zuletztloeschpunkte+loeschpunkteabstand)
      graphics.drawString(punkte+applet.punktestring,punktex,punktey);
    else
      graphics.drawString(punkte+applet.unloeschbarstring,punktex,punktey);
  }

  void PunkteUnzeichnen(Graphics graphics)
  {
    graphics.clearRect(unpunktex,unpunktey,unpunktebreite,unpunktehoehe);
  }

  void NeuesFeld()
  {
    int x,y;

    for(y=0;y<feldhoehe;y++)
      for(x=0;x<feldbreite;x++)
        feld[x][y]=lr;

    teilchen=0;
  }

  void NeuesTeil()
  {
    teiln=applet.Zufall(teil.length);
    teild=applet.Zufall(teil[teiln].length);
    teilx=applet.Zufall(feldbreite-groesze[teiln][teild][breite]+1);
    teily=applet.Zufall(feldhoehe-groesze[teiln][teild][hoehe]+1);

    zeit=startzeit;
  }

  void BewegeTeil(int x, int y)
  {
    unteiln=teiln;
    unteild=teild;
    unteilx=teilx;
    unteily=teily;

    if(x<=0)
      teilx=0;
    else if(x+groesze[teiln][teild][breite]>=feldbreite)
      teilx=feldbreite-groesze[teiln][teild][breite];
    else
      teilx=x;

    if(y<=0)
      teily=0;
    else if(y+groesze[teiln][teild][hoehe]>=feldhoehe)
      teily=feldhoehe-groesze[teiln][teild][hoehe];
    else
      teily=y;

    if(teilx!=unteilx||teily!=unteily)
    {
      TeilUnzeichnen(getGraphics());
      TeilZeichnen(getGraphics());
    }
  }

  void DrehTeil()
  {
    unteiln=teiln;
    unteild=teild;
    unteilx=teilx;
    unteily=teily;

    if((teild=++teild%teil[teiln].length)!=unteild)
    {
      if(teilx+groesze[teiln][teild][breite]>=feldbreite)
        teilx=feldbreite-groesze[teiln][teild][breite];

      if(teily+groesze[teiln][teild][hoehe]>=feldhoehe)
        teily=feldhoehe-groesze[teiln][teild][hoehe];

      TeilUnzeichnen(getGraphics());
      TeilZeichnen(getGraphics());
    }
  }

  boolean GehtEs()
  {
    int x,y;

    for(y=0;y<groesze[teiln][teild][hoehe];y++)
      for(x=0;x<groesze[teiln][teild][breite];x++)
        if(teil[teiln][teild][x][y]!=lr)
          if(feld[teilx+x][teily+y]!=lr)
            return false;

    return true;
  }

  void IstSchleife()
  {
    int x,y;
    int ikone;
    int startx,starty;
    int richtung;

    schleife:
    for(ikone=x=y=0;y<groesze[unteiln][unteild][hoehe];y++)
      for(x=0;x<groesze[unteiln][unteild][breite];x++)
        if((ikone=teil[unteiln][unteild][x][y])!=lr)
          break schleife;

    startx=(x+=unteilx);
    starty=(y+=unteily);
    richtung=ikonenausgang[ikone-lu][eingang];

    do
    {
      x+=delta[richtung][deltax];
      y+=delta[richtung][deltay];

      if(x>=0&&x<feldbreite&&y>=0&&y<feldhoehe&&(ikone=feld[x][y])!=lr)
      {
        if(anderes[ikonenausgang[ikone-lu][eingang]]==richtung)
          richtung=ikonenausgang[ikone-lu][ausgang];
        else if(anderes[ikonenausgang[ikone-lu][ausgang]]==richtung)
          richtung=ikonenausgang[ikone-lu][eingang];
        else
          return;
      }
      else
        return;
    }
    while(x!=startx||y!=starty);

    do
    {
      x+=delta[richtung][deltax];
      y+=delta[richtung][deltay];

      ikone=feld[x][y];
      feld[x][y]=lr;

                        if(++punkte>applet.maxpunkte)
                                punkte=applet.maxpunkte;

      teilchen--;

      PunkteUnzeichnen(getGraphics());
      PunkteZeichnen(getGraphics());

      unteilx=x;
      unteily=y;

      IkoneUnzeichnen(getGraphics());

      try
      {
        synchronized(applet.object)
        {
          applet.object.wait(istschleifezeit);
        }
      }
      catch(InterruptedException exception)
      {
      }

      if(anderes[ikonenausgang[ikone-lu][eingang]]==richtung)
        richtung=ikonenausgang[ikone-lu][ausgang];
      else if(anderes[ikonenausgang[ikone-lu][ausgang]]==richtung)
        richtung=ikonenausgang[ikone-lu][eingang];
    }
    while(x!=startx||y!=starty);

    if(teilchen==0)
    {
                        if((punkte*=2)>applet.maxpunkte)
                                punkte=applet.maxpunkte;

      PunkteUnzeichnen(getGraphics());
      PunkteZeichnen(getGraphics());
    }
  }

  void SetzTeil()
  {
    int x,y;
    int ikone;

    applet.thread.suspend();

    if(GehtEs())
    {
      for(y=0;y<groesze[teiln][teild][hoehe];y++)
        for(x=0;x<groesze[teiln][teild][breite];x++)
          if((ikone=teil[teiln][teild][x][y])!=lr)
          {
            feld[teilx+x][teily+y]=ikone;
            teilchen++;
          }

      unteiln=teiln;
      unteild=teild;
      unteilx=teilx;
      unteily=teily;

      TeilUnzeichnen(getGraphics());

      IstSchleife();
      NeuesTeil();

      TeilZeichnen(getGraphics());
      BalkenZeichnen(getGraphics());

      synchronized(applet.object)
      {
        catched=true;
        applet.object.notifyAll();
      }
    }

    applet.thread.resume();
  }

  boolean GehtLoeschen()
  {
    int x,y,n=0;

    if(punkte<zuletztloeschpunkte+loeschpunkteabstand)
      return false;

    for(y=0;y<groesze[teiln][teild][hoehe];y++)
      for(x=0;x<groesze[teiln][teild][breite];x++)
        if(teil[teiln][teild][x][y]!=feld[teilx+x][teily+y])
          return false;

    zuletztloeschpunkte=punkte;

    return true;
  }

  void LoeschTeil()
  {
    int x,y;

    applet.thread.suspend();

    if(GehtLoeschen())
    {
      PunkteUnzeichnen(getGraphics());
      PunkteZeichnen(getGraphics());

      for(y=0;y<groesze[teiln][teild][hoehe];y++)
        for(x=0;x<groesze[teiln][teild][breite];x++)
          if(teil[teiln][teild][x][y]!=lr)
          {
            feld[teilx+x][teily+y]=lr;

            teilchen--;

            unteilx=teilx+x;
            unteily=teily+y;

            IkoneUnzeichnen(getGraphics());

            try
            {
              synchronized(applet.object)
              {
                applet.object.wait(loeschzeit);
              }
            }
            catch(InterruptedException exception)
            {
            }
          }

      NeuesTeil();

      TeilZeichnen(getGraphics());
      BalkenZeichnen(getGraphics());

      synchronized(applet.object)
      {
        catched=true;
        applet.object.notifyAll();
      }
    }

    applet.thread.resume();
  }

  void AutoDrop()
  {
    int x,y;
    int ikone;

    if(GehtEs())
    {
      PunkteUnzeichnen(getGraphics());
      PunkteZeichnen(getGraphics());

      for(y=0;y<groesze[teiln][teild][hoehe];y++)
        for(x=0;x<groesze[teiln][teild][breite];x++)
          if((ikone=teil[teiln][teild][x][y])!=lr)
          {
            feld[teilx+x][teily+y]=ikone;
            teilchen++;
          }

      unteiln=teiln;
      unteild=teild;
      unteilx=teilx;
      unteily=teily;

      TeilUnzeichnen(getGraphics());

      IstSchleife();
      NeuesTeil();

      TeilZeichnen(getGraphics());
      BalkenZeichnen(getGraphics());
    }
  }

  void Spiel()
  {
    punkte=0;
    zuletztloeschpunkte=-loeschpunkteabstand;

    NeuesFeld();
    NeuesTeil();

    applet.cardlayout.show(applet,"Spiel");
    requestFocus();

    while(zeit>0)
    {
      while(zeit>0)
      {
        catched=false;

        try
        {
          synchronized(applet.object)
          {
            applet.object.wait(wartezeit);
          }
        }
        catch(InterruptedException exception)
        {
        }

        if(catched)
          continue;

        zeit-=wartezeit;

        BalkenUnzeichnen(getGraphics());
      }

      try
      {
        synchronized(applet.object)
        {
          applet.object.wait(wartezeit);
        }
      }
      catch(InterruptedException exception)
      {
      }

      if(zeit==0)
        AutoDrop();
    }

    try
    {
      synchronized(applet.object)
      {
        applet.object.wait(nachspielzeit);
      }
    }
    catch(InterruptedException exception)
    {
    }
  }

  public boolean mouseMove(Event event,int x,int y)
  {
    if(zeit>0)
                        BewegeTeil(x/ikonenbreite,y/ikonenhoehe);

    return true;
  }

  public boolean mouseDown(Event event,int x,int y)
  {
    if(zeit>0)
    {
      if(event.modifiers==Event.META_MASK)
        DrehTeil();

      if(zeit<=dropzeit)
      {
        if(event.modifiers==0)
          SetzTeil();
        else if(event.modifiers==Event.ALT_MASK)
          LoeschTeil();
      }
    }

    return true;
  }

  public boolean keyDown(Event event,int key)
  {
    if(zeit>0)
    {
      if(key=='\n')
        DrehTeil();
      else if(key==Event.LEFT)
        BewegeTeil(teilx-1,teily);
      else if(key==Event.RIGHT)
        BewegeTeil(teilx+1,teily);
      else if(key==Event.UP)
        BewegeTeil(teilx,teily-1);
      else if(key==Event.DOWN)
        BewegeTeil(teilx,teily+1);
      else if(key=='a')
        zeit=abbruchzeit;

      if(zeit<=dropzeit)
      {
        if(key==' ')
          SetzTeil();
        else if(key=='e')
          LoeschTeil();
      }
    }

    return true;
  }
}

// ------------------------ Titel ------------------------

class TitelCanvas extends Canvas
{
  // ------------------------ Konstanten ------------------------

        int highscoreyohnepunkte;
        int highscoreymitpunkte;

  int highscoreseite=10;

        int titelzeit=30000;

  String titeltext[][]=
  {
    {
      "Controls",
      "",
      "Abort: timeout or 'a' key",
      "Move: mouse or cursor keys",
      "Erase: middle mouse button or 'e' key",
      "Rotate: right mouse button or return key",
      "Drop: left mouse button or space key or automatic",
      "",
      "Highscores screens: '1', '2', ... '0' keys",
      "Instructions screen: 'i' key",
      "Controls screen: 'c' key",
      "",
      "Press left mouse button or any other key to play"
    },
    {
      "Instructions",
      "",
      "You have to drop the blue piece within the time",
      "indicated by the yellow bar, i.e. four seconds.",
      "After that time, the game tries to drop the piece",
      "automatically. If you build a closed loop it will",
      "dissolve into score, one point for each tile of",
      "the loop. If you clear the whole field, your points",
      "will be doubled. Erasing can only be done if the",
      "blue piece coincides with the underlying tiles and",
      "only if you scored at least 100 points since the",
      "last erasing was done, which is indicated by an 'X'."
    },
    {
      "Highscores",
      "Your score: "
    }
  };

  int spielstart=-2;
  int automatik=-1;

  int controlstitel=0;
  int instructionstitel=1;
  int highscoretitel=2;

  int highscoretext=0;
  int yourscoretext=1;

  // ------------------------ Variable ------------------------

  int titel;
  int highscoreindex;

  double_ applet;
  int catched;

  // ------------------------ Konstruktor ------------------------

  TitelCanvas(double_ applet)
  {
    this.applet=applet;

                applet.autorenbildx=(applet.appletbreite-applet.autorenbildbreite)/2;
                applet.autorenbildy=applet.applethoehe-applet.autorenbildvspace-applet.autorenbildhoehe;

                applet.texty=applet.doublebildminy+applet.doublebildhoehe+applet.doublebildvspace+applet.fontaufstieg;
                applet.texthoehe=applet.autorenbildy-applet.autorenbildvspace-applet.fontmetrics.getDescent()-applet.texty;

                highscoreymitpunkte=applet.texty+(applet.texthoehe-(4+highscoreseite)*applet.fonthoehe)/2;
                highscoreyohnepunkte=applet.texty+(applet.texthoehe-(2+highscoreseite)*applet.fonthoehe)/2;

    titel=controlstitel;
    highscoreindex=applet.highscores-highscoreseite;

    setFont(applet.pearlfont);
    setForeground(applet.gelb);
    setBackground(applet.schwarz);
  }

  // ------------------------ Methoden ------------------------

  public void paint(Graphics graphics)
  {
                applet.DoublebildZeichnen(graphics,applet.doublebildminy);

    if(titel==highscoretitel)
      HighscoreSchreiben(graphics);
    else
      TextSchreiben(graphics);

    applet.AutorenbildZeichnen(graphics);
  }

  public void update(Graphics graphics)
  {
    TextUnschreiben(graphics);

    if(titel==highscoretitel)
    {
      highscoreindex=(highscoreindex+highscoreseite)%applet.highscores;
      HighscoreSchreiben(graphics);
    }
    else
                {
      TextSchreiben(graphics);
                }
  }

  void HighscoreSchreiben(Graphics graphics)
  {
                int y;
    int n;

    if(applet.spielcanvas.punkte==applet.ohnepunkte)
      y=highscoreyohnepunkte;
    else
      y=highscoreymitpunkte;

                applet.MittigSchreiben(graphics,titeltext[highscoretitel][highscoretext],y);
                y+=2*applet.fonthoehe;

    for(n=highscoreindex;n<highscoreindex+highscoreseite;n++)
    {
                        applet.MittigSchreiben(graphics,applet.highscorepanel.HighscoreString(n),y);
                        y+=applet.fonthoehe;
    }

    if(applet.spielcanvas.punkte!=applet.ohnepunkte)
                        applet.MittigSchreiben(graphics,titeltext[highscoretitel][yourscoretext]+applet.spielcanvas.punkte+applet.punktestring,y+applet.fonthoehe);
  }

  void TextSchreiben(Graphics graphics)
  {
                int y;
    int n;

                y=applet.texty+(applet.texthoehe-titeltext[titel].length*applet.fonthoehe)/2;

    for(n=0;n<titeltext[titel].length;n++)
    {
                        applet.MittigSchreiben(graphics,titeltext[titel][n],y);
                        y+=applet.fonthoehe;
    }
  }

  void TextUnschreiben(Graphics graphics)
  {
    graphics.clearRect(0,applet.texty,applet.appletbreite,applet.texthoehe);
  }

  public void Titel()
  {
    applet.cardlayout.show(applet,"Titel");
    requestFocus();

    for(;;)
    {
      catched=automatik;

      try
      {
        synchronized(applet.object)
        {
          applet.object.wait(titelzeit);
        }
      }
      catch(InterruptedException exception)
      {
      }

      if(catched==spielstart)
        break;
      else if(catched==automatik)
                          titel=++titel%titeltext.length;
      else
        titel=catched;

      repaint();
    }

    titel=highscoretitel;
  }

  public boolean keyDown(Event event,int key)
  {
    if(key=='1')
    {
      catched=highscoretitel;
      highscoreindex=applet.highscores-highscoreseite;
    }
    else if(key=='2')
    {
      catched=highscoretitel;
      highscoreindex=0;
    }
    else if(key=='3')
    {
      catched=highscoretitel;
      highscoreindex=highscoreseite;
    }
    else if(key=='4')
    {
      catched=highscoretitel;
      highscoreindex=2*highscoreseite;
    }
    else if(key=='5')
    {
      catched=highscoretitel;
      highscoreindex=3*highscoreseite;
    }
    else if(key=='6')
    {
      catched=highscoretitel;
      highscoreindex=4*highscoreseite;
    }
    else if(key=='7')
    {
      catched=highscoretitel;
      highscoreindex=5*highscoreseite;
    }
    else if(key=='8')
    {
      catched=highscoretitel;
      highscoreindex=6*highscoreseite;
    }
    else if(key=='9')
    {
      catched=highscoretitel;
      highscoreindex=7*highscoreseite;
    }
    else if(key=='0')
    {
      catched=highscoretitel;
      highscoreindex=8*highscoreseite;
    }
    else if(key=='c')
      catched=controlstitel;
    else if(key=='i')
      catched=instructionstitel;
    else
      catched=spielstart;

    synchronized(applet.object)
    {
      applet.object.notifyAll();
    }

    return true;
  }

  public boolean mouseDown(Event event,int x,int y)
  {
    if(event.modifiers==0)
      catched=spielstart;

    synchronized(applet.object)
    {
      applet.object.notifyAll();
    }

    return true;
  }
}

// ------------------------ Highscores ------------------------

class HighscorePanel extends Panel
{
        // ------------------------ Konstanten ------------------------

        int textfieldbreite;
        int textfieldhoehe;

        int textfieldx;
        int textfieldy;

        int welldoney;

        int eingabezeit=60000;

  String lesedatei="http://www.informatik.uni-muenchen.de/~michael.kraus/double/highscores";
  String schreibdatei="http://cgi.cip.informatik.uni-muenchen.de/cgi-bin/user/krausm/double.cgi";

        String eingabetext[]=
        {
                "Well done, hero!",
                "",
                "Please enter your name:",
                "",
                "",
                "",
                "Score: ",
                "Rank: "
        };

        int eingabezeile=4;
        int punktezeile=6;
        int rangzeile=7;

        String silben[]=
        {
                "MAR","TIN","KAR","STEN","NOR","KURT","UR",
                "BERT","DAM","MAN","FRED","HEL","MUT","TON",
                "DOLF","WOLF","GANG","SEF","SA","BI","NE",
                "OLLI","AL","AN","EDE","ANNE","OL","DON",
                "ORG","SU","LA","OT","TO","RU","MICH","AEL",
                "SUF","FI","SIEG","MUND","FRIED","FONS","DRE",
                "AS","HI","AR","NOLD","TUR","EL","AU","KO",
                "GUST","WUF","LIN","DE","RO","LO"
        } ;

        // ------------------------ Variable ------------------------

        int index;
  boolean scoresok;

        TextField textfield;

  double_ applet;

  // ------------------------ Konstruktor ------------------------

        HighscorePanel(double_ applet)
        {
    this.applet=applet;

    textfield=new TextField(applet.highscorenamelaenge);

                try
                {
                        HighscoresLesen();
      scoresok=true;
                }
                catch(Exception exception)
                {
/**/      System.out.println("Error while loading highscores: "+exception.toString());

                        NeueHighscores();
      scoresok=false;
                }

                welldoney=applet.texty+(applet.texthoehe-eingabetext.length*applet.fonthoehe)/2;

                setLayout(null);
    setFont(applet.pearlfont);
    setForeground(applet.gelb);
    setBackground(applet.schwarz);

                add(textfield);
  }

  // ------------------------ Methoden ------------------------

  public void paint(Graphics graphics)
  {
                applet.DoublebildZeichnen(graphics,applet.doublebildminy);
                EingabeSchreiben(graphics);
                applet.AutorenbildZeichnen(graphics);
  }

        void EingabeSchreiben(Graphics graphics)
        {
                int y;
                int n;

                y=welldoney;

                for(n=0;n<eingabetext.length;n++)
    {
                        if(n==punktezeile)
                                applet.MittigSchreiben(graphics,eingabetext[n]+String.valueOf(applet.spielcanvas.punkte)+applet.punktestring,y);
                        else if(n==rangzeile)
                                applet.MittigSchreiben(graphics,eingabetext[n]+String.valueOf(index+1)+".",y);
                        else
                                applet.MittigSchreiben(graphics,eingabetext[n],y);

                        y+=applet.fonthoehe;
    }
        }

        void HighscoresLesen() throws MalformedURLException,IOException
        {
                int n;
                DataInputStream datainputstream=new DataInputStream(new URL(lesedatei).openConnection().getInputStream());

                for(n=0;n<applet.highscores;n++)
                {
                        applet.highscorename[n]=datainputstream.readUTF();
                        applet.highscorepunkte[n]=datainputstream.readInt();
                }

    datainputstream.close();
        }

        void HighscoresSchreiben() throws MalformedURLException,IOException
        {
                int n;
                DataOutputStream dataoutputstream;
                URLConnection urlconnection=new URL(schreibdatei).openConnection();

                urlconnection.setDoOutput(true);
    dataoutputstream=new DataOutputStream(urlconnection.getOutputStream());

                for(n=0;n<applet.highscores;n++)
                {
                        dataoutputstream.writeUTF(applet.highscorename[n]);
                        dataoutputstream.writeInt(applet.highscorepunkte[n]);
                }

    dataoutputstream.close();
        }

        String MakeName()
        {
                String name=silben[applet.Zufall(silben.length)];
                name+=silben[applet.Zufall(silben.length)];

                if(applet.Zufall(2)==1)
                        name+=silben[applet.Zufall(silben.length)];

                return name;
        }

        void NeueHighscores()
        {
                int n;

                for(n=0;n<applet.highscores;n++)
                {
                        applet.highscorename[n]=MakeName();
                        applet.highscorepunkte[n]=(applet.highscores-n)*(applet.highscores-n);
                }
        }

  String HighscoreString(int n)
  {
          return applet.Auffuellen(String.valueOf(n+1),String.valueOf(applet.highscores).length())+"."+applet.zwischenraum+applet.Auffuellen(applet.highscorename[n],applet.highscorenamelaenge)+applet.zwischenraum+applet.Auffuellen(String.valueOf(applet.highscorepunkte[n]),String.valueOf(applet.maxpunkte).length())+applet.punktestring;
  }

        void EnterScore()
        {
                int n;

                try
                {
                        HighscoresLesen();
      scoresok=true;
                }
                catch(Exception exception)
                {
/**/                    System.out.println("Error while loading highscores: "+exception.toString());

      scoresok=false;
                }

                for(index=0;index<applet.highscores&&applet.spielcanvas.punkte<=applet.highscorepunkte[index];index++)
                        ;

                if(index<applet.highscores)
                {
                        textfield.setText(MakeName());
                        textfield.selectAll();

                        applet.cardlayout.show(applet,"Score");
                        textfield.requestFocus();

                        try
                        {
        synchronized(applet.object)
        {
          applet.object.wait(eingabezeit);
        }
                        }
                        catch(InterruptedException exception)
                        {
                        }

                        for(n=applet.highscores-1;n>index;n--)
                        {
                                applet.highscorename[n]=applet.highscorename[n-1];
                                applet.highscorepunkte[n]=applet.highscorepunkte[n-1];
                        }

                        if((applet.highscorename[index]=textfield.getText()).length()>applet.highscorenamelaenge)
                                applet.highscorename[index]=applet.highscorename[index].substring(0,applet.highscorenamelaenge);

                        applet.highscorepunkte[index]=applet.spielcanvas.punkte;

                        applet.titelcanvas.highscoreindex=(index/applet.titelcanvas.highscoreseite)*applet.titelcanvas.highscoreseite;

                        try
                        {
        if(scoresok)
                                  HighscoresSchreiben();
                        }
                        catch(Exception exception)
                        {
/**/                            System.out.println("Error while saving highscores: "+exception.toString());
                        }
                }
                else
                {
                        applet.titelcanvas.highscoreindex=(applet.titelcanvas.highscoreindex+applet.titelcanvas.highscoreseite)%applet.highscores;
                }
        }

        public boolean action(Event event,Object object)
        {
    synchronized(applet.object)
    {
      applet.object.notifyAll();
    }

    return true;
        }
}

public class double_ extends Applet implements Runnable
{
        // ------------------------ Applet ------------------------

        String appletinfo="double\n\nWritten by Martin Koerwien & Michael Kraus\n1992-1998\nVersion JDK 1.0\n\nPlease send bug reports, comments and suggestions to:\nmichael.kraus@informatik.uni-muenchen.de\nOr visit my homepage:\nwww.informatik.uni-muenchen.de/~michael.kraus";

        String parameterinfo[][]=
        {
                {"width","","don't change this parameter"},
                {"height","","don't change this parameter"}
        };

  Thread thread;
  Object object=new Object();

        // ------------------------ AWT ------------------------

        String bildname[]=
  {
    "lr.gif",  // leere Ikone
    "lu0.gif",  // dunkle Ikonen
    "lo0.gif",
    "ro0.gif",
    "ru0.gif",
    "wg0.gif",
    "sk0.gif",
    "lu1.gif",  // helle Ikonen
    "lo1.gif",
    "ro1.gif",
    "ru1.gif",
    "wg1.gif",
    "sk1.gif",
    "double.gif",  // "double"-Schriftzug
    "autoren.gif"  // "written by..."-Schriftzug
  };

        Image bild[]=new Image[bildname.length];

  int doublebild=13;
  int autorenbild=14;

        Font pearlfont=new Font("Courier",Font.BOLD,16);
        FontMetrics fontmetrics;

        int fonthoehe;
        int fontaufstieg;

  Color schwarz=new Color(0x11,0x11,0x11);
  Color gelb=new Color(0xff,0xee,0xcc);

  CardLayout cardlayout=new CardLayout();

        SpielCanvas spielcanvas;
        TitelCanvas titelcanvas;
        HighscorePanel highscorepanel;

        // ------------------------ Maße ------------------------

  int appletbreite;
  int applethoehe;

  int doublebildbreite;
  int doublebildhoehe;
  int doublebildvspace=6;

  int doublebildx;
  int doublebildminy=doublebildvspace;
  int doublebildmaxy;

  int autorenbildbreite;
  int autorenbildhoehe;
  int autorenbildvspace=6;

  int autorenbildx;
  int autorenbildy;

  int texty;
  int texthoehe;

        // ------------------------ Highscores ------------------------

        int ohnepunkte=-1;
  int maxpunkte=999999;

  String unloeschbarstring=" pts. X";
  String punktestring=" pts.";
        String leername="               ";
  String zwischenraum="  ";
        int highscorenamelaenge=leername.length();

  int highscores=100;

  String highscorename[]=new String[highscores];
  int highscorepunkte[]=new int[highscores];

  // ------------------------ System Funktionen ------------------------

        int Zufall(int bereich)         // [0;bereich-1]
        {
                return (int)(Math.random()*bereich);
        }

        String Auffuellen(String string,int maximum)
        {
                return leername.substring(string.length(),maximum)+string;
        }

        void MittigSchreiben(Graphics graphics,String string,int y)
        {
                graphics.drawString(string,(appletbreite-fontmetrics.stringWidth(string))/2,y);
        }

        void DoublebildZeichnen(Graphics graphics,int y)
        {
                graphics.drawImage(bild[doublebild],doublebildx,y,null);
        }

        void AutorenbildZeichnen(Graphics graphics)
        {
                graphics.drawImage(bild[autorenbild],autorenbildx,autorenbildy,null);
        }

        void BilderLaden()
        {
                int n;
                MediaTracker mediatracker=new MediaTracker(this);

                for(n=0;n<bildname.length;n++)
                {
                        bild[n]=getImage(getCodeBase(),bildname[n]);
                        mediatracker.addImage(bild[n],0);
                }

                try
                {
                        mediatracker.waitForAll();
                }
                catch(InterruptedException exception)
                {
                }

    doublebildbreite=bild[doublebild].getWidth(null);
    doublebildhoehe=bild[doublebild].getHeight(null);

    autorenbildbreite=bild[autorenbild].getWidth(null);
    autorenbildhoehe=bild[autorenbild].getHeight(null);
        }

  public void init()
  {
                BilderLaden();

    thread=null;

                fontmetrics=getFontMetrics(pearlfont);
    fonthoehe=18;    // fontmetrics.getHeight();
    fontaufstieg=13;  // fontmetrics.getAscent();

                setBackground(schwarz);
    setLayout(cardlayout);

                spielcanvas=new SpielCanvas(this);
                titelcanvas=new TitelCanvas(this);
                highscorepanel=new HighscorePanel(this);

    add("Titel",titelcanvas);
    add("Spiel",spielcanvas);
                add("Score",highscorepanel);

                highscorepanel.textfieldbreite=highscorepanel.textfield.preferredSize().width;
                highscorepanel.textfieldhoehe=highscorepanel.textfield.preferredSize().height;

                highscorepanel.textfieldx=(appletbreite-highscorepanel.textfieldbreite)/2;
                highscorepanel.textfieldy=highscorepanel.welldoney+highscorepanel.eingabezeile*fonthoehe-fontaufstieg-(highscorepanel.textfieldhoehe-fonthoehe)/2;

                highscorepanel.textfield.reshape(highscorepanel.textfieldx,highscorepanel.textfieldy,highscorepanel.textfieldbreite,highscorepanel.textfieldhoehe);

                parameterinfo[0][1]=String.valueOf(appletbreite);
                parameterinfo[1][1]=String.valueOf(applethoehe);

    resize(appletbreite,applethoehe);
  }

  public void run()
  {
    while(true)
    {
      titelcanvas.Titel();
      spielcanvas.Spiel();
                        highscorepanel.EnterScore();
    }
  }

  public void start()
  {
    if(thread==null)
    {
      thread=new Thread(this);
      thread.start();
    }
  }

  public void stop()
  {
    if(thread!=null)
    {
      thread.stop();
      thread=null;
    }
  }

  public String getAppletInfo()
  {
                return appletinfo;
  }

  public String[][] getParameterInfo()
  {
    return parameterinfo;
  }
};


Back to the Double applet page

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft RIA Development Center
 IBM Rational Resource Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Free VMware Server 2.0 Now Release Candidate
Linux Player Xandros Grabs Storied Rival Linspire
Hey Enterprise: Here Comes the 3G iPhone
MySpace Opens Profile Portability API
Microsoft Jumps Into Virtualization Fray
Eclipse Ganymede Makes It Easier for Devs
Open Source Nokia a Threat to Microsoft, Google?
Salesforce, Google Head for 2nd on Apps
HP Open Sources Unix File System for Linux
Red Hat Opens Its Network to Space

Build a Generic Histogram Generator for SQL Server
Beyond XML and JSON: YAML for Java Developers
Mastering the Windows Mobile Emulators
Avaya AE Services Provide Rapid Telephony Integration with Facebook
Featured Algorithm: Intel Threading Building Blocks: parallel_reduce
Getting Started with Windows Live Admin Center
Eight Key Practices for ASP.NET Deployment
Java ME User Interfaces: Do It with LWUIT!
Talking VPro: Transcript
Bringing Semantic Technology to the Enterprise

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES