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

📄 graphicstest.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
字号:
/* * GraphicsTest.java * * Created on den 3 juli 2003, 14:12 */package DXBenchmarker;import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet;import java.io.IOException;import java.util.Random;public class GraphicsTest extends Canvas implements CommandListener, Runnable{  private static final Command exitCommand = new Command("Exit", Command.EXIT, 2);  private static final Command helpCommand = new Command("Help", Command.HELP, 2);  private static final Command resumeCommand = new Command("Resume", Command.HELP, 2);    private static final int COLOR_WHITE = 0xFFFFFFFF;  private static final int COLOR_BLACK = 0x00000000;  private static final int COLOR_YELLOW = 0x00FFFF00;    private static final int VERTICAL_DIRECTION = 0;  private static final int HORIZONTAL_DIRECTION = 1;    private static final int NUMBER_OF_TILES_TO_LOAD = 8;    private static final int TILE_WIDTH = 16;  private static final int TILE_HEIGHT = 16;    private static final int MAX_BALLS = 60;    //Application thread  private Thread thread;    private Display display;  private Displayable previousDisplay;    private int scrollBackGroundInDirection;    // Application is stopped (terminated)  private boolean stopped;    // Application drawing is paused (waiting for paint from the event thread)  private boolean paused;    private Random random;    private int widthInTiles;  private int heightInTiles;    private Image backgroundImage;  private Image semcLogo;    private int tileMapXPos[];  private int tileMapYPos[];    private boolean showFPS = true;    private long startStamp;  private int fps;    private int tempValue = 0;    // Number of bouncing Images and their positions [x, dx, y, dy]  private int n;  private int[] positions;    private boolean showHelp = false;    private String[] helpStrings;    private int currentDisplayedLine;    private int stringHeight;  private int minAllowedStringLines;    public GraphicsTest(Display display, Displayable previousDisplay)  {    this.display = display;    this.previousDisplay = previousDisplay;        scrollBackGroundInDirection = VERTICAL_DIRECTION;        stopped = false;    paused = true;        random = new Random();        try    {      backgroundImage = Image.createImage("/background2.png");      semcLogo = Image.createImage("/semc_logo.png");    }    catch (IOException ioe)    {      System.out.println("Can't load image. " + ioe);      stop();    }        widthInTiles = backgroundImage.getWidth() / getWidth();    heightInTiles = backgroundImage.getHeight() / getHeight();        while (heightInTiles * backgroundImage.getHeight() < getHeight() * 2)    {      if (backgroundImage.getWidth() % getWidth() != 0)      {        widthInTiles++;        if (widthInTiles == 1)        {          widthInTiles++;        }      }      if ((backgroundImage.getHeight() % getHeight() != 0))      {        heightInTiles++;        if (heightInTiles == 1)        {          heightInTiles++;        }      }    }        tileMapXPos = new int[widthInTiles * heightInTiles];    tileMapYPos = new int[widthInTiles * heightInTiles];        for (int tileRow = 0; tileRow < heightInTiles; tileRow++)    {      for (int tileCol = 0; tileCol < widthInTiles; tileCol++)      {        tileMapXPos[tileRow * widthInTiles + tileCol] = tileCol * backgroundImage.getWidth();        tileMapYPos[tileRow * widthInTiles + tileCol] = tileRow * backgroundImage.getHeight();      }    }        n = 1;    positions = new int[40];    positions[0] = 50;    positions[1] = 2;    positions[2] = 0;    positions[3] = 2;        startStamp = System.currentTimeMillis();        helpStrings = new String[5];    helpStrings[0] = "Up - Add ball";    helpStrings[1] = "Down - Remove ball";    helpStrings[2] = "Left - Reset balls";    helpStrings[3] = "Right - Run Test";    helpStrings[4] = "Fire - Toggle FPS";        currentDisplayedLine = 0;    stringHeight = 0;    minAllowedStringLines = 0;        this.addCommand(exitCommand);    this.addCommand(helpCommand);    this.setCommandListener(this);        try    {      thread = new Thread(this);      thread.start();    }    catch(Exception e)    {      System.out.println(e.getMessage());    }      }    public void commandAction(Command c, Displayable s)  {    if (c == exitCommand)    {      this.stop();      display.setCurrent(previousDisplay);    }    else if (c == helpCommand)    {      this.setShowHelp(!this.isHelpShown());      this.removeCommand(helpCommand);      this.addCommand(resumeCommand);    }    else if (c == resumeCommand)    {      this.setShowHelp(!this.isHelpShown());      this.removeCommand(resumeCommand);      this.addCommand(helpCommand);    }  }    public boolean isHelpShown()  {    return showHelp;  }    public void setShowHelp(boolean show)  {    showHelp = show;  }    public void restart()   {    stopped = false;   }    public void stop()  {    stopped = true;  }    public void run()  {    while (!stopped)    {      int xMax = getWidth() - semcLogo.getWidth();      int yMax = getHeight() - semcLogo.getHeight();            // Wait for previous drawing to complete      while (paused && !stopped)      {        Thread.currentThread().yield();      }            if (!stopped)      {        int arrayIndex = 0;        int r1;        int r2;        int r3;                synchronized (positions)        {          for (int i = 0; i < n; i++)          {            // r1 = x;            r1 = positions[arrayIndex];            // r2 = dx            r2 = positions[arrayIndex + 1];            r3 = r1 + r2;                        if (r3 < 0 || r3 > xMax)            {              // switch x direction              positions[arrayIndex + 1] = 0 - r2;            }            else            {              // move x              positions[arrayIndex] = r3;            }            arrayIndex += 2;            // r1 = y;            r1 = positions[arrayIndex];            // r2 = dy            r2 = positions[arrayIndex + 1];            r3 = r1 + r2;                        if (r3 < 0 || r3 > yMax)            {              // switch y direction              positions[arrayIndex + 1] = 0 - r2;            }            else            {              // move y              positions[arrayIndex] = r3;            }            arrayIndex += 2;          }        } // end synchronized                if (scrollBackGroundInDirection == VERTICAL_DIRECTION)        {          for (int tileRow = 0; tileRow < heightInTiles; tileRow++)          {            for (int tileCol = 0; tileCol < widthInTiles; tileCol++)            {              tileMapYPos[tileRow * widthInTiles + tileCol]--;              if (tileMapYPos[tileRow * widthInTiles + tileCol] + backgroundImage.getHeight() == 0)              {                tileMapYPos[tileRow * widthInTiles + tileCol] = (heightInTiles - 1) * backgroundImage.getWidth();              }            }          }        }        // pause moving until drawn        paused = true;        repaint();      }    }  }      protected void paint(Graphics g)  {    if (showHelp)    {      boolean done = false;      int numberOfLine = 0;            stringHeight = g.getFont().getHeight();      minAllowedStringLines = getHeight() / (stringHeight + 8);            g.setColor(COLOR_WHITE);      g.fillRect(0, 0, getWidth(), getHeight());            g.setColor(COLOR_BLACK);      for (int i = currentDisplayedLine; i < helpStrings.length && !done; i++)      {        if (stringHeight * numberOfLine + stringHeight < getHeight())        {          g.drawString(helpStrings[i], 0, stringHeight * numberOfLine, g.TOP | g.LEFT);        }        else        {          done = true;        }        numberOfLine++;      }    }    else if (!showHelp)    {      int arrayIndex = 0;      int x;      int y;            g.setColor(COLOR_BLACK);            for (int tileRow = 0; tileRow < heightInTiles; tileRow++)      {        for (int tileCol = 0; tileCol < widthInTiles; tileCol++)        {          g.drawImage(backgroundImage, tileMapXPos[tileRow * widthInTiles + tileCol], tileMapYPos[tileRow * widthInTiles + tileCol], g.TOP | g.LEFT);        }        System.currentTimeMillis();      }            for (int i = 0; i < n; i++)      {        x = positions[arrayIndex];        arrayIndex += 2;        y = positions[arrayIndex];        arrayIndex += 2;                g.drawImage(semcLogo, x , y, g.TOP | g.LEFT);      }            if (showFPS)      {        // Drawing is done. Check time since previous time here.        if (System.currentTimeMillis() - startStamp != 0)        {                    fps = 1000 / (int) (System.currentTimeMillis() - startStamp);                    g.setColor(COLOR_BLACK);          g.drawString(Integer.toString(fps), 5, 0, g.TOP | g.LEFT);         // g.drawString(Integer.toString(getHeight()), 5, 16, g.TOP | g.LEFT);         // g.drawString(Integer.toString(semcLogo.getHeight()), 5, 32, g.TOP | g.LEFT);                    g.drawString(Integer.toString(n), getWidth() - 5, 0, g.TOP | g.RIGHT);         // g.drawString(Integer.toString(positions[arrayIndex - 2]), getWidth() - 5, 16, g.TOP | g.RIGHT);                    //if (positions[arrayIndex - 2] > tempValue)          //{          //  tempValue = positions[arrayIndex - 2];          //}          //g.drawString(Integer.toString(tempValue), getWidth() - 5, 32, g.TOP | g.RIGHT);        }                startStamp = System.currentTimeMillis();      }    }    paused = false;  }    protected void keyPressed(int keyCode)  {    int neededSize = 0;        switch (getGameAction(keyCode))    {      case UP:        if (showHelp)        {          if (currentDisplayedLine > 0)          {            currentDisplayedLine--;          }        }        else        {          n++;          neededSize = n << 2; // * 4          if (positions.length < neededSize)          {            int[] larger = new int[neededSize + 40];            System.arraycopy(positions, 0, larger, 0, positions.length);            positions = larger;          }          // Init          neededSize--;          positions[neededSize--] = (random.nextInt() > 0) ? 2 : -2; // dy          positions[neededSize--] = Math.abs(random.nextInt()) % (getHeight() - semcLogo.getHeight()); // y          positions[neededSize--] = (random.nextInt() > 0) ? 2 : -2; // dx          positions[neededSize--] = Math.abs(random.nextInt()) % (getWidth() - semcLogo.getWidth()); // x        }        break;      case DOWN:        if (showHelp)        {          if (helpStrings.length - currentDisplayedLine > minAllowedStringLines)          {            currentDisplayedLine++;            if (currentDisplayedLine >= minAllowedStringLines)            {              currentDisplayedLine--;            }          }        }        else        {          n--;          if (n <= 0)          {            n = 1;          }        }        break;      case LEFT:        while(n != 1)        {          n--;          if (n <= 0)          {            n = 1;          }        }        break;      case RIGHT:        while (n != MAX_BALLS)        {          if (n < MAX_BALLS)          {            n++;            neededSize = n << 2; // * 4            if (positions.length < neededSize)            {              int[] larger = new int[neededSize + 40];              System.arraycopy(positions, 0, larger, 0, positions.length);              positions = larger;            }            // Init            neededSize--;            positions[neededSize--] = (random.nextInt() > 0) ? 2 : -2; // dy            positions[neededSize--] = Math.abs(random.nextInt()) % (getHeight() - semcLogo.getHeight()); // y            positions[neededSize--] = (random.nextInt() > 0) ? 2 : -2; // dx            positions[neededSize--] = Math.abs(random.nextInt()) % (getWidth() - semcLogo.getWidth()); // x          }          else if (n > MAX_BALLS)          {            n--;            if (n <= 0)            {              n = 1;            }          }        }        break;      case FIRE:        showFPS = !showFPS;        break;      default:        break;    }    repaint();  }}

⌨️ 快捷键说明

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