annotate game/Engine.java @ 18:4507a431b410

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 31 Jan 2011 16:05:36 +0200
parents 59ff451750fb
children df494a65bf8c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
2 * Ristipolku Game Engine
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 */
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
5 package game;
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
6
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 import java.awt.*;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 import java.awt.geom.*;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
9 import java.awt.event.*;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
10 import java.awt.image.*;
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
11 import java.awt.event.*;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
12 import javax.imageio.*;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
13 import javax.swing.*;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
14 import java.util.*;
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
15 import java.io.*;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
16 import game.*;
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
18 import javax.sound.sampled.*;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
19
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
20
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
21 enum Sound {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
22 PIECE_PLACED("placed.wav"),
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
23 MUSIC_GAME("gamemusic.wav");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
24
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
25 final String name;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
26 private Clip clip;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
27
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
28 Sound(String name)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
29 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
30 this.name = name;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
31 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
32
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
33 public boolean initialized()
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
34 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
35 return clip != null;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
36 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
37
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
38 public static void load(String path) throws IOException
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
39 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
40 for (Sound snd : Sound.values()) {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
41 String filename = path + snd.name;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
42 ResourceLoader res = new ResourceLoader(filename);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
43 if (res == null || res.getStream() == null)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
44 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
45 throw new IOException("Could not load audio resource '"+filename+"'.\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
46 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
47 try {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
48 AudioInputStream is = AudioSystem.getAudioInputStream(res.getStream());
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
49 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
50 catch (UnsupportedAudioFileException e) {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
51 throw new IOException("Unsupported audio file format for '"+filename+"'.\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
52 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
53 catch (IOException e)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
54 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
55 throw new IOException("Could not load audio resource '"+filename+"'.\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
56 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
57 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
58 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
59
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
60 public void play()
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
61 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
62 if (clip == null)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
63 return;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
64
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
65 clip.setFramePosition(0);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
66 clip.start();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
67 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
68
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
69 public void loop(int n)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
70 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
71 if (clip == null)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
72 return;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
73
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
74 if (n < 0)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
75 clip.loop(Clip.LOOP_CONTINUOUSLY);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
76 else
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
77 clip.loop(n);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
78 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
79
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
80 public void stop()
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
81 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
82 if (clip == null)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
83 return;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
84
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
85 if (clip.isRunning())
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
86 clip.stop();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
87 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
88
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
89 public boolean isPlaying()
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
90 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
91 if (clip == null)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
92 return false;
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
93
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
94 return clip.isRunning();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
95 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
96 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
97
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
98 class PathInfo
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
99 {
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
100 public int in, inX, inY, out, outX, outY;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
101
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
102 public PathInfo(int in, int inX, int inY, int out, int outX, int outY)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
103 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
104 this.in = in;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
105 this.inX = inX;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
106 this.inY = inY;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
107
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
108 this.out = out;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
109 this.outX = outX;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
110 this.outY = outY;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
111 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
112 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
114 /*
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
115 class AnimatedElement
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
116 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
117 float x, y, stime, value;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
118 Interpolate lerp;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
119 boolean active;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
120
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
121 public AnimatedElement(float x, float y, )
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
122 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
123 stime = 0;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
124 this.x = x;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
125 this.y = y;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
126
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
127 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
128
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
129 public animate(float time)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
130 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
131 if (!active)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
132 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
133 active = true;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
134 stime = time;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
135 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
136
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
137 float t = (time - stime) / 10.0f;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
138 if (t < 100)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
139 value = lerp.getValue(t);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
140 else
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
141 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
142
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
143 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
144 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
145
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
146 public paint(Graphics2D g, );
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
147 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
148 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
149 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
150 */
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
152 class GameBoard
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 {
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
154 public static final int boardSize = 9;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
155 public static final int boardMiddle = 4;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
156 Piece[][] board;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
157 Piece current;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
158 public boolean flagGameOver;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
159
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
160 int moveX, moveY, movePoint;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
161
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
162 public GameBoard()
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
163 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
164 board = new Piece[boardSize][boardSize];
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
165
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
166 board[boardMiddle][boardMiddle] = new Piece(PieceType.START);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
167
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
168 moveX = boardMiddle;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
169 moveY = boardMiddle - 1;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
170 movePoint = 0;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
171
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
172 pieceFinishTurn();
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
173
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
174 flagGameOver = false;
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
175 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
177 public void paint(Graphics2D g, int sx, int sy, float scale)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
178 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
179 for (int y = 0; y < boardSize; y++)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
180 for (int x = 0; x < boardSize; x++)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
181 if (board[x][y] != null)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
182 {
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
183 AffineTransform save = g.getTransform();
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
184
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
185 board[x][y].paint(g,
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
186 sx + (x * scale),
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
187 sy + (y * scale),
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
188 scale - scale / 10);
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
189
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
190 g.setTransform(save);
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
191 }
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
192 }
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
193
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
194 public void animate(float time)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
195 {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
196 for (int y = 0; y < boardSize; y++)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
197 for (int x = 0; x < boardSize; x++)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
198 if (board[x][y] != null)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
199 {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
200 board[x][y].animate(time);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
201 }
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
202
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
203 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
204
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
205 private boolean isEmpty(int x, int y)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
206 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
207 return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
208 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
209
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
210 private Piece getPiece(int x, int y)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
211 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
212 if (x >= 0 && x < boardSize && y >= 0 && y < boardSize)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
213 return board[x][y];
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
214 else
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
215 return null;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
216 }
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
217
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
218 public void pieceRotate(boolean dir)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
219 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
220 if (current != null)
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
221 current.rotate(dir);
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
222 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
223
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
224 public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark)
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
225 {
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
226 int x = startX, y = startY;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
227 int point = -1;
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
228
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
229 Piece curr = getPiece(startX, startY);
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
230 if (curr == null)
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
231 return null;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
232
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
233 /*
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
234 while (curr != null)
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
235 {
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
236 // curr.(true);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
237 // elements.spawn("", );
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
238 }
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
239 */
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
240
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
241 return new PathInfo(startPoint, startX, startY, point, x, y);
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
242 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
243
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
244 public void pieceFinishTurn()
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
245 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
246 if (current != null)
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
247 {
6
be0bf7544069 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
248 current.setType(PieceType.LOCKED);
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
249 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
250
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
251 if (i != null)
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
252 {
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
253 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
254 }
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
255
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
256 current = new Piece(PieceType.ACTIVE);
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
257 if (isEmpty(moveX, moveY))
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
258 {
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
259 board[moveX][moveY] = current;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
260 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
261 else
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
262 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
263 PathInfo i = resolvePath(moveX, moveY, movePoint, true);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
264 if (i != null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
265 board[moveX][moveY] = current;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
266 else
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
267 flagGameOver = true;
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
268 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
269 }
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
270 }
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
271
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
272
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
273 public class Engine extends JPanel
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
274 implements Runnable, KeyListener, MouseListener
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
275 {
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
276 Thread animThread;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
277 boolean animEnable = false;
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
278 GameBoard lauta = null;
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
279 BufferedImage lautaBG = null, lautaBGScaled = null;
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
280 Dimension oldDim;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
281 float clock;
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
282 Sound sounds;
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
283
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
284 public Engine()
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
285 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
286 BufferedImage img;
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
287 clock = 0;
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
288
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
289 System.out.print("Engine() constructor\n");
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
290
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
291 try
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
292 {
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
293 ResourceLoader res = new ResourceLoader("graphics/board.png");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
294 lautaBG = ImageIO.read(res.getStream());
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
295 sounds.load("sounds/");
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
296 }
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
297 catch (IOException e)
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
298 {
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
299 /*
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
300 JOptionPane.showMessageDialog(null,
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
301 "Could not load some of the resources.",
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
302 "Initialization error",
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
303 JOptionPane.ERROR_MESSAGE);
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
304 */
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
305 System.out.print("Could not load some of the resources.\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
306 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
307
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
308 lauta = new GameBoard();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
309 addKeyListener(this);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
310 addMouseListener(this);
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
311
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
312 // Get initial focus
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
313 if (!hasFocus())
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
314 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
315 System.out.print("Engine(): requesting focus\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
316 requestFocus();
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
317 }
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
318
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
319 sounds.MUSIC_GAME.loop(-1);
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
320 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
321
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
322 public void startThreads()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
323 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
324 System.out.print("startThreads()\n");
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
325 if (animThread == null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
326 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
327 animThread = new Thread(this);
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
328 animEnable = true;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
329 animThread.start();
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
330 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
331 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
332
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
333 public void stopThreads()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
334 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
335 System.out.print("stopThreads()\n");
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
336 if (animThread != null)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
337 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
338 animThread.interrupt();
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
339 animEnable = false;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
340 animThread = null;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
341 }
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
342 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343
18
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
344 public void mousePressed(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
345 public void mouseEntered(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
346 public void mouseExited(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
347 public void mouseReleased(MouseEvent e) { }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
348
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
349 public void mouseClicked(MouseEvent e)
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
350 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
351 System.out.print("mouseClicked()\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
352 if (!hasFocus())
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
353 {
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
354 System.out.print("requesting focus\n");
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
355 requestFocus();
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
356 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
357 }
4507a431b410 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
358
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
359 public void paintComponent(Graphics g)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
360 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
361 Graphics2D g2 = (Graphics2D) g;
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
362
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
363 // Use antialiasing when rendering the game elements
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 RenderingHints.VALUE_ANTIALIAS_ON);
4
e0e8fd08331e Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
366
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
367 // Rescale background if component size has changed
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
368 Dimension dim = getSize();
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
369 if (oldDim == null || !dim.equals(oldDim))
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
370 {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
371 lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
372 Graphics2D gimg = lautaBGScaled.createGraphics();
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
373 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
374 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
375
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
376 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
377 oldDim = dim;
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
378 System.out.print("scale changed\n");
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
379 }
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
380
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
381 // Background, pieces
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
382 g2.drawImage(lautaBGScaled, 0, 0, null);
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
383 lauta.paint(g2, 100, 150, 60);
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
384
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
385 // Other elements
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
388 public void keyTyped(KeyEvent e)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
389 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
390 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
391
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
392 public void keyReleased(KeyEvent e)
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
393 {
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
394 }
1
44f1e7b47fcf Preliminary work ... puuh.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
395
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
396 public void keyPressed(KeyEvent e)
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
397 {
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
398 System.out.print("lol\n");
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
399 switch (e.getKeyCode())
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
400 {
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
401 case KeyEvent.VK_LEFT:
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
402 case KeyEvent.VK_UP:
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
403 lauta.pieceRotate(false);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
404 break;
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
406 case KeyEvent.VK_RIGHT:
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
407 case KeyEvent.VK_DOWN:
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
408 lauta.pieceRotate(true);
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
409 break;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
410
7
70714c229e23 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
411 case KeyEvent.VK_ENTER:
2
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
412 lauta.pieceFinishTurn();
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
413 break;
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
414 }
1785f66a7beb Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
415 }
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
416
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
417 public void run()
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
418 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
419 while (animEnable)
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
420 {
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
421 clock++;
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
422
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
423 // System.out.print("clock=" + clock + "\n");
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
424
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
425 lauta.animate(clock);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
426
10
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
427 if (clock % 2 == 1)
4bacc98973f5 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
428 repaint();
9
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
429
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
430 try {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
431 Thread.sleep(10);
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
432 }
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
433
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
434 catch (InterruptedException x) {
a7751971c2a3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
435 }
8
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
436 }
d8e7fd8f3ccf Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
437 }
0
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 }