Skip to content

Instantly share code, notes, and snippets.

@aalmiray
Created May 21, 2009 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalmiray/115315 to your computer and use it in GitHub Desktop.
Save aalmiray/115315 to your computer and use it in GitHub Desktop.
package snake
import java.awt.Color
import java.awt.Dimension
import java.awt.Graphics
import javax.swing.JPanel
import org.pushingpixels.trident.Timeline
import org.pushingpixels.trident.Timeline.RepeatBehavior
class SnakePanel extends JPanel {
private final int rows
private final int cols
private final int dim
private final SnakePanelRectangle[][] grid
SnakePanel( Map args ) {
rows = args.rows
cols = args.cols
dim = args.dim
grid = new SnakePanelRectangle[cols][rows]
(0..<cols).each { i ->
(0..<rows).each { j ->
grid[i][j] = new SnakePanelRectangle()
}
}
Timeline repaint = new Timeline.Repaint(this)
repaint.playLoop(RepeatBehavior.LOOP)
this.preferredSize = new Dimension(cols * (dim + 1), rows * (dim + 1))
int rowOld = -1
int colOld = -1
this.mouseMoved = { e ->
int column = e.x / (dim + 1)
int row = e.y / (dim + 1)
if ((column != colOld) || (row != rowOld)) {
if ((colOld >= 0) && (rowOld >= 0))
grid[colOld][rowOld].rollover = false
grid[column][row].rollover = true
}
colOld = column
rowOld = row
}
}
protected void paintComponent(Graphics g) {
g.color = Color.black
g.fillRect(0, 0, getWidth(), getHeight())
(0..<cols).each { i ->
(0..<rows).each { j ->
SnakePanelRectangle rect = grid[i][j]
Color backgr = rect.backgroundColor
if( Color.BLACK != backgr ) {
g.color = backgr
g.fillRect(i * (dim + 1), j * (dim + 1), dim, dim)
}
}
}
g.dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment