comparison Ristipolku.java @ 1:44f1e7b47fcf

Preliminary work ... puuh.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 28 Jan 2011 16:34:53 +0200
parents f930f72ed0f5
children 1785f66a7beb
comparison
equal deleted inserted replaced
0:f930f72ed0f5 1:44f1e7b47fcf
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 *
7 */ 6 */
8 import java.awt.*; 7 import java.awt.*;
9 import java.awt.geom.*; 8 import java.awt.geom.*;
10 import java.awt.font.TextLayout; 9 import java.awt.event.*;
11 import java.awt.event.WindowEvent;
12 import java.awt.event.WindowListener;
13 import java.awt.event.WindowAdapter;
14 import java.awt.image.BufferedImage;
15 import javax.swing.*; 10 import javax.swing.*;
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 }
16 29
17 30
18 public class RistiPolku extends JApplet implements Runnable 31 class GameBoard
19 { 32 {
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 }
20 47
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 /*
21 public Graphics2D createGraphics2D(Dimension d) { 108 public Graphics2D createGraphics2D(Dimension d) {
22 Graphics2D g2 = null; 109 Graphics2D g2 = null;
23 110
24 if (bimg == null || bimg.getWidth() != d.w || bimg.getHeight() != d.h) { 111 if (bimg == null || bimg.getWidth() != d.w || bimg.getHeight() != d.h) {
25 bimg = (BufferedImage) createImage(d.w, d.h); 112 bimg = (BufferedImage) createImage(d.w, d.h);
31 g2.clearRect(0, 0, w, h); 118 g2.clearRect(0, 0, w, h);
32 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 119 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
33 RenderingHints.VALUE_ANTIALIAS_ON); 120 RenderingHints.VALUE_ANTIALIAS_ON);
34 return g2; 121 return g2;
35 } 122 }
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 }
36 134
37 135
38 public void paint(Graphics g) { 136 public void keyPressed(KeyEvent e)
39 Dimension d = getSize(); 137 {
40 step(d.width, d.height); 138 }
41 Graphics2D g2 = createGraphics2D(d); 139
42 drawDemo(d.width, d.height, g2); 140 public void keyReleased(KeyEvent e)
43 g2.dispose(); 141 {
44 g.drawImage(bimg, 0, 0, this); 142 }
45 } 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;
46 152
47 153 case VK_RIGHT:
48 public void start() { 154 case VK_DOWN:
49 thread = new Thread(this); 155 moveRotate(true);
50 thread.setPriority(Thread.MIN_PRIORITY); 156 break;
51 thread.start(); 157
52 } 158 case VK_ENTER:
53 159 moveNew();
54 160 break;
55 public synchronized void stop() { 161 }
56 thread = null; 162 }
57 }
58
59
60 public void run() {
61 Thread me = Thread.currentThread();
62 while (thread == me) {
63 repaint();
64 try {
65 thread.sleep(10);
66 } catch (InterruptedException e) { break; }
67 }
68 thread = null;
69 }
70
71
72 public static void main(String argv[]) {
73 final RistiPolku demo = new RistiPolku();
74 demo.init();
75 JFrame f = new JFrame("RistiPolku");
76 f.addWindowListener(new WindowAdapter() {
77 public void windowClosing(WindowEvent e) {System.exit(0);}
78 public void windowDeiconified(WindowEvent e) { demo.start(); }
79 public void windowIconified(WindowEvent e) { demo.stop(); }
80 });
81 f.getContentPane().add("Center", demo);
82 f.pack();
83 f.setSize(new Dimension(400,300));
84 f.show();
85 demo.start();
86 }
87 } 163 }