[Picross-commit] SF.net SVN: picross: [18] trunk
Status: Pre-Alpha
Brought to you by:
yvan_norsa
From: <yva...@us...> - 2007-06-06 19:14:24
|
Revision: 18 http://picross.svn.sourceforge.net/picross/?rev=18&view=rev Author: yvan_norsa Date: 2007-06-06 12:14:19 -0700 (Wed, 06 Jun 2007) Log Message: ----------- added rollover icons Modified Paths: -------------- trunk/src/picross/PicrossMediator.java trunk/src/picross/grid/Box.java trunk/src/picross/grid/PicrossGridController.java trunk/src/picross/grid/PicrossGridModel.java trunk/src/picross/grid/PicrossGridUI.java trunk/src/picross/menus/MainMenuUI.java Added Paths: ----------- trunk/images/checked-rollover.png trunk/images/empty-rollover.png trunk/src/picross/grid/GridBox.java Added: trunk/images/checked-rollover.png =================================================================== (Binary files differ) Property changes on: trunk/images/checked-rollover.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/images/empty-rollover.png =================================================================== (Binary files differ) Property changes on: trunk/images/empty-rollover.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/src/picross/PicrossMediator.java =================================================================== --- trunk/src/picross/PicrossMediator.java 2007-06-06 15:08:52 UTC (rev 17) +++ trunk/src/picross/PicrossMediator.java 2007-06-06 19:14:19 UTC (rev 18) @@ -38,7 +38,7 @@ import fr.cle.mmvcs.Mediateur; import fr.cle.mmvcs.SimpleEvent; -import org.apache.log4j.Logger; +//import org.apache.log4j.Logger; import picross.grid.PicrossGridController; import picross.grid.PicrossGridMediator; @@ -55,7 +55,7 @@ /*** Static field ***/ /** Class' logger. */ - private static Logger log = Logger.getLogger(PicrossMediator.class); + //private static Logger log = Logger.getLogger(PicrossMediator.class); /*** Fields ***/ @@ -84,7 +84,7 @@ /** {@inheritDoc} */ public void eventPerformed(SimpleEvent e) { - PicrossMediator.log.debug("eventPerformed(" + e + ")"); + //PicrossMediator.log.debug("eventPerformed(" + e + ")"); String cmd = e.getCommandName(); Modified: trunk/src/picross/grid/Box.java =================================================================== --- trunk/src/picross/grid/Box.java 2007-06-06 15:08:52 UTC (rev 17) +++ trunk/src/picross/grid/Box.java 2007-06-06 19:14:19 UTC (rev 18) @@ -45,26 +45,20 @@ package picross.grid; -import java.awt.Image; - -import java.util.HashMap; -import java.util.Map; import java.util.Random; //import org.apache.log4j.Logger; -import picross.Picross; - /** * Representation of a box in the grid. * * @author Y. Norsa */ -final class Box { +class Box { /*** Enum ***/ /** Possibles states of a box. */ - private enum BoxState { + enum BoxState { /** An empty box. */ EMPTY, @@ -75,11 +69,6 @@ CROSSED } - /*** Constant ***/ - - /** Images files extension. */ - private static final String IMAGES_EXT = ".png"; - /*** Statics fields ***/ /** The class's logger. */ @@ -88,37 +77,25 @@ /** Random number generator. */ private static Random rand; - /** Map containing the images corresponding to the different states. */ - private static Map<Box.BoxState, Image> images; - /*** Fields ***/ /** State of the box. */ - private Box.BoxState state; + protected Box.BoxState state; /** Pseudo random hash-code. */ - private int hash; + private final int hash; /*** Static block ***/ - // Fills in the images map static { Box.rand = new Random(); - - Box.images = new HashMap<Box.BoxState, Image>(); - - for (Box.BoxState state : Box.BoxState.values()) { - Box.images.put(state, Picross - .getImage(state.toString().toLowerCase() - + Box.IMAGES_EXT).getImage()); - } } /*** Constructor ***/ /** Constructor. */ Box() { - this.state = Box.BoxState.EMPTY; + this.empty(); this.hash = Box.rand.nextInt(); } @@ -153,7 +130,7 @@ * * @return boolean telling if the box is checked */ - boolean isChecked() { + final boolean isChecked() { return this.state == Box.BoxState.CHECKED; } @@ -162,7 +139,7 @@ * * @return boolean telling if the box is crossed */ - boolean isCrossed() { + final boolean isCrossed() { return this.state == Box.BoxState.CROSSED; } @@ -171,33 +148,23 @@ * * @return boolean telling if the box is empty */ - boolean isEmpty() { + final boolean isEmpty() { return this.state == Box.BoxState.EMPTY; } /** Empties the box. */ - void empty() { + final void empty() { this.state = Box.BoxState.EMPTY; } /** Checks the box. */ - void check() { + final void check() { this.state = Box.BoxState.CHECKED; } /** Crosses the box. */ - void cross() { + final void cross() { this.state = Box.BoxState.CROSSED; } - - /** - * Returns the image associated with the box's state. - * - * @return the image corresponding to the box's state - */ - Image getImage() { - //Box.log.debug("return " + Box.images.get(this.state)); - return Box.images.get(this.state); - } } Added: trunk/src/picross/grid/GridBox.java =================================================================== --- trunk/src/picross/grid/GridBox.java (rev 0) +++ trunk/src/picross/grid/GridBox.java 2007-06-06 19:14:19 UTC (rev 18) @@ -0,0 +1,111 @@ +package picross.grid; + +import java.awt.Rectangle; + +import java.util.HashMap; +import java.util.Map; + +import javax.swing.ImageIcon; + +import picross.Picross; + +/** + * Graphical representation of a box in the grid. + * + * @author Y. Norsa + */ +final class GridBox extends Box { + /*** Constants ***/ + + /** Index of the normal icon. */ + private static final int ICON_INDEX = 0; + + /** Index of the rollover icon. */ + private static final int ROLLOVER_ICON_INDEX = 1; + + /** Suffix for rollover icons. */ + private static final String ROLLOVER_NAME = "-rollover"; + + /** Images files extension. */ + private static final String IMAGES_EXT = ".png"; + + /*** Static field ***/ + + /** Map containing the images corresponding to the different states. */ + private static Map<Box.BoxState, ImageIcon[]> images; + + /*** Field ***/ + + /** Rectangle occupied by the box. */ + private Rectangle rect; + + // Fills in the images map + static { + /* + * We create a too large HashMap so it doesn't grow + * during its initialisation + */ + GridBox.images = new HashMap<Box.BoxState, ImageIcon[]>(7); + + for (Box.BoxState state : Box.BoxState.values()) { + ImageIcon[] img = new ImageIcon[2]; + + String stateImageName = state.toString().toLowerCase(); + img[GridBox.ICON_INDEX] = Picross.getImage(stateImageName + + GridBox.IMAGES_EXT); + img[GridBox.ROLLOVER_ICON_INDEX] = + Picross.getImage(stateImageName + GridBox.ROLLOVER_NAME + + GridBox.IMAGES_EXT); + + GridBox.images.put(state, img); + } + + // We don't have a rollover icon for crossed boxes + GridBox.images.get(Box.BoxState.CROSSED)[GridBox.ROLLOVER_ICON_INDEX] = + GridBox.images.get(Box.BoxState.CROSSED)[GridBox.ICON_INDEX]; + } + + /*** Constructor ***/ + + /** + * Constructor. + * + * @param rect rectangle occupied by this box + */ + GridBox(Rectangle rect) { + super(); + + this.rect = rect; + } + + /*** Methods ***/ + + /** + * Returns the icon representing the current state of the box. + * + * @return icon of the state of the box + */ + ImageIcon getIcon() { + return GridBox.images.get(this.state)[GridBox.ICON_INDEX]; + } + + /** + * Returns the rollover icon representing the current state of the box. + * + * @return rollover icon of the state of the box + */ + ImageIcon getRolloverIcon() { + return GridBox.images.get(this.state)[GridBox.ROLLOVER_ICON_INDEX]; + } + + /*** Accessor ***/ + + /** + * Returns this box' rectangle. + * + * @return rectangle occupied by this box + */ + Rectangle getRect() { + return this.rect; + } +} Property changes on: trunk/src/picross/grid/GridBox.java ___________________________________________________________________ Name: svn:keywords + Id Modified: trunk/src/picross/grid/PicrossGridController.java =================================================================== --- trunk/src/picross/grid/PicrossGridController.java 2007-06-06 15:08:52 UTC (rev 17) +++ trunk/src/picross/grid/PicrossGridController.java 2007-06-06 19:14:19 UTC (rev 18) @@ -127,7 +127,9 @@ public void mouseExited(MouseEvent e) { } /** {@inheritDoc} */ - public void mousePressed(MouseEvent e) { } + public void mousePressed(MouseEvent e) { + this.view.rolloverEnded(); + } /** {@inheritDoc} */ public void mouseReleased(MouseEvent e) { @@ -144,8 +146,22 @@ } /** {@inheritDoc} */ - public void mouseMoved(MouseEvent e) { } + public void mouseMoved(MouseEvent e) { + //PicrossGridController.log.debug(e.getPoint()); + Point point = e.getPoint(); + + if (this.view.isInGrid(point)) { + int row = this.view.getRow(point); + int column = this.view.getColumn(point); + + this.view.setRollover(row, column); + } else { + this.view.rolloverEnded(); + } + + } + /*** Methods ***/ /** Modified: trunk/src/picross/grid/PicrossGridModel.java =================================================================== --- trunk/src/picross/grid/PicrossGridModel.java 2007-06-06 15:08:52 UTC (rev 17) +++ trunk/src/picross/grid/PicrossGridModel.java 2007-06-06 19:14:19 UTC (rev 18) @@ -284,7 +284,7 @@ this.mediator.check(row, column, PicrossGridController.CROSS_ACTION); } - } else { //if (this.boxes[row][column].isCrossed())\xA0{ + } else { //if (this.boxes[row][column].isCrossed()) { //PicrossGridModel.log.debug("crossed"); if (type == PicrossGridController.CROSS_ACTION) { Modified: trunk/src/picross/grid/PicrossGridUI.java =================================================================== --- trunk/src/picross/grid/PicrossGridUI.java 2007-06-06 15:08:52 UTC (rev 17) +++ trunk/src/picross/grid/PicrossGridUI.java 2007-06-06 19:14:19 UTC (rev 18) @@ -37,10 +37,11 @@ import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; +import java.awt.Rectangle; import javax.swing.JPanel; -//import org.apache.log4j.Logger; +import org.apache.log4j.Logger; /** * Grid UI. @@ -83,7 +84,7 @@ /*** Static field ***/ /** Class' logger. */ - //private static Logger log = Logger.getLogger(PicrossGridUI.class); + private static Logger log = Logger.getLogger(PicrossGridUI.class); /*** Fields ***/ @@ -111,9 +112,18 @@ /** Position of the bottom end of the grid. */ private int bottomBoundary; + /** Rectangle occupied by the top hints. */ + private Rectangle topHintsRect; + + /** Rectangle occupied by the left hints. */ + private Rectangle leftHintsRect; + /** Current state of the grid. */ - private Box[][] boxes; + private GridBox[][] boxes; + /** Current rolled-over box. */ + private transient GridBox rollover; + /** Controller attached to this view. */ private transient PicrossGridController controller; @@ -132,7 +142,7 @@ int[][] colData, int[][] rowData, PicrossGridController controller) { - super(); + super(true); this.controller = controller; @@ -148,14 +158,6 @@ this.colData = colData; this.rowData = rowData; - this.boxes = new Box[this.width][this.height]; - - for (int i = 0; i < this.boxes.length; i++) { - for (int j = 0; j < this.boxes[i].length; j++) { - this.boxes[i][j] = new Box(); - } - } - this.leftBoundary = this.rowData[0].length * PicrossGridUI.ROW_HINT_WIDTH; @@ -168,6 +170,33 @@ this.bottomBoundary = this.topBoundary + (this.height * PicrossGridUI.BOX_HEIGHT); + this.topHintsRect = new Rectangle(this.leftBoundary, 0, + this.width * PicrossGridUI.BOX_WIDTH, + this.topBoundary); + this.leftHintsRect = new Rectangle(0, this.topBoundary, + this.leftBoundary, + this.height + * PicrossGridUI.BOX_HEIGHT); + + this.boxes = new GridBox[this.width][this.height]; + + for (int i = 0; i < this.boxes.length; i++) { + for (int j = 0; j < this.boxes[i].length; j++) { + /* + * We compute here the rectangle corresponding to each box + * so we'll be able + * to redraw only what is needed + */ + this.boxes[i][j] = + new GridBox(new Rectangle(this.leftBoundary + + (j * PicrossGridUI.BOX_WIDTH), + this.topBoundary + + (i * PicrossGridUI.BOX_WIDTH), + PicrossGridUI.BOX_WIDTH, + PicrossGridUI.BOX_HEIGHT)); + } + } + //PicrossGridUI.log.debug("rightBoundary : " + this.rightBoundary); //PicrossGridUI.log.debug("bottomBoundary : " + this.bottomBoundary); @@ -180,55 +209,69 @@ /*** Method overloaded from JPanel ***/ /** {@inheritDoc} */ - public void paintComponent(Graphics g) { + protected void paintComponent(Graphics g) { super.paintComponent(g); - int x = 0; - int y = PicrossGridUI.TOP_HINTS; + Rectangle clipRect = g.getClipBounds(); - for (int i = this.colData[0].length - 1; i >= 0; i--) { - x = this.leftBoundary + PicrossGridUI.COL_HINT_WIDTH; + if (this.topHintsRect.intersects(clipRect)) { + PicrossGridUI.log.debug("top hints"); - for (int j = 0; j < this.colData.length; j++) { - if (this.colData[j][i] != PicrossGridModel.EMPTY_HINT) { - g.drawString(String.valueOf(this.colData[j][i]), - x, y); + int x = 0; + int y = PicrossGridUI.TOP_HINTS; + + for (int i = this.colData[0].length - 1; i >= 0; i--) { + x = this.leftBoundary + PicrossGridUI.COL_HINT_WIDTH; + + for (int j = 0; j < this.colData.length; j++) { + if (this.colData[j][i] != PicrossGridModel.EMPTY_HINT) { + g.drawString(String.valueOf(this.colData[j][i]), + x, y); + } + + x += PicrossGridUI.BOX_WIDTH; } - x += PicrossGridUI.BOX_WIDTH; + y += PicrossGridUI.COL_HINT_HEIGHT; } - - y += PicrossGridUI.COL_HINT_HEIGHT; } - int gridY = y; + if (this.leftHintsRect.intersects(clipRect)) { + PicrossGridUI.log.debug("left hints"); - for (int i = 0; i < this.rowData.length; i++) { - x = 0; + int y = this.topBoundary; - for (int j = 0; j < this.rowData[i].length; j++) { - if (this.rowData[i][j] != PicrossGridModel.EMPTY_HINT) { - g.drawString(String.valueOf(this.rowData[i][j]), - x, y + PicrossGridUI.ROW_HINT_HEIGHT); + for (int i = 0; i < this.rowData.length; i++) { + int x = 0; + + for (int j = 0; j < this.rowData[i].length; j++) { + if (this.rowData[i][j] != PicrossGridModel.EMPTY_HINT) { + g.drawString(String.valueOf(this.rowData[i][j]), + x, y + PicrossGridUI.ROW_HINT_HEIGHT); + } + + x += PicrossGridUI.ROW_HINT_WIDTH; } - x += PicrossGridUI.ROW_HINT_WIDTH; + y += PicrossGridUI.BOX_HEIGHT; } - - y += PicrossGridUI.BOX_HEIGHT; } - y = gridY; - for (int i = 0; i < this.height; i++) { - x = this.leftBoundary; + for (int j = 0; j < this.width; j++) { + Rectangle currentRect = this.boxes[i][j].getRect(); - for (int j = 0; j < this.width; j++) { - g.drawImage(this.boxes[i][j].getImage(), x, y, null); - x += PicrossGridUI.BOX_WIDTH; + if (currentRect.intersects(clipRect)) { + if (this.boxes[i][j] == this.rollover) { + this.boxes[i][j].getRolloverIcon() + .paintIcon(this, g, currentRect.x, currentRect.y); + } else { + this.boxes[i][j].getIcon().paintIcon(this, g, + currentRect.x, + currentRect.y); + } + } } - - y += PicrossGridUI.BOX_HEIGHT; } } @@ -319,7 +362,35 @@ } } - this.repaint(); + this.repaint(this.boxes[row][column].getRect()); } + + /** + * Allows to set the current rolled-over box. + * + * @param row row of the box + * @param column column of the box + */ + void setRollover(int row, int column) { + //PicrossGridUI.log.debug("setRollover(" + row + ", " + column + ")"); + + this.rolloverEnded(); + this.rollover = this.boxes[row][column]; + this.repaint(this.rollover.getRect()); + } + + /** Indicates that no box is currently rolled over. */ + void rolloverEnded() { + if (this.rollover != null) { + /* + * Save the old rolled-over box so we can draw it + * in its initial state + */ + Rectangle rect = this.rollover.getRect(); + + this.rollover = null; + this.repaint(rect); + } + } } Modified: trunk/src/picross/menus/MainMenuUI.java =================================================================== --- trunk/src/picross/menus/MainMenuUI.java 2007-06-06 15:08:52 UTC (rev 17) +++ trunk/src/picross/menus/MainMenuUI.java 2007-06-06 19:14:19 UTC (rev 18) @@ -121,7 +121,7 @@ /*** Method overloaded from the class JPanel ***/ /** {@inheritDoc} */ - public void paintComponent(Graphics g) { + protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(this.image, 0, 0, null); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |