# HG changeset patch # User Matti Hamalainen # Date 1296225293 -7200 # Node ID 44f1e7b47fcf4a3b4d2bc3f1274042f824e17f48 # Parent f930f72ed0f5fb1815c796d9d44b85aa600e50ec Preliminary work ... puuh. diff -r f930f72ed0f5 -r 44f1e7b47fcf Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Jan 28 16:34:53 2011 +0200 @@ -0,0 +1,33 @@ +# Settings, directories +RUN=gfxtest.class +TARGETS=$(RUN) +SUBDIR=game + +# Utils +JAVAC=javac -g +APPLETVIEWER=appletviewer + +### +### Targets +### +all: $(TARGETS) + +gfxtest.class: game/Piece.class game/PieceType.class + +%.class: %.java + $(JAVAC) $< + + +run: $(RUN) + $(APPLETVIEWER) $(patsubst %.class,%.html,$<) + + +clean: + $(RM) $(TARGETS) *.class $(SUBDIR)/*.class + + +srcclean: clean + $(RM) *~ $(SUBDIR)/*~ + + +# dummy diff -r f930f72ed0f5 -r 44f1e7b47fcf Ristipolku.java --- a/Ristipolku.java Wed Jan 26 03:43:17 2011 +0200 +++ b/Ristipolku.java Fri Jan 28 16:34:53 2011 +0200 @@ -3,21 +3,108 @@ * (C) Copyright 2011 Matti 'ccr' Hämäläinen * * Ohjelmointiprojekti Java-kurssille. - * */ import java.awt.*; import java.awt.geom.*; -import java.awt.font.TextLayout; -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; -import java.awt.event.WindowAdapter; -import java.awt.image.BufferedImage; +import java.awt.event.*; import javax.swing.*; +import java.util.*; +import game.*; + +class PathInfo +{ + public int in, inX, inY, out, outX, outY; + + public PathInfo(in, inX, inY, out, outX, outY) + { + this.in = in; + this.inX = inX; + this.inY = inY; + + this.out = out; + this.outX = outX; + this.outY = outY; + } +} -public class RistiPolku extends JApplet implements Runnable +class GameBoard { + public static final int boardSize = 9; + public static final int boardMiddle = 4; + Piece[][] board; + Piece current; + + public GameBoard() + { + board = new Piece[boardSize][boardSize]; + board[boardMiddle][boardMiddle] = new Piece(PieceType.START); + moveX = boardMiddle; + moveY = boardMiddle - 1; + + + } + public void paint(Graphics2D g, int sx, int sy, double scale) + { + for (int y = 0; y < boardSize; y++) + for (int x = 0; x < boardSize; x++) + { + if (board[x][y] != null) + board[x][y].paint(g, sx + (x * scale), sy + (y * scale), scale - scale/10); + } + } + + private boolean isEmpty(int x, int y) + { + return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); + } + + private Piece getPiece(int x, int y) + { + return board[x][y]; + } + + public Point resolvePath(int x, int y, int inpoint) + { + } + + public pieceRotate(boolean dir) + { + current.rotate(dir); + } + + public void pieceFinishTurn() + { + if (current != null) + current.setType(PieceType.NORMAL); + + if (isEmpty(cx, cy)) + { + current = new Piece(PieceType.ACTIVE); + lauta.setPiece(cx, cy, current); + } + + } +} + + +public class gfxtest extends JApplet + implements KeyListener +{ + GameBoard lauta; + + public void init() + { + getContentPane().setBackground(Color.white); + + lauta = new GameBoard(); + + addKeyListener(this); + } + + +/* public Graphics2D createGraphics2D(Dimension d) { Graphics2D g2 = null; @@ -33,55 +120,44 @@ RenderingHints.VALUE_ANTIALIAS_ON); return g2; } - +*/ - public void paint(Graphics g) { - Dimension d = getSize(); - step(d.width, d.height); - Graphics2D g2 = createGraphics2D(d); - drawDemo(d.width, d.height, g2); - g2.dispose(); - g.drawImage(bimg, 0, 0, this); - } + public void paint(Graphics g) + { + Graphics2D g2 = (Graphics2D) g; + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); - public void start() { - thread = new Thread(this); - thread.setPriority(Thread.MIN_PRIORITY); - thread.start(); - } - - - public synchronized void stop() { - thread = null; - } + lauta.paint(g2, 15, 15, 60); + } - public void run() { - Thread me = Thread.currentThread(); - while (thread == me) { - repaint(); - try { - thread.sleep(10); - } catch (InterruptedException e) { break; } - } - thread = null; - } - + public void keyPressed(KeyEvent e) + { + } + + public void keyReleased(KeyEvent e) + { + } + + public void keyTyped(KeyEvent e) + { + switch (event.getKeyCode()) + { + case VK_LEFT: + case VK_UP: + moveRotate(false); + break; - public static void main(String argv[]) { - final RistiPolku demo = new RistiPolku(); - demo.init(); - JFrame f = new JFrame("RistiPolku"); - f.addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) {System.exit(0);} - public void windowDeiconified(WindowEvent e) { demo.start(); } - public void windowIconified(WindowEvent e) { demo.stop(); } - }); - f.getContentPane().add("Center", demo); - f.pack(); - f.setSize(new Dimension(400,300)); - f.show(); - demo.start(); - } + case VK_RIGHT: + case VK_DOWN: + moveRotate(true); + break; + + case VK_ENTER: + moveNew(); + break; + } + } } diff -r f930f72ed0f5 -r 44f1e7b47fcf game/Interpolate.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/Interpolate.java Fri Jan 28 16:34:53 2011 +0200 @@ -0,0 +1,26 @@ +/* + * Class for smooth non-linear interpolation between two given values in N steps + * (C) Copyright 2011 Matti 'ccr' Hämäläinen + */ +package game; + +import java.util.*; + +public class Interpolate +{ + double start, end, steps; + + public Interpolate(double start, double end, double steps) + { + this.start = start; + this.end = end; + this.steps = steps; + } + + public double getValue(double step) + { + double n = step / steps; + double v = n * n * (3.0f - 2.0f * n); + return (start * v) + (end * (1.0f - v)); + } +} diff -r f930f72ed0f5 -r 44f1e7b47fcf game/Piece.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/Piece.java Fri Jan 28 16:34:53 2011 +0200 @@ -0,0 +1,174 @@ +/* + * Ristipolku + * (C) Copyright 2011 Matti 'ccr' Hämäläinen + * + * Ohjelmointiprojekti Java-kurssille. + */ +package game; + +import java.awt.*; +import java.awt.geom.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + + +public class Piece +{ + static final int numConnections = 8; + static final int minRotation = 0; + static final int maxRotation = 3; + int currRotation; + int[] connections; + PieceType type, oldType; + + boolean rotationChanged, rotationActive, + typeChanged, typeActive; + double currAngle, newAngle; + + + public Piece(PieceType ptype) + { + // Initialize + connections = new int[numConnections]; + type = ptype; + + rotationChanged = false; + typeChanged = false; + rotationActive = false; + typeActive = false; + + + currRotation = 0; + + // Initialize connections between endpoints of the paths inside the piece + for (int i = 0; i < numConnections; i++) + connections[i] = -1; + + Random rnd = new Random(); + for (int i = 0; i < numConnections; i++) + { + int tmp = rnd.nextInt(numConnections); + if (connections[tmp] < 0 && connections[i] < 0) + { + connections[i] = tmp; + connections[tmp] = i; + } + } + } + + public Piece() + { + this(PieceType.NONE); + } + + public void setType(PieceType ptype) + { + oldType = type; + type = ptype; + } + + public int getConnection(int in) + { + return connections[in]; + } + + public void rotate(boolean dir) + { + if (type != PieceType.NORMAL) + return; + + newRotation = currRotation + (dir ? 1 : -1); + + if (newRotation < minRotation) + newRotation = maxRotation; + else if (currRotation > maxRotation) + newRotation = minRotation; + + rotationDir = dir; + rotationChanged = true; + } + + public Point2D getPoint(double x, double y, double dim, int index) + { + double ox = 0, oy = 0; + double step = dim / 10; + + switch (index) { + case 0: ox = 3.0f; oy = 0.5f; break; + case 1: ox = 7.0f; oy = 0.5f; break; + case 2: ox = 9.5f; oy = 3.0f; break; + case 3: ox = 9.5f; oy = 7.0f; break; + case 4: ox = 7.0f; oy = 9.5f; break; + case 5: ox = 3.0f; oy = 9.5f; break; + case 6: ox = 0.5f; oy = 7.0f; break; + case 7: ox = 0.5f; oy = 3.0f; break; + + case -1: ox = 5; oy = 5; break; + } + + return new Point2D.Double(x + ox * step, y + oy * step); + } + + public void animate(double time) + { + if (rotationChanged) + { + rotationTime = time; + rotationActive = true; + } + + if (rotationActive) + { + double t = time - rotationTime; + rotationAngle = + } + + } + + public void paint(Graphics2D g, double x, double y, double dim) + { + AffineTransform save = g.getTransform(); + + g.setStroke(new BasicStroke(4.0f)); + + switch (type) { + case NORMAL: g.setPaint(Color.green); break; + case ACTIVE: g.setPaint(Color.red); break; + case START: g.setPaint(Color.orange); break; + } + + AffiineTransform tf = new AffineTransform(); + tf.rotate(Math.toRadians(rotationAngle)); + g.transform(tf); + g.fill(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10)); + + g.setPaint(Color.black); + g.setStroke(new BasicStroke(4.0f)); + g.draw(new RoundRectangle2D.Double(x, y, dim, dim, dim / 10, dim / 10)); + + if (type == PieceType.START) + return; + + g.setStroke(new BasicStroke(6.0f)); +// CubicCurve2D c = new CubicCurve2D.Double(); + QuadCurve2D c = new QuadCurve2D.Double(); + + for (int i = 0; i < numConnections / 2; i++) + { + Point2D start, cp1, cp2, end; + + start = getPoint(x, y, dim, i); + end = getPoint(x, y, dim, connections[i]); + +// cp1 = getPoint(x, y, dim, (i + 4) % 8); +// cp2 = getPoint(x, y, dim, (connections[i] + 4) % 8); + cp1 = getPoint(x, y, dim, -1); + + c.setCurve(start, cp1, end); + g.draw(c); + } + + g.setTransform(save); + } +} diff -r f930f72ed0f5 -r 44f1e7b47fcf game/PieceType.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/PieceType.java Fri Jan 28 16:34:53 2011 +0200 @@ -0,0 +1,10 @@ +/* + * Ristipolku + * (C) Copyright 2011 Matti 'ccr' Hämäläinen + * + * Ohjelmointiprojekti Java-kurssille. + */ +package game; + +public enum PieceType { START, NORMAL, ACTIVE, NONE } +