Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 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.     private boolean interrupted;
  39.    
  40.  
  41.  
  42.     public MenuCanvas() {
  43.         super(true);
  44.  
  45.  
  46.  
  47.          try {
  48.             this.setFullScreenMode(true);
  49.             this.init();
  50.         } catch (IOException ex) {
  51.             ex.printStackTrace();
  52.         }
  53.     }
  54.  
  55.     private void init() throws IOException{
  56.    
  57.  
  58.        }
  59.  
  60.     public void run() {
  61.  
  62.  
  63.             //menu render
  64.             drawBg();
  65.             drawMenu(currentSelectedIndex);
  66.  
  67.  
  68.             int keyState = getKeyStates();
  69.            
  70.             //check keystate
  71.             while (!this.interrupted) {
  72.             if((keyState & DOWN_PRESSED)!=0)
  73.             {
  74.                if(currentSelectedIndex == 0)
  75.                    currentSelectedIndex = 1;
  76.                else
  77.                    currentSelectedIndex = 0;
  78.  
  79.                drawBg();
  80.                drawMenu(currentSelectedIndex);
  81.                
  82.  
  83.             }
  84.  
  85.            else if ((keyState & UP_PRESSED) != 0)
  86.            {
  87.                if(currentSelectedIndex == 0)
  88.                    currentSelectedIndex = 1;
  89.                else
  90.                    currentSelectedIndex = 0;
  91.  
  92.                drawBg();
  93.                drawMenu(currentSelectedIndex);
  94.                g.drawString("hi",0,0,0);
  95.                
  96.             }
  97.            
  98.             flushGraphics();
  99.  
  100.          
  101.  
  102.             try {
  103.                 Thread.sleep(20);
  104.             } catch (InterruptedException ex) {
  105.                 ex.printStackTrace();
  106.             }
  107.     }
  108.     }
  109.  
  110.      
  111.         // work with menu according to its current state
  112.  
  113.        
  114.  
  115.     /**
  116.      * Stops the main game loop.
  117.      */
  118.     public void stop() {
  119.  
  120.     }
  121.  
  122.     //paint bg black
  123.     public void drawBg ()
  124.     {
  125.         g.setColor(0, 0, 0);
  126.         g.fillRect(0, 0, getWidth(), getHeight());
  127.         flushGraphics();
  128.     }
  129.  
  130.     //drawmenu
  131.     public void drawMenu (int currentSelectedIndex)
  132.     {
  133.         g.setColor(255,0,0);
  134.         String menuTitle = "SUPER BEAT BOY";
  135.         g.drawString(menuTitle,w/2, h/2,g.BASELINE|g.HCENTER);
  136.  
  137.         //start menu options
  138.  
  139.         String menuOptions[] = {"PLAY", "EXIT"};
  140.         int paddedHeight = h/2 + 40;
  141.  
  142.  
  143.         for (int i = 0; i<2; i++)
  144.         {
  145.             if(currentSelectedIndex == i)
  146.             {
  147.                 g.setColor(255,255,255);
  148.             }
  149.             else
  150.             {
  151.                 g.setColor(255,0,0);
  152.             }
  153.                 g.drawString(menuOptions[i],w/2, paddedHeight,g.BASELINE|g.HCENTER);
  154.                 paddedHeight = paddedHeight + 40;
  155.         }
  156.         flushGraphics();
  157.     }
  158.  
  159.  
  160.  
  161.    
  162.  
  163.  
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement