Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /**
  7.  *
  8.  * @author hp78
  9.  */
  10.  
  11. import java.io.IOException;
  12. import java.io.*;
  13. import java.util.*;
  14. import java.util.Timer;
  15. import javax.microedition.lcdui.Graphics;
  16. import javax.microedition.lcdui.*;
  17. import javax.microedition.lcdui.game.GameCanvas;
  18. import javax.microedition.lcdui.game.LayerManager;
  19. import javax.microedition.lcdui.game.Sprite;
  20. import javax.microedition.lcdui.game.TiledLayer;
  21. import javax.microedition.lcdui.game.Layer;
  22. import javax.microedition.lcdui.Font;
  23. import javax.microedition.media.*;
  24. import javax.microedition.media.Control.*;
  25. import javax.microedition.media.control.ToneControl;
  26. import javax.microedition.media.protocol.*;
  27. import javax.microedition.io.*;
  28. import javax.microedition.rms.RecordStoreException;
  29.  
  30. public class MenuCanvas extends GameCanvas implements Runnable {
  31.  
  32.    
  33.     //menu system
  34.     private Graphics g = getGraphics();
  35.     private int w = getWidth();
  36.     private int h = getHeight();
  37.     private int currentSelectedIndex = 0;
  38.     boolean x = true;
  39.     private Thread t;
  40.     private Display d;
  41.     private BeatBoyCanvas gameCanvas;
  42.     private GameMidlet boss;
  43.    
  44.    
  45.  
  46.  
  47.     public MenuCanvas() {
  48.         super(true);
  49.  
  50.  
  51.  
  52.          try {
  53.             this.setFullScreenMode(true);
  54.             this.init();
  55.         } catch (IOException ex) {
  56.             ex.printStackTrace();
  57.         }
  58.     }
  59.  
  60.     private void init() throws IOException{
  61.    
  62.  
  63.        }
  64.  
  65.     public void run() {
  66.  
  67.  
  68.             //menu render
  69.             drawBg();
  70.             drawMenu(currentSelectedIndex);
  71.  
  72.            
  73.            
  74.  
  75.             //check keystate
  76.             while(x = true ){
  77.  
  78.             int keyState = getKeyStates();
  79.             if((keyState & DOWN_PRESSED)!=0)
  80.             {
  81.                if(currentSelectedIndex == 0)
  82.                    currentSelectedIndex = 1;
  83.                else
  84.                    currentSelectedIndex = 0;
  85.  
  86.                drawBg();
  87.                drawMenu(currentSelectedIndex);
  88.                System.out.println(currentSelectedIndex);
  89.              
  90.  
  91.             }
  92.  
  93.            else if ((keyState & UP_PRESSED) != 0)
  94.            {
  95.                if(currentSelectedIndex == 0)
  96.                    currentSelectedIndex = 1;
  97.                else
  98.                    currentSelectedIndex = 0;
  99.  
  100.                drawBg();
  101.                drawMenu(currentSelectedIndex);
  102.                System.out.println(currentSelectedIndex);
  103.  
  104.             }
  105.  
  106.             else if((keyState & FIRE_PRESSED) != 0)
  107.             {
  108.                 if(currentSelectedIndex == 0){
  109.                 boss.startGame();
  110.                 x = false;
  111.                 }
  112.                 else
  113.                 System.out.println("exit chosen");
  114.                 x=false;
  115.                
  116.  
  117.             }
  118.            
  119.             flushGraphics();
  120.  
  121.          
  122.  
  123.     }
  124.    
  125.             try {
  126.                 Thread.sleep(20);
  127.             } catch (InterruptedException ex) {
  128.                 ex.printStackTrace();
  129.             }
  130.     }
  131.  
  132.      
  133.         // work with menu according to its current state
  134.  
  135.        
  136.  
  137.     /**
  138.      * Stops the main game loop.
  139.      */
  140.     public void stop() {
  141.  
  142.     }
  143.  
  144.     //paint bg black
  145.     public void drawBg ()
  146.     {
  147.         g.setColor(0, 0, 0);
  148.         g.fillRect(0, 0, getWidth(), getHeight());
  149.         //flushGraphics();
  150.     }
  151.  
  152.     //drawmenu
  153.     public void drawMenu (int currentSelectedIndex)
  154.     {
  155.         g.setColor(255,0,0);
  156.         String menuTitle = "SUPER BEAT BOY";
  157.         g.drawString(menuTitle,w/2, h/2,g.BASELINE|g.HCENTER);
  158.  
  159.         //start menu options
  160.  
  161.         String menuOptions[] = {"PLAY", "EXIT"};
  162.         int paddedHeight = h/2 + 40;
  163.  
  164.  
  165.         for (int i = 0; i<2; i++)
  166.         {
  167.             if(currentSelectedIndex == i)
  168.             {
  169.                 g.setColor(255,255,255);
  170.             }
  171.             else
  172.             {
  173.                 g.setColor(255,0,0);
  174.             }
  175.                 g.drawString(menuOptions[i],w/2, paddedHeight,g.BASELINE|g.HCENTER);
  176.                 paddedHeight = paddedHeight + 40;
  177.         }
  178.         //flushGraphics();
  179.     }
  180.  
  181.  
  182.  
  183.    
  184.  
  185.  
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement