상규

~cpp 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

class SnakeBiteCanvas extends Canvas {
	private final int boardWallWidth = 4;
	private final int snakeCellWidth = 4;

	private final int canvasWidth;
	private final int canvasHeight;

	private final int boardX;
	private final int boardY;
	private final int boardWidth;
	private final int boardHeight;

	private final int boardInnerX;
	private final int boardInnerY;
	private final int boardInnerWidth;
	private final int boardInnerHeight;

	private final int snakeCellXRange;
	private final int snakeCellYRange;
	
	private int snakeCellBeforeX;
	private int snakeCellBeforeY;
	private int snakeCellX;
	private int snakeCellY;

	private boolean drawAll;
	
	public SnakeBiteCanvas() {
		canvasWidth = getWidth();
		canvasHeight = getHeight();

		boardWidth = canvasWidth - 6 - (canvasWidth - 6 - boardWallWidth * 2) % snakeCellWidth;
		boardHeight = boardWidth / 10 * 8 - (boardWidth / 10 * 8 - boardWallWidth * 2) % snakeCellWidth;
		boardX = (canvasWidth - boardWidth) / 2;
		boardY = 3;
		
		boardInnerX = boardX + boardWallWidth;
		boardInnerY = boardY + boardWallWidth;
		boardInnerWidth = boardWidth - boardWallWidth * 2;
		boardInnerHeight = boardHeight - boardWallWidth * 2;
		
		snakeCellXRange = boardInnerWidth / snakeCellWidth;
		snakeCellYRange = boardInnerHeight / snakeCellWidth;
		
		snakeCellBeforeX = 1;
		snakeCellBeforeY = 1;
		snakeCellX = 1;
		snakeCellY = 1;

		drawAll = true;
	}

	public void paint(Graphics g) {
		if(drawAll) {
			g.setColor(255, 255, 255);
			g.fillRect(0, 0, canvasWidth, canvasHeight);

			g.setColor(0, 0, 0);
			g.fillRect(boardX, boardY, boardWidth, boardHeight);

			g.setColor(255, 255, 255);
			g.fillRect(boardInnerX - 1, boardInnerY - 1, boardInnerWidth + 2, boardInnerHeight + 2);

			drawAll = false;
		}

		g.setColor(255, 255, 255);
		g.fillRect(boardInnerX + snakeCellWidth * (snakeCellBeforeX - 1),
			boardInnerY + snakeCellWidth * (snakeCellBeforeY - 1),
			snakeCellWidth, snakeCellWidth);

		g.setColor(0, 0, 0);
		g.fillRect(boardInnerX + snakeCellWidth * (snakeCellX - 1),
			boardInnerY + snakeCellWidth * (snakeCellY - 1),
			snakeCellWidth, snakeCellWidth);
	}
	
	public void keyPressed(int keyCode) {
		snakeCellBeforeX = snakeCellX;
		snakeCellBeforeY = snakeCellY;

		int gameAction = getGameAction(keyCode);
		if(gameAction == Canvas.LEFT && snakeCellX > 1)
			snakeCellX--;
		else if(gameAction == Canvas.RIGHT && snakeCellX < snakeCellXRange)
			snakeCellX++;
		else if(gameAction == Canvas.UP && snakeCellY > 1)
			snakeCellY--;
		else if(gameAction == Canvas.DOWN && snakeCellY < snakeCellYRange)
			snakeCellY++;
		
		repaint();
	}
};

public class SnakeBite extends MIDlet implements CommandListener {
	Display display;
	SnakeBiteCanvas canvas;
	Command exitCommand;
	
	public SnakeBite() {
		display = Display.getDisplay(this);
		canvas = new SnakeBiteCanvas();
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		canvas.addCommand(exitCommand);
		canvas.setCommandListener(this);
	}
	
	public void startApp() {
		display.setCurrent(canvas);
	}
	
	public void pauseApp() {
	}
	
	public void destroyApp(boolean unconditional) {
		display = null;
		canvas = null;
		exitCommand = null;
	}
	
	public void commandAction(Command c, Displayable d) {
		if(c == exitCommand) {
			destroyApp(true);
			notifyDestroyed();
		}
	}
};

재동

~cpp 
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

class StartCanvas extends Canvas {
	private String startString;
	private final int BACKGROUND_COLOR = 0xFFFFFF;
	private final int FOREGROUND_COLOR = 0x000000;
	private Image image;
	
	public StartCanvas() {
		try {
			image = Image.createImage("/start.png");
		}
		catch(Exception e) {
			System.out.println("그림이 없음");
		}
	}
	public void paint(Graphics g) {
		g.setColor(BACKGROUND_COLOR);
        g.fillRect(g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight());
        g.setColor(FOREGROUND_COLOR);
        g.drawImage(image,this.getWidth()/2,this.getHeight()/2,g.HCENTER|g.VCENTER);
	}
}
class BoardCanvas extends Canvas {
	private int board[][];
	private int snakeHeadX;
	private int snakeHeadY;
	private int pathLength;
	private int boardLength;
	private final int snakeRect;
	
	private final int BACKGROUND_COLOR = 0xFFFFFF;
	private final int FOREGROUND_COLOR = 0x000000;

	private final int boardX;
	private final int boardY;
	private final int boardWidth;
	private final int boardHeight;
	
	public BoardCanvas() {
		boardLength = 10;
		
		board = new int[boardLength][boardLength];
        initializeBoard();
        
		snakeHeadX = 20;
		snakeHeadY = 30;
		pathLength = 4;
		snakeRect = 4;
		
		boardX = 10;
		boardY = 20;
		boardWidth = this.getWidth() - 20;
		boardHeight = this.getHeight() - 20;
	}
	public void initializeBoard() {
	}
	
	public void keyPressed(int keyCode) {
	    int gameAction = getGameAction(keyCode);
	    if(isMoveLeft(gameAction))
	    	snakeHeadX -= pathLength;
	    else if(isMoveRight(gameAction))
	    	snakeHeadX += pathLength;
	    else if(isMoveUp(gameAction))
	    	snakeHeadY -= pathLength;
	    else if(isMoveDown(gameAction))
	    	snakeHeadY += pathLength;    
	    repaint();
    }
    
    public boolean isMoveUp(int ga) {
    	return ga == Canvas.UP && snakeHeadY > boardY;
    }
    
    public boolean isMoveDown(int ga) {
    	return ga == Canvas.DOWN && snakeHeadY < boardHeight + boardY - snakeRect * 2;
    }
    
    public boolean isMoveLeft(int ga) {
    	return ga == Canvas.LEFT && snakeHeadX > boardX;
    }
    public boolean isMoveRight(int ga) {
    	return ga == Canvas.RIGHT && snakeHeadX < boardWidth + boardX - snakeRect * 2;
    }
    
	public void paint(Graphics g) {
		g.setColor(BACKGROUND_COLOR);
        g.fillRect(g.getClipX(),g.getClipY(),g.getClipWidth(),g.getClipHeight());
        g.setColor(FOREGROUND_COLOR);
        g.drawRect(boardX-2,boardY-2,boardWidth,boardHeight);
        g.fillRect(snakeHeadX,snakeHeadY,snakeRect,snakeRect);
	}
}

public class SnakeBite extends MIDlet implements CommandListener {
	private Display display;
    private BoardCanvas boardCanvas;
    private StartCanvas startCanvas;
    
    private Command exitCommand;
    private Command startCommand;
    
	public SnakeBite() {
        display = Display.getDisplay(this);
        
        exitCommand = new Command("Exit", Command.EXIT, 1);
        startCommand = new Command("Start", Command.SCREEN, 2);
        
        startCanvas = new StartCanvas();
        boardCanvas = new BoardCanvas();
        
        startCanvas.addCommand(startCommand);
        startCanvas.addCommand(exitCommand);
        startCanvas.setCommandListener(this);
        
        boardCanvas.addCommand(exitCommand);
        boardCanvas.setCommandListener(this);
    }
    public void startApp() {
    	display.setCurrent(startCanvas);
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
		display = null;
		boardCanvas = null;
		startCanvas = null;
		exitCommand = null;
		startCommand = null;
    }
    
    public void commandAction(Command c, Displayable d) {
	    if(c == exitCommand) {
	        destroyApp(true);
	        notifyDestroyed();
	    }
	    else if(c == startCommand) {
	    	display.setCurrent(boardCanvas);
	    }
    }
}
Retrieved from http://wiki.zeropage.org/wiki.php/MobileJavaStudy/SnakeBite/Spec1Source
last modified 2021-02-07 05:23:46