0
|
1 /*
|
|
2 * Ristipolku
|
|
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
|
|
4 *
|
|
5 * Ohjelmointiprojekti Java-kurssille.
|
|
6 */
|
|
7 import java.awt.*;
|
|
8 import java.awt.geom.*;
|
1
|
9 import java.awt.event.*;
|
0
|
10 import javax.swing.*;
|
1
|
11 import java.util.*;
|
|
12 import game.*;
|
|
13
|
|
14 class PathInfo
|
|
15 {
|
|
16 public int in, inX, inY, out, outX, outY;
|
|
17
|
|
18 public PathInfo(in, inX, inY, out, outX, outY)
|
|
19 {
|
|
20 this.in = in;
|
|
21 this.inX = inX;
|
|
22 this.inY = inY;
|
|
23
|
|
24 this.out = out;
|
|
25 this.outX = outX;
|
|
26 this.outY = outY;
|
|
27 }
|
|
28 }
|
0
|
29
|
|
30
|
1
|
31 class GameBoard
|
0
|
32 {
|
1
|
33 public static final int boardSize = 9;
|
|
34 public static final int boardMiddle = 4;
|
|
35 Piece[][] board;
|
|
36 Piece current;
|
|
37
|
|
38 public GameBoard()
|
|
39 {
|
|
40 board = new Piece[boardSize][boardSize];
|
|
41 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
|
|
42 moveX = boardMiddle;
|
|
43 moveY = boardMiddle - 1;
|
|
44
|
|
45
|
|
46 }
|
0
|
47
|
1
|
48 public void paint(Graphics2D g, int sx, int sy, double scale)
|
|
49 {
|
|
50 for (int y = 0; y < boardSize; y++)
|
|
51 for (int x = 0; x < boardSize; x++)
|
|
52 {
|
|
53 if (board[x][y] != null)
|
|
54 board[x][y].paint(g, sx + (x * scale), sy + (y * scale), scale - scale/10);
|
|
55 }
|
|
56 }
|
|
57
|
|
58 private boolean isEmpty(int x, int y)
|
|
59 {
|
|
60 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
|
|
61 }
|
|
62
|
|
63 private Piece getPiece(int x, int y)
|
|
64 {
|
|
65 return board[x][y];
|
|
66 }
|
|
67
|
|
68 public Point resolvePath(int x, int y, int inpoint)
|
|
69 {
|
|
70 }
|
|
71
|
|
72 public pieceRotate(boolean dir)
|
|
73 {
|
|
74 current.rotate(dir);
|
|
75 }
|
|
76
|
|
77 public void pieceFinishTurn()
|
|
78 {
|
|
79 if (current != null)
|
|
80 current.setType(PieceType.NORMAL);
|
|
81
|
|
82 if (isEmpty(cx, cy))
|
|
83 {
|
|
84 current = new Piece(PieceType.ACTIVE);
|
|
85 lauta.setPiece(cx, cy, current);
|
|
86 }
|
|
87
|
|
88 }
|
|
89 }
|
|
90
|
|
91
|
|
92 public class gfxtest extends JApplet
|
|
93 implements KeyListener
|
|
94 {
|
|
95 GameBoard lauta;
|
|
96
|
|
97 public void init()
|
|
98 {
|
|
99 getContentPane().setBackground(Color.white);
|
|
100
|
|
101 lauta = new GameBoard();
|
|
102
|
|
103 addKeyListener(this);
|
|
104 }
|
|
105
|
|
106
|
|
107 /*
|
0
|
108 public Graphics2D createGraphics2D(Dimension d) {
|
|
109 Graphics2D g2 = null;
|
|
110
|
|
111 if (bimg == null || bimg.getWidth() != d.w || bimg.getHeight() != d.h) {
|
|
112 bimg = (BufferedImage) createImage(d.w, d.h);
|
|
113 reset(d);
|
|
114 }
|
|
115
|
|
116 g2 = bimg.createGraphics();
|
|
117 g2.setBackground(getBackground());
|
|
118 g2.clearRect(0, 0, w, h);
|
|
119 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
120 RenderingHints.VALUE_ANTIALIAS_ON);
|
|
121 return g2;
|
|
122 }
|
1
|
123 */
|
0
|
124
|
1
|
125 public void paint(Graphics g)
|
|
126 {
|
|
127 Graphics2D g2 = (Graphics2D) g;
|
0
|
128
|
1
|
129 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
130 RenderingHints.VALUE_ANTIALIAS_ON);
|
0
|
131
|
1
|
132 lauta.paint(g2, 15, 15, 60);
|
|
133 }
|
0
|
134
|
|
135
|
1
|
136 public void keyPressed(KeyEvent e)
|
|
137 {
|
|
138 }
|
|
139
|
|
140 public void keyReleased(KeyEvent e)
|
|
141 {
|
|
142 }
|
|
143
|
|
144 public void keyTyped(KeyEvent e)
|
|
145 {
|
|
146 switch (event.getKeyCode())
|
|
147 {
|
|
148 case VK_LEFT:
|
|
149 case VK_UP:
|
|
150 moveRotate(false);
|
|
151 break;
|
0
|
152
|
1
|
153 case VK_RIGHT:
|
|
154 case VK_DOWN:
|
|
155 moveRotate(true);
|
|
156 break;
|
|
157
|
|
158 case VK_ENTER:
|
|
159 moveNew();
|
|
160 break;
|
|
161 }
|
|
162 }
|
0
|
163 }
|