annotate Ristipolku.java @ 0:f930f72ed0f5

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Jan 2011 03:43:17 +0200
parents
children 44f1e7b47fcf
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 /*
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Ristipolku
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 *
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * Ohjelmointiprojekti Java-kurssille.
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 *
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 import java.awt.*;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 import java.awt.geom.*;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 import java.awt.font.TextLayout;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 import java.awt.event.WindowEvent;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 import java.awt.event.WindowListener;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 import java.awt.event.WindowAdapter;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 import java.awt.image.BufferedImage;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 import javax.swing.*;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 public class RistiPolku extends JApplet implements Runnable
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 public Graphics2D createGraphics2D(Dimension d) {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 Graphics2D g2 = null;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 if (bimg == null || bimg.getWidth() != d.w || bimg.getHeight() != d.h) {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 bimg = (BufferedImage) createImage(d.w, d.h);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 reset(d);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 g2 = bimg.createGraphics();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 g2.setBackground(getBackground());
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 g2.clearRect(0, 0, w, h);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 RenderingHints.VALUE_ANTIALIAS_ON);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 return g2;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 public void paint(Graphics g) {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 Dimension d = getSize();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 step(d.width, d.height);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 Graphics2D g2 = createGraphics2D(d);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 drawDemo(d.width, d.height, g2);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 g2.dispose();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 g.drawImage(bimg, 0, 0, this);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 public void start() {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 thread = new Thread(this);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 thread.setPriority(Thread.MIN_PRIORITY);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 thread.start();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 public synchronized void stop() {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 thread = null;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 public void run() {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 Thread me = Thread.currentThread();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 while (thread == me) {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 repaint();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 try {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 thread.sleep(10);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 } catch (InterruptedException e) { break; }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 thread = null;
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 public static void main(String argv[]) {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 final RistiPolku demo = new RistiPolku();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 demo.init();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 JFrame f = new JFrame("RistiPolku");
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 f.addWindowListener(new WindowAdapter() {
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 public void windowClosing(WindowEvent e) {System.exit(0);}
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 public void windowDeiconified(WindowEvent e) { demo.start(); }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 public void windowIconified(WindowEvent e) { demo.stop(); }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 });
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 f.getContentPane().add("Center", demo);
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 f.pack();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 f.setSize(new Dimension(400,300));
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 f.show();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 demo.start();
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 }
f930f72ed0f5 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 }