comparison Ristipolku.java @ 2:1785f66a7beb

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