comparison Ristipolku.java @ 7:70714c229e23

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 20:23:21 +0200
parents be0bf7544069
children d8e7fd8f3ccf
comparison
equal deleted inserted replaced
6:be0bf7544069 7:70714c229e23
5 * Ohjelmointiprojekti 2010-2011 Java-kurssille T740306. 5 * Ohjelmointiprojekti 2010-2011 Java-kurssille T740306.
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 java.awt.image.*;
11 import java.awt.event.*;
12 import javax.swing.*;
11 import javax.imageio.*; 13 import javax.imageio.*;
12 import java.util.*; 14 import java.util.*;
13 import java.io.*; 15 import java.io.*;
14 import game.*; 16 import game.*;
15 17
35 { 37 {
36 public static final int boardSize = 9; 38 public static final int boardSize = 9;
37 public static final int boardMiddle = 4; 39 public static final int boardMiddle = 4;
38 Piece[][] board; 40 Piece[][] board;
39 Piece current; 41 Piece current;
40 42
43 int moveX, moveY, movePoint;
44
41 public GameBoard() 45 public GameBoard()
42 { 46 {
43 board = new Piece[boardSize][boardSize]; 47 board = new Piece[boardSize][boardSize];
44 48
45 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); 49 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
46 50
47 moveX = boardMiddle; 51 moveX = boardMiddle;
48 moveY = boardMiddle - 1; 52 moveY = boardMiddle - 1;
53 movePoint = 0;
49 } 54 }
50 55
51 public void paint(Graphics2D g, int sx, int sy, double scale) 56 public void paint(Graphics2D g, int sx, int sy, float scale)
52 { 57 {
53 for (int y = 0; y < boardSize; y++) 58 for (int y = 0; y < boardSize; y++)
54 for (int x = 0; x < boardSize; x++) 59 for (int x = 0; x < boardSize; x++)
55 if (board[x][y] != null) 60 if (board[x][y] != null)
56 { 61 {
75 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize) 80 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize)
76 return board[x][y]; 81 return board[x][y];
77 else 82 else
78 return null; 83 return null;
79 } 84 }
80 85
81 public Point resolvePath(int x, int y, int inpoint)
82 {
83 }
84
85 public void pieceRotate(boolean dir) 86 public void pieceRotate(boolean dir)
86 { 87 {
87 current.rotate(dir); 88 current.rotate(dir);
88 } 89 }
89 90
91 public PathInfo resolvePath(int startX, int startY, int startPoint)
92 {
93 int x = startX, y = startY;
94 int point;
95
96 Piece curr = getPiece(startX, startY);
97 if (curr == null)
98 return null;
99
100 while (curr != null)
101 {
102
103 }
104
105 return new PathInfo(startPoint, startX, startY, point, x, y);
106 }
107
90 public void pieceFinishTurn() 108 public void pieceFinishTurn()
91 { 109 {
92 if (current != null) 110 if (current != null)
111 {
93 current.setType(PieceType.LOCKED); 112 current.setType(PieceType.LOCKED);
94 113 PathInfo i = resolvePath(moveX, moveY, movePoint);
95 if (isEmpty(cx, cy)) 114
115 if (i != null)
116 {
117
118 }
119 }
120
121 if (isEmpty(moveX, moveY))
96 { 122 {
97 current = new Piece(PieceType.ACTIVE); 123 current = new Piece(PieceType.ACTIVE);
98 lauta.setPiece(cx, cy, current); 124 lauta.setPiece(moveX, moveY, current);
99 } 125 }
100 } 126 }
101 } 127 }
102 128
103 129
111 { 137 {
112 getContentPane().setBackground(Color.white); 138 getContentPane().setBackground(Color.white);
113 139
114 try 140 try
115 { 141 {
116 lautaBG = ImageIO.read(new File("tausta.png")); 142 lautaBG = ImageIO.read(new File("background.jpg"));
117 } 143 }
144
118 catch (IOException e) 145 catch (IOException e)
119 { 146 {
120 JOptionPane.showMessageDialog(null, 147 JOptionPane.showMessageDialog(null,
121 "Could not load background image.", 148 "Could not load background image.",
122 "Initialization error", 149 "Initialization error",
132 Graphics2D g2 = (Graphics2D) g; 159 Graphics2D g2 = (Graphics2D) g;
133 160
134 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 161 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
135 RenderingHints.VALUE_ANTIALIAS_ON); 162 RenderingHints.VALUE_ANTIALIAS_ON);
136 163
137 lauta.paint(g2, 15, 15, 60); 164 lauta.paint(g2, 100, 150, 60);
138 } 165 }
139 166
140 public void keyPressed(KeyEvent e) 167 public void keyPressed(KeyEvent e)
141 { 168 {
142 } 169 }
145 { 172 {
146 } 173 }
147 174
148 public void keyTyped(KeyEvent e) 175 public void keyTyped(KeyEvent e)
149 { 176 {
150 switch (event.getKeyCode()) 177 switch (e.getKeyCode())
151 { 178 {
152 case VK_LEFT: 179 case KeyEvent.VK_LEFT:
153 case VK_UP: 180 case KeyEvent.VK_UP:
154 lauta.pieceRotate(false); 181 lauta.pieceRotate(false);
155 break; 182 break;
156 183
157 case VK_RIGHT: 184 case KeyEvent.VK_RIGHT:
158 case VK_DOWN: 185 case KeyEvent.VK_DOWN:
159 lauta.pieceRotate(true); 186 lauta.pieceRotate(true);
160 break; 187 break;
161 188
162 case VK_ENTER: 189 case KeyEvent.VK_ENTER:
163 lauta.pieceFinishTurn(); 190 lauta.pieceFinishTurn();
164 break; 191 break;
165 } 192 }
166 } 193 }
167 } 194 }