Skip to content

Commit 3d50447

Browse files
committed
added BatGirl Puzzle Animation w/Jim
1 parent 3f19f99 commit 3d50447

File tree

6 files changed

+215
-142
lines changed

6 files changed

+215
-142
lines changed
255 Bytes
Binary file not shown.
Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,93 @@
11
package org.teachingkidsprogramming.section08tdd;
22

3-
import java.awt.Color;
3+
import java.awt.Graphics;
44
import java.awt.Graphics2D;
5+
import java.awt.Point;
6+
import java.util.ArrayList;
7+
import java.util.List;
58

69
import javax.swing.JPanel;
710

8-
import org.teachingextensions.logo.Paintable;
911
import org.teachingextensions.logo.PenColors;
10-
import org.teachingextensions.windows.ProgramWindow;
1112

12-
public class PuzzleBoard extends JPanel implements Paintable
13+
public class PuzzleBoard extends JPanel
1314
{
14-
private static final long serialVersionUID = 1L;
15-
public void addTo(ProgramWindow panel)
15+
private static final long serialVersionUID = -3592444274530147326L;
16+
private List<Tile> tiles;
17+
private List<Point> positions;
18+
public PuzzleBoard()
1619
{
17-
panel.addPaintable(this);
20+
this.positions = createPositions();
21+
this.tiles = createTiles(this.positions);
22+
}
23+
private static List<Point> createPositions()
24+
{
25+
ArrayList<Point> p = new ArrayList<Point>(9);
26+
Point point = null;
27+
for (int i = 0; i < 9; i++)
28+
{
29+
if (i < 3)
30+
{
31+
point = new Point(35, 35 + (127 * i));
32+
}
33+
else if (i < 6)
34+
{
35+
point = new Point(162, 35 + (127 * (i - 3)));
36+
}
37+
else
38+
{
39+
point = new Point(289, 35 + (127 * (i - 6)));
40+
}
41+
if (point != null)
42+
{
43+
p.add(point);
44+
}
45+
point = null;
46+
}
47+
return p;
48+
}
49+
private static List<Tile> createTiles(List<Point> positions)
50+
{
51+
ArrayList<Tile> t = new ArrayList<Tile>(9);
52+
for (int i = 0; i < 8; i++)
53+
{
54+
t.add(new Tile(i, positions.get(i)));
55+
}
56+
return t;
1857
}
1958
@Override
20-
public void paint(Graphics2D g, JPanel caller)
59+
protected void paintComponent(Graphics g)
60+
{
61+
super.paintComponent(g);
62+
drawBorder(g);
63+
drawField(g);
64+
drawTiles(g);
65+
}
66+
private void drawTiles(Graphics g)
67+
{
68+
Graphics2D g2d = (Graphics2D) g.create();
69+
for (Tile tile : tiles)
70+
{
71+
tile.paint(g2d);
72+
}
73+
g2d.dispose();
74+
}
75+
private void drawField(Graphics g)
2176
{
22-
Color color2 = PenColors.Blues.DarkBlue;
23-
g.setColor(color2);
24-
g.fillRect(20, 20, 410, 410);
2577
g.setColor(PenColors.Blues.SkyBlue);
2678
g.fillRect(30, 30, 386, 386);
2779
}
28-
}
29-
//be aware of time, ex. every second change the x by a certain value (animates)
30-
//be able to hold on to a tile so that it can be manipulated
80+
private void drawBorder(Graphics g)
81+
{
82+
g.setColor(PenColors.Blues.DarkBlue);
83+
g.fillRect(20, 20, 410, 410);
84+
}
85+
public Tile getPiece(int i)
86+
{
87+
return this.tiles.get(i);
88+
}
89+
public List<Point> getPositions()
90+
{
91+
return new ArrayList<Point>(positions);
92+
}
93+
}
Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
package org.teachingkidsprogramming.section08tdd;
22

3-
public class PuzzleSolver
3+
import java.awt.Point;
4+
5+
import javax.swing.SwingUtilities;
6+
7+
public class PuzzleSolver implements Runnable
48
{
5-
//this problem is an ArraySort w/ custom rules
6-
//the ArraySort (square) controls which positions can be swapped with each other
7-
//must determine 'where is the blank (square)?'
8-
//must determine 'where to move the blank to get the array more sorted than it is?'
9-
//this is a type A* problem (AI) -
10-
//generate all permutations and check to see which ones are valid (evaluate costs)
11-
//throw away all non-valid choices (highest costs)
12-
//keep track of the history of cost
13-
//develop a heuristic for estimated cost of possible actions
14-
//
15-
//NOTE: this is a kata (higher level instructions)
16-
//part of the exercise is to translate into line-by-line English, THEN Java
17-
//
18-
//for more complete directions see this page
19-
//https://www.penflip.com/lynnlangit/tkp-lesson-plans/blob/master/course09.txt
20-
//complex example -- http://www.brian-borowski.com/software/puzzle/
21-
//http://en.wikipedia.org/wiki/File:Batgirl.gif
22-
//
23-
public PuzzleWindow puzzleWindow;
24-
public PuzzleSolver()
9+
private PuzzleBoard board;
10+
public PuzzleSolver(PuzzleBoard board)
2511
{
26-
puzzleWindow = new PuzzleWindow("BatGirl Puzzle");
27-
PuzzleBoard jboard = new PuzzleBoard();
28-
jboard.addTo(puzzleWindow);
29-
puzzleWindow.setVisible(true);
12+
this.board = board;
3013
}
31-
public static void main(String[] args)
14+
public void run()
3215
{
33-
new PuzzleSolver();
16+
final PuzzleBoard b = this.board;
17+
Point target = b.getPositions().get(8);
18+
while (b.isVisible())
19+
{
20+
SwingUtilities.invokeLater(new Runnable()
21+
{
22+
public void run()
23+
{
24+
b.repaint();
25+
}
26+
});
27+
Tile piece = b.getPiece(7);
28+
if (!piece.isMovingTo(target))
29+
{
30+
piece.moveTo(target);
31+
}
32+
if (!piece.isAt(target))
33+
{
34+
piece.step();
35+
}
36+
try
37+
{
38+
Thread.sleep(10);
39+
}
40+
catch (InterruptedException e)
41+
{
42+
}
43+
}
3444
}
35-
//repaint the window every second
3645
}
Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,18 @@
11
package org.teachingkidsprogramming.section08tdd;
22

3-
import java.awt.Graphics;
4-
import java.awt.Graphics2D;
5-
import java.awt.Image;
6-
import java.net.URL;
7-
import java.util.Calendar;
8-
9-
import javax.swing.ImageIcon;
3+
import java.awt.BorderLayout;
104

115
import org.teachingextensions.windows.ProgramWindow;
126

137
public class PuzzleWindow extends ProgramWindow
148
{
15-
private int y = 35;
16-
private String[] tiles = {"Batgirl1a.png",
17-
"Batgirl1b.png",
18-
"Batgirl1c.png",
19-
"Batgirl2a.png",
20-
"Batgirl2b.png",
21-
"Batgirl2c.png",
22-
"Batgirl3a.png",
23-
"Batgirl3b.png",
24-
"Batgirl3c.png" };
25-
public PuzzleWindow(String title)
26-
{
27-
super(title);
28-
}
29-
// @Override
30-
// public void paint(Graphics g)
31-
// {
32-
// super.paint(g);
33-
// for (int i = 0; i < 8; i++)
34-
// {
35-
// if (i < 3)
36-
// {
37-
// Tile t = new Tile(tiles[i], 35, y + (127 * i));
38-
// t.paint((Graphics2D) g, this);
39-
// //drawTile(g, tiles[i], 35, 35 + (127 * i));
40-
// }
41-
// else if (i < 6)
42-
// drawTile((Graphics2D) g, tiles[i], 162, y + (127 * (i - 3)));
43-
// else
44-
// {
45-
// drawTile((Graphics2D) g, tiles[i], 289, y + (127 * (i - 6)));
46-
// }
47-
// }
48-
// }
49-
private void drawTile(Graphics2D g, String tile, int x, int y)
50-
{
51-
URL imagePicture = this.getClass().getResource(tile);
52-
Image img = new ImageIcon(imagePicture).getImage();
53-
g.drawImage(img, x, y, 122, 122, null);
54-
}
55-
@Override
56-
public void update(Graphics g)
9+
public PuzzleWindow()
5710
{
58-
super.update(g);
59-
Calendar c = Calendar.getInstance();
60-
long l = c.getTimeInMillis();
61-
long m = l % 1000;
62-
if (m == 0)
63-
{
64-
y += 20;
65-
}
66-
for (int i = 0; i < 8; i++)
67-
{
68-
if (i < 3)
69-
{
70-
Tile t = new Tile(tiles[i], 35, y + (127 * i));
71-
t.paint((Graphics2D) g, this);
72-
//drawTile(g, tiles[i], 35, 35 + (127 * i));
73-
}
74-
else if (i < 6)
75-
drawTile((Graphics2D) g, tiles[i], 162, y + (127 * (i - 3)));
76-
else
77-
{
78-
drawTile((Graphics2D) g, tiles[i], 289, y + (127 * (i - 6)));
79-
}
80-
}
11+
super("Puzzle");
12+
this.setLayout(new BorderLayout());
8113
}
82-
}
14+
/**
15+
*
16+
*/
17+
private static final long serialVersionUID = -1526978082665818880L;
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.teachingkidsprogramming.section08tdd;
2+
3+
import java.awt.EventQueue;
4+
5+
import javax.swing.UIManager;
6+
import javax.swing.UnsupportedLookAndFeelException;
7+
8+
public class SimplePuzzle implements Runnable
9+
{
10+
public static void main(String[] args)
11+
{
12+
EventQueue.invokeLater(new SimplePuzzle());
13+
}
14+
public void run()
15+
{
16+
this.setLookAndFeel();
17+
PuzzleBoard board = new PuzzleBoard();
18+
PuzzleWindow pw = new PuzzleWindow();
19+
pw.add(board);
20+
pw.setVisible(true);
21+
new Thread(new PuzzleSolver(board)).start();
22+
}
23+
private void setLookAndFeel()
24+
{
25+
try
26+
{
27+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
28+
}
29+
catch (ClassNotFoundException ex)
30+
{
31+
}
32+
catch (InstantiationException ex)
33+
{
34+
}
35+
catch (IllegalAccessException ex)
36+
{
37+
}
38+
catch (UnsupportedLookAndFeelException ex)
39+
{
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)