package elte.java2_utikalauz5.j2me; import java.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** Reversi játék. @link.forrásfájl {@docRoot}/../data/j2me/src CanvasTeszt.java @link.letöltés {@docRoot}/../data/j2me CanvasTeszt.jar @link.letöltés {@docRoot}/../data/j2me CanvasTeszt.jad @since Java 2 Útikalauz programozóknak 5.0 */ public class CanvasTeszt extends MIDlet implements CommandListener { class ReversiCanvas extends Canvas { private Image offscreen = null; private Graphics offGraphics = null; private static final int ASPECT_LIMIT = 15; // 1.5 private boolean vertInfo; public ReversiCanvas(CanvasTeszt boss, Display display) { this.boss = boss; this.display = display; colored = display.isColor() && display.numColors() > 127; vertInfo = true; width = getWidth() * 8 / 10; vertWidth = getWidth() - width; height = getHeight(); sizex = (width-1) / 8; sizey = (height-1) / 8; if( 10*sizex / sizey > ASPECT_LIMIT ) { sizex = sizey * ASPECT_LIMIT / 10; width = sizex * 8; } if( 10*sizey / sizex > ASPECT_LIMIT ) { sizey = sizex * ASPECT_LIMIT / 10; height = sizey * 8; } selx = 0; sely = 0; // if( !isDoubleBuffered() ) { // there are phones which claims to have a doubleBuffered Canvas // although they don't have offscreen = Image.createImage(width+vertWidth, height); offGraphics = offscreen.getGraphics(); // } } protected void paint(Graphics g) { Graphics saved = g; if( offscreen != null ) { g = offGraphics; } g.setColor(0xffffff); g.fillRect(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight()); drawBoard(g); drawPiece(g, 3, 3, 0); // kezdeti kövek drawPiece(g, 4, 3, 1); drawPiece(g, 3, 4, 1); drawPiece(g, 4, 4, 0); drawSelectionBox(g); g.setColor(0x000000); g.drawString("0", width+vertWidth, sizey+2, g.TOP| g.RIGHT); g.drawString("0", width+vertWidth, 7 * sizey, g.BOTTOM| g.RIGHT); if( g != saved ) { saved.drawImage( offscreen, 0, 0, Graphics.LEFT | Graphics.TOP ); } } protected void drawBoard(Graphics g) { if( colored ) { g.setColor(BG_COLOR); g.fillRect(0,0, 8*sizex, 8*sizey); } g.setColor(0x000000); for( int i=0; i<=8; ++i ) { g.drawLine(0, i*sizey, 8*sizex, i*sizey); g.drawLine(i*sizex, 0, i*sizex, 8*sizey); } } protected void drawPiece(Graphics g,int row, int col, int player) { int x = row*sizex+sizex/6; int y = col*sizey+sizey/6; int w = 2*sizex/3; int h = 2*sizey/3; if( player == 1 ) { if( colored ) { g.setColor(P1_COLOR ); g.fillArc(x,y,w,h,0,360); } else { g.drawArc(x,y,w,h,0,360); } } else { if( colored ) { g.setColor(P2_COLOR ); } else { g.setColor(0x000000); } g.fillArc(x,y,w,h,0,360); } } protected void drawSelectionBox(Graphics g, int sx, int sy) { if( colored ) { g.setColor(BOX_P1_COLOR); } else { g.setColor(LIGHT_BOX_COLOR); } g.drawRect( sx*sizex, sy*sizey,sizex, sizey); g.drawRect( sx*sizex+1, sy*sizey+1,sizex-2, sizey-2); } protected void drawSelectionBox(Graphics g) { drawSelectionBox(g, selx, sely); } public void keyPressed(int keyCode) { int oldselx = selx; int oldsely = sely; switch( getGameAction(keyCode) ) { case Canvas.UP: sely = (sely + 8 -1) % 8; repaint(); break; case Canvas.DOWN: sely = (sely + 1) % 8; repaint(); break; case Canvas.LEFT: selx = (selx + 8 -1) % 8; repaint(); break; case Canvas.RIGHT: selx = (selx + 1) % 8; repaint(); break; case Canvas.FIRE: //TODO boss.nextTurn(selx, sely); repaint(); break; } } protected CanvasTeszt boss; protected Display display; protected boolean colored; public static final int P1_COLOR = 0xff0000; public static final int P2_COLOR = 0x0000ff; public static final int LIGHT_BOX_COLOR = 0x008f00; public static final int DARK_BOX_COLOR = 0x000000; public static final int BOX_P1_COLOR = P1_COLOR; public static final int BOX_P2_COLOR = P2_COLOR;; public static final int BG_COLOR = 0xffffd0; int width, height; int vertWidth; int sizex, sizey; int selx, sely; int pnums[] = new int[2]; String infoLines[] = new String[3]; } // ReversiCanvas protected boolean started = false; private ReversiCanvas canvas; private Command exitCommand; // The exit command public void startApp() { if( !started ) { canvas = new ReversiCanvas(this, Display.getDisplay(this)); exitCommand = new Command("Exit", Command.EXIT, 99); canvas.addCommand(exitCommand); canvas.setCommandListener(this); started = true; } Display.getDisplay(this).setCurrent(canvas); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if( d.equals(canvas) ) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } } }