[Picross-commit] SF.net SVN: picross: [26] trunk
Status: Pre-Alpha
Brought to you by:
yvan_norsa
From: <yva...@us...> - 2007-06-08 12:07:03
|
Revision: 26 http://picross.svn.sourceforge.net/picross/?rev=26&view=rev Author: yvan_norsa Date: 2007-06-08 05:07:03 -0700 (Fri, 08 Jun 2007) Log Message: ----------- hint boxes image, blocks color Modified Paths: -------------- trunk/src/picross/grid/GridUI.java Added Paths: ----------- trunk/images/hint.png Added: trunk/images/hint.png =================================================================== (Binary files differ) Property changes on: trunk/images/hint.png ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/src/picross/grid/GridUI.java =================================================================== --- trunk/src/picross/grid/GridUI.java 2007-06-08 07:41:19 UTC (rev 25) +++ trunk/src/picross/grid/GridUI.java 2007-06-08 12:07:03 UTC (rev 26) @@ -37,19 +37,20 @@ import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; -import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; -import java.awt.RenderingHints; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; +import javax.swing.ImageIcon; import javax.swing.JPanel; import org.apache.log4j.Logger; +import picross.Picross; + /** * Grid UI. * @@ -62,29 +63,11 @@ private static final long serialVersionUID = 2050855523399115878L; /** Space left before the top hints. */ - private static final int TOP_HINTS = 13; + private static final int TOP_HINTS = 10; /** Space left after the left hints. */ private static final int LEFT_HINTS = 10; - /** Width occupied by a row hint. */ - private static final int ROW_HINT_WIDTH = 10; - - /** Height occupied by a row hint. */ - private static final int ROW_HINT_HEIGHT = 15; - - /** Width occupied by a column hint. */ - private static final int COL_HINT_WIDTH = 5; - - /** Height occupied by a column hint. */ - private static final int COL_HINT_HEIGHT = 12; - - /** Outline color for the hints boxes. */ - private static final Color HINT_OUTLINE_COLOR = new Color(188, 211, 227); - - /** Background color for the hints boxes. */ - private static final Color HINT_FILL_COLOR = new Color(131, 155, 200); - /** Color of the hints text. */ private static final Color HINT_TEXT_COLOR = new Color(233, 246, 255); @@ -104,12 +87,27 @@ /** Extra space at the bottom of the grid. */ private static final int BOTTOM_SPACE = 5; - /** Text used to compute the hints boxes size. */ - private static final String SAMPLE_HINT = "42"; + /** Text used to compute the high hints boxes size. */ + private static final String HIGH_SAMPLE_HINT = "88"; - /** Diameter of the arc for hint boxes. */ - private static final int ROUND_DIAMETER = 8; + /** Text used to compute the low hints boxes size. */ + private static final String LOW_SAMPLE_HINT = "8"; + /** Hint box icon. */ + private static final String HINT_BOX_ICON = "hint.png"; + + /** Space between hint boxes. */ + private static final int HINTS_SPACE = 2; + + /** Boxes blocks color. */ + private static final Color BLOCKS_COLOR = new Color(131, 155, 200); + + /** A block width. */ + private static final int BLOCK_WIDTH = 5; + + /** A block height. */ + private static final int BLOCK_HEIGHT = 5; + /*** Static field ***/ /** Class' logger. */ @@ -156,9 +154,30 @@ /** Controller attached to this view. */ private transient GridController controller; - /** Size of the hints boxes. */ - private int hintBoxSize; + /** Hint box icon. */ + private ImageIcon hintBoxIcon; + /** X coordinate of the beginning of a top hints row. */ + private int topHintsX; + + /** Space left between two top hints. */ + private int topHintsDecal; + + /** Y coordinate of the beginning of a left hints column. */ + private int leftHintsY; + + /** Space left between two left hints. */ + private int leftHintsDecal; + + /** Used to center a hint vertically. */ + private int centerHintHeight; + + /** Used to center a low hint horizontally. */ + private int centerLowHintWidth; + + /** Used to center a high hint horizontally. */ + private int centerHighHintWidth; + /*** Constructor ***/ /** @@ -190,27 +209,35 @@ this.colData = colData; this.rowData = rowData; - // Computes the size of a hint box + // Computes the size of a hint FontRenderContext frc = new FontRenderContext(null, true, true); + + // High hint (> 10) Rectangle2D textBounds = - GridUI.HINT_FONT.getStringBounds(GridUI.SAMPLE_HINT, frc); + GridUI.HINT_FONT.getStringBounds(GridUI.HIGH_SAMPLE_HINT, frc); + int hintHeight = (int) textBounds.getHeight(); + int highHintWidth = (int) textBounds.getWidth(); - double textWidth = textBounds.getWidth(); - double textHeight = textBounds.getHeight(); + // Low hint + textBounds = GridUI.HINT_FONT.getStringBounds(GridUI.LOW_SAMPLE_HINT, + frc); + int lowHintWidth = (int) textBounds.getWidth(); - this.hintBoxSize = ((int) (textWidth > textHeight - ? textWidth : textHeight)) + 2; + this.hintBoxIcon = Picross.getImage(GridUI.HINT_BOX_ICON); + int hintBoxWidth = this.hintBoxIcon.getIconWidth(); + int hintBoxHeight = this.hintBoxIcon.getIconHeight(); + // Now computes the grid boundaries this.leftBoundary = GridUI.LEFT_HINTS - + (this.rowData[0].length * this.hintBoxSize); + + (this.rowData[0].length * hintBoxWidth); this.rightBoundary = this.leftBoundary + (this.width * GridUI.BOX_WIDTH); this.topBoundary = GridUI.TOP_HINTS - + (this.colData[0].length * this.hintBoxSize); + + (this.colData[0].length * hintBoxHeight); this.bottomBoundary = this.topBoundary + (this.height * GridUI.BOX_HEIGHT); @@ -225,6 +252,24 @@ this.height * GridUI.BOX_HEIGHT); + this.topHintsX = this.leftBoundary + + (GridUI.BOX_WIDTH / 2) - (hintBoxWidth / 2); + this.topHintsDecal = hintBoxHeight + GridUI.HINTS_SPACE; + + this.leftHintsY = this.topBoundary + + (GridUI.BOX_HEIGHT / 2) - (hintBoxHeight / 2); + + this.leftHintsDecal = hintBoxWidth + GridUI.HINTS_SPACE; + + this.centerHintHeight = + (hintBoxHeight / 2) + (hintHeight / 2); + this.centerLowHintWidth = + (hintBoxWidth / 2) - (lowHintWidth / 2); + this.centerHighHintWidth = + (hintBoxWidth / 2) - (highHintWidth / 2); + + // Contain the state of the grid + this.boxes = new GridBox[this.width][this.height]; for (int i = 0; i < this.width; i++) { @@ -259,67 +304,20 @@ protected void paintComponent(Graphics g) { super.paintComponent(g); - Rectangle clipRect = g.getClipBounds(); + Graphics newG = g.create(); - if (this.topHintsRect.intersects(clipRect)) { - Graphics2D g2d = (Graphics2D) g.create(); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); + Rectangle clipRect = newG.getClipBounds(); - //GridUI.log.debug("top hints"); - - int x = 0; - int y = GridUI.TOP_HINTS; - - for (int i = this.colData[0].length - 1; i >= 0; i--) { - x = this.leftBoundary + GridUI.COL_HINT_WIDTH; - - for (int j = 0; j < this.colData.length; j++) { - if (this.colData[j][i] != GridModel.EMPTY_HINT) { - // Center the hint - int hintX = x - + (((x + GridUI.COL_HINT_WIDTH) - x) / 2); - - this.drawColHint(g2d, this.colData[j][i], hintX, y); - } - - x += GridUI.BOX_WIDTH; - } - - y += this.hintBoxSize + 2; - } - - g2d.dispose(); + if (this.topHintsRect.intersects(clipRect)) { + this.drawTopHints(newG); } if (this.leftHintsRect.intersects(clipRect)) { - //GridUI.log.debug("left hints"); - - Graphics2D g2d = (Graphics2D) g.create(); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - - int y = this.topBoundary; - - for (int i = 0; i < this.rowData.length; i++) { - int x = 0; - - int hintY = y + GridUI.ROW_HINT_HEIGHT; - - for (int j = 0; j < this.rowData[i].length; j++) { - if (this.rowData[i][j] != GridModel.EMPTY_HINT) { - this.drawRowHint(g2d, this.rowData[i][j], x, hintY); - } - - x += this.hintBoxSize + 2; - } - - y += GridUI.BOX_HEIGHT; - } - - g2d.dispose(); + this.drawLeftHints(newG); } + // Paints the boxes + for (int i = 0; i < this.width; i++) { for (int j = 0; j < this.height; j++) { //GridUI.log.debug("currentRect : " + i + "," + j); @@ -329,9 +327,10 @@ if (currentRect.intersects(clipRect)) { if (this.boxes[i][j] == this.rollover) { this.boxes[i][j].getRolloverIcon() - .paintIcon(this, g, currentRect.x, currentRect.y); + .paintIcon(this, newG, + currentRect.x, currentRect.y); } else { - this.boxes[i][j].getIcon().paintIcon(this, g, + this.boxes[i][j].getIcon().paintIcon(this, newG, currentRect.x, currentRect.y); } @@ -339,130 +338,123 @@ } } - g.setColor(Color.RED); + // Draws the blocks + newG.setColor(GridUI.BLOCKS_COLOR); - int currentY = this.topBoundary; - int boxWidth = 5 * GridUI.BOX_WIDTH; - int boxHeight = 5 * GridUI.BOX_HEIGHT; + int boxWidth = GridUI.BLOCK_WIDTH * GridUI.BOX_WIDTH; + int boxHeight = GridUI.BLOCK_HEIGHT * GridUI.BOX_HEIGHT; int i = 0; + int currentY = this.topBoundary; while (i < this.height) { - int currentX = this.leftBoundary; int j = 0; + int currentX = this.leftBoundary; while (j < this.width) { - if ((i + 5) <= this.height) { - if ((j + 5 <= this.width)) { - g.drawRect(currentX, currentY, + if ((i + GridUI.BLOCK_HEIGHT) <= this.height) { + if ((j + GridUI.BLOCK_WIDTH <= this.width)) { + newG.drawRect(currentX, currentY, boxWidth, boxHeight); } else { - g.drawRect(currentX, currentY, + newG.drawRect(currentX, currentY, (this.width - j) * GridUI.BOX_WIDTH, boxHeight); } } else { - if ((j + 5 <= this.width)) { - g.drawRect(currentX, currentY, + if ((j + GridUI.BLOCK_WIDTH <= this.width)) { + newG.drawRect(currentX, currentY, boxWidth, (this.height - i) * GridUI.BOX_HEIGHT); } else { - g.drawRect(currentX, currentY, + newG.drawRect(currentX, currentY, (this.width - j) * GridUI.BOX_WIDTH, (this.height - i) * GridUI.BOX_HEIGHT); } } currentX += boxWidth; - j += 5; + j += GridUI.BLOCK_WIDTH; } currentY += boxHeight; - i += 5; + i += GridUI.BLOCK_HEIGHT; } + + newG.dispose(); } /*** Methods ***/ /** - * Draws a column hint. + * Draws the top hints. * - * @param g2d the graphic context - * @param value hint value - * @param x X coordinate - * @param y Y coordinate + * @param g the graphics context */ - private void drawColHint(Graphics2D g2d, int value, int x, int y) { - this.drawHintBox(g2d, x, y, GridUI.COL_HINT_HEIGHT); - this.setHintPen(g2d); + private void drawTopHints(Graphics g) { + int y = 0; - if (value < 10) { - // Tries to center the hint text - g2d.drawString(String.valueOf(value), - x + (this.hintBoxSize / 3), - y); - } else { - g2d.drawString(String.valueOf(value), - x, - y); + for (int i = this.colData[0].length - 1; i >= 0; i--) { + int x = this.topHintsX; + + for (int j = 0; j < this.colData.length; j++) { + if (this.colData[j][i] != GridModel.EMPTY_HINT) { + this.drawHint(g, this.colData[j][i], x, y); + } + + x += GridUI.BOX_WIDTH; + } + + y += this.topHintsDecal; } } /** - * Draws a row hint. + * Draws the left hints. * - * @param g2d the graphic context - * @param value hint value - * @param x X coordinate - * @param y Y coordinate + * @param g the graphics context */ - private void drawRowHint(Graphics2D g2d, int value, int x, int y) { - this.drawHintBox(g2d, x, y, GridUI.ROW_HINT_HEIGHT); - this.setHintPen(g2d); + private void drawLeftHints(Graphics g) { + int y = this.leftHintsY; - if (value < 10) { - g2d.drawString(String.valueOf(value), - x + (this.hintBoxSize / 3) - 1, - y - 3); - } else { - g2d.drawString(String.valueOf(value), - x + 1, - y - 3); + 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] != GridModel.EMPTY_HINT) { + this.drawHint(g, this.rowData[i][j], x, y); + } + + x += this.leftHintsDecal; + } + + y += GridUI.BOX_HEIGHT; } } /** - * Draws a hint box. + * Draws a hint. * - * @param g2d the graphic context + * @param g the graphics context + * @param value hint value * @param x X coordinate * @param y Y coordinate - * @param hintHeight the hint height */ - private void drawHintBox(Graphics2D g2d, int x, int y, int hintHeight) { - g2d.setColor(GridUI.HINT_FILL_COLOR); - g2d.fillRoundRect(x, - y - hintHeight, - this.hintBoxSize, - this.hintBoxSize, - GridUI.ROUND_DIAMETER, GridUI.ROUND_DIAMETER); + private void drawHint(Graphics g, int value, int x, int y) { + this.hintBoxIcon.paintIcon(this, g, x, y); - g2d.setColor(GridUI.HINT_OUTLINE_COLOR); - g2d.drawRoundRect(x, - y - hintHeight, - this.hintBoxSize, - this.hintBoxSize, - GridUI.ROUND_DIAMETER, GridUI.ROUND_DIAMETER); - } + g.setColor(GridUI.HINT_TEXT_COLOR); + g.setFont(GridUI.HINT_FONT); - /** - * Sets the hint color and font. - * - * @param g2d the graphic context - */ - private void setHintPen(Graphics2D g2d) { - g2d.setColor(GridUI.HINT_TEXT_COLOR); - g2d.setFont(GridUI.HINT_FONT); + y += this.centerHintHeight; + + if (value < 10) { + x += this.centerLowHintWidth; + g.drawString(String.valueOf(value), x, y); + } else { + x += this.centerHighHintWidth; + g.drawString(String.valueOf(value), x, y); + } } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |