📄 puzzleview.java
字号:
/*
* Copyright (C) 2005-2006 Leopardo.f
*
* This file is part of M-SuDoKu, a J2ME version of SuDoKu.
*
* M-SuDoKu is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* M-SuDoKu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with M-SuDoKu; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA. Or, visit http://www.gnu.org/copyleft/gpl.html
*/
package MSuDoKu;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* Class handling all screen painting tasks for the puzzle
* @author Leopardo.f
*/
public class PuzzleView extends Canvas {
public static final byte CORRECT = 1, ERROR = 2, CURSOR = 4, CARVEDINSTONE = 8;
protected static final String[] saBack = {"sfondo.PNG", "sfondo4_5.png", "sfondo00231_3.png", "Wood.png", "sfondo00177_2.png"};
protected static final String sDigit = "digits3.png";
protected static final byte[] byaDigitSize = {8, 10, 12, 16};
protected int nWidth, nHeight, nCellWidth, nPuzzleWidth, xStart, yStart, yDigitOffset = 0, yTotalOffset = 0;
protected int nFore = 0x00000000, nForeThick = 0x00000000, nBack = 0x00AAAAFF, nError = 0x00FF0000, nCursorBack = 0x00FFFFFF;
protected static final int[][] naaBackColor = {
{
0x008888FF, 0x00B5ADD6, 0x008181FF, 0x00EFD286, 0x00D8BC68, 0x00D0CAAE
},
{
0x00BBBBFF, 0x00D0CAEE, 0x001919F0, 0x00CBAE62, 0x00C09C45, 0x00B0A48F
}
};
protected Image imagePuzzle, imageBackground, imageTemp, imageDigits;
protected Graphics gPuzzle;
protected byte bBack, byDigit;
protected PuzzleModel pmModel;
protected boolean bShowingSolution;
/** Creates a new instance of PuzzleView */
public PuzzleView (byte b) {
doComputations();
bBack = b;
bShowingSolution = false;
imageBackground = Image.createImage (getWidth(), getHeight());
imagePuzzle = Image.createImage (getWidth(), getHeight());
gPuzzle = imagePuzzle.getGraphics();
drawPuzzle (imageBackground.getGraphics());
gPuzzle.drawImage (imageBackground, 0, 0, Graphics.TOP | Graphics.LEFT);
}
/* JDB --> */
/** Performs cleanup stuff (credits to JDB) */
public void deleteAll()
{
imageBackground = null;
imagePuzzle = null;
imageTemp = null;
imageDigits = null;
gPuzzle = null;
pmModel = null;
System.gc();
}
/* <-- JDB */
/** Sets the PuzzleModel for this PuzzleView
* @param pm PuzzleModel to set
* @see PuzzleModel
*/
public void setPuzzleModel (PuzzleModel pm)
{
pmModel = pm;
}
/** Performs all needed preliminary computations
*/
protected void doComputations()
{
nWidth = getWidth();
nHeight = getHeight();
byDigit = getDigitSize (nWidth, nHeight);
xStart = (nWidth - nPuzzleWidth) / 2;
yStart = Math.max (0, (nHeight - nPuzzleWidth) / 2 - 1);
// System.out.println ("Start: (" + xStart + "," + yStart + "); Cell width: " + nCellWidth + "; PuzzleWidth: " + nPuzzleWidth);
// System.out.println ("charHeight:" + fNormal.getHeight() + ", charWidth:" + fNormal.charWidth ('0'));
imageTemp = Image.createImage (nCellWidth, nCellWidth);
}
/** Computes digit size from the bitmap image
* @param nX Max x dimension
* @param nY Max y dimension
*/
protected byte getDigitSize (int nX, int nY)
{
int i = byaDigitSize.length, j;
// System.out.println ("nX=" + nX + ", nY=" + nY);
do
{
i--;
// System.out.println ("Check #" + i);
nCellWidth = byaDigitSize[i];
nPuzzleWidth = 9 * nCellWidth + 1;
// System.out.println ("nCellWidth:" + nCellWidth + ", nPuzzleWidth:" + nPuzzleWidth);
} while (((nPuzzleWidth > nX) || (nPuzzleWidth > nY)) && (i > 0));
yDigitOffset = 0;
for (j = 0; j < i; j++)
yDigitOffset += byaDigitSize[j];
yTotalOffset = 0;
for (j = 0; j < byaDigitSize.length; j++)
yTotalOffset += byaDigitSize[j];
// System.out.println ("yDigitOffset:" + yDigitOffset);
try
{
imageDigits = Image.createImage ("/MSuDoKu/" + sDigit);
} catch (IOException ioe) { System.out.println ("Img not found: " + sDigit); }
return byaDigitSize[i];
}
/** Draws the background (tiling the current background image) to the given Graphics
* @param g Graphics used to draw the background
*/
protected void drawBackground (Graphics g)
{
byte bX = 0, bY = 0;
int xImgDim, yImgDim;
Image img;
if (bBack < 0)
{
int nColor = g.getColor();
g.setColor (nBack);
g.fillRect (0, 0, nWidth, nHeight);
g.setColor (nColor);
} else
{
try
{
img = Image.createImage ("/MSuDoKu/" + saBack[bBack]);
xImgDim = img.getWidth();
yImgDim = img.getHeight();
while (bY * yImgDim <= nHeight)
{
bX = 0;
while (bX * xImgDim <= nWidth)
{
g.drawImage (img, bX * xImgDim, bY * yImgDim, Graphics.TOP|Graphics.LEFT);
bX++;
};
bY++;
};
} catch (IOException ioe)
{
System.out.println ("Img not found: " + saBack[bBack]);
int nColor = g.getColor();
g.setColor (nBack);
g.fillRect (0, 0, nWidth, nHeight);
g.setColor (nColor);
}
}
}
/** Draws the puzzle skeleton (background and empty grid) to the given Graphics
* @param g Graphics used to draw the background and the grid
*/
protected void drawPuzzle (Graphics g)
{
int i, j;
int lColor;
drawBackground (g);
lColor = g.getColor();
g.setColor (nFore);
// for (i = 0; i <= 9; i++)
for (i = 1; i < 9; i++)
{
g.drawLine (xStart + i * nCellWidth, yStart, xStart + i * nCellWidth, yStart + nPuzzleWidth - 1);
g.drawLine (xStart, yStart + i * nCellWidth, xStart + nPuzzleWidth - 1, yStart + i * nCellWidth);
}
g.setColor (nForeThick);
for (i = 0; i <= 3; i++)
{
g.drawLine (xStart + 3 * i * nCellWidth, yStart, xStart + 3 * i * nCellWidth, yStart + nPuzzleWidth - 1);
g.drawLine (xStart, yStart + 3 * i * nCellWidth, xStart + nPuzzleWidth - 1, yStart + 3 * i * nCellWidth);
}
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
{
g.setColor (naaBackColor[(i / 3 + j / 3) % 2][bBack + 1]);
g.fillRect (xStart + 1 + i * nCellWidth, yStart + 1 + j * nCellWidth, nCellWidth - 1, nCellWidth - 1);
}
/*
for (i = 0; i < 9; i++)
g.drawRect (xStart + (i % 3) * 3 * nCellWidth + 1, yStart + (i / 3) * 3 * nCellWidth + 1, 3 * nCellWidth, 3 * nCellWidth);
*/
// g.drawRect (xStart + (i % 3) * 3 * nCellWidth + 1, yStart + (i / 3) * 3 * nCellWidth + 1, 3 * nCellWidth, 3 * nCellWidth);
g.setColor (lColor);
}
/** Changes image used for background
* @param b index of the new backgound image (0-saBack.length - 1)
*/
public void setBackground (byte b)
{
if (b == bBack)
return;
// if ((b >= 0) && (b < saBack.length))
if (b < saBack.length)
bBack = b;
drawPuzzle (imageBackground.getGraphics());
gPuzzle.drawImage (imageBackground, 0, 0, Graphics.TOP | Graphics.LEFT);
pmModel.writeAllNumbers();
}
/** Returns the current background
*/
public byte getBackground()
{
return bBack;
}
/** Returns the starting offset to draw the cursor in the given row or column
* @param n row or column of the cell where the cursor has to be drawn
*/
protected int getWriteOffset (int n)
{
// if ((n % 3) == 0)
// return n * nCellWidth + 2;
// else
return n * nCellWidth + 1;
}
/** Returns the width or height of the cursor to be drawn in the given row or column
* @param n row or column of the cell where the cursor hs to be drawn
*/
protected int getWriteWidth (int n)
{
// if ((n % 3) == 0)
// return nCellWidth - 2;
// else
return nCellWidth - 1;
}
/** Writes the given number in the given cell, with the given style
* @param x x coordinate of the cell where the number has to be written
* @param y y coordinate of the cell where the number has to be written
* @param n number to be written
* @param nMode style to be used to draw the number (use CORRECT, ERROR, CURSOR, CARVEDINSTONE)
* @param bRepaint true if screen has to be repainted immediately (i.e. repaint() is called)
*/
protected void writeNumber (int x, int y, int n, byte nMode, boolean bRepaint)
{
int nColorTemp;
if ((nMode & CURSOR) == 0)
{
nColorTemp = gPuzzle.getColor();
gPuzzle.setColor (naaBackColor[(x / 3 + y / 3) % 2][bBack + 1]);
gPuzzle.fillRect (xStart + getWriteOffset (x), yStart + getWriteOffset (y), getWriteWidth (x), getWriteWidth (y));
gPuzzle.setColor (nColorTemp);
// imageTemp.getGraphics().drawImage (imageBackground, - (xStart + x * nCellWidth), - (yStart + y * nCellWidth), Graphics.TOP|Graphics.LEFT);
// gPuzzle.drawImage (imageTemp, xStart + x * nCellWidth, yStart + y * nCellWidth, Graphics.TOP|Graphics.LEFT);
}
else
{
nColorTemp = gPuzzle.getColor();
gPuzzle.setColor (nCursorBack);
gPuzzle.fillRect (xStart + getWriteOffset (x), yStart + getWriteOffset (y), getWriteWidth (x), getWriteWidth (y));
gPuzzle.setColor (nColorTemp);
}
if (n > 0)
{
gPuzzle.setClip (xStart + getWriteOffset (x), yStart + getWriteOffset (y), getWriteWidth (x), getWriteWidth (y));
if ((nMode & CARVEDINSTONE) > 0)
gPuzzle.drawImage (imageDigits, xStart + x * nCellWidth + 1 - (n - 1) * nCellWidth - 1, yStart + y * nCellWidth + 1 - yDigitOffset - 1, Graphics.TOP|Graphics.LEFT);
else
gPuzzle.drawImage (imageDigits, xStart + x * nCellWidth + 1 - (n - 1) * nCellWidth - 1, yStart + y * nCellWidth + 1 - yDigitOffset - yTotalOffset - 1, Graphics.TOP|Graphics.LEFT);
gPuzzle.setClip (0, 0, nWidth, nHeight);
}
if (bRepaint)
{
repaint (xStart + getWriteOffset (x), yStart + getWriteOffset (y), getWriteWidth (x), getWriteWidth (y));
serviceRepaints();
}
}
/** Writes the given number in the given cell, with the given style
* @param x x coordinate of the cell where the number has to be written
* @param y y coordinate of the cell where the number has to be written
* @param n number to be written
* @param nMode style to be used to draw the number (use CORRECT, ERROR, CURSOR, CARVEDINSTONE)
*/
protected void writeNumber (int x, int y, int n, byte nMode)
{
writeNumber (x, y, n, nMode, true);
}
/** Repaints back the game grid after showing the solution
*/
public void repaintBoard()
{
gPuzzle.drawImage (imageBackground, 0, 0, Graphics.TOP | Graphics.LEFT);
pmModel.writeAllNumbers();
bShowingSolution = false;
}
/** Shows the solution
*/
public void showSolution()
{
bShowingSolution = true;
pmModel.writeSolution();
}
protected void paint(Graphics g) {
/* JDB --> */
if(imagePuzzle != null)
{
/* <-- JDB */
g.drawImage (imagePuzzle, 0, 0, Graphics.TOP|Graphics.LEFT);
/* JDB --> */
}
/* <-- JDB */
// drawNumbers(PuzzleModel)...
}
protected void keyReleased (int keyCode)
{
// Debugging info printed for platform-dependent softkeys
// System.out.println ("Key: " + keyCode + ", " + getGameAction (keyCode));
if (bShowingSolution)
return;
pmModel.handleEvent (keyCode);
}
protected void keyRepeated (int keyCode)
{
if (!bShowingSolution && (getGameAction(keyCode) != 0))
pmModel.handleEvent (keyCode);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -