comparison Ristipolku.java @ 0:f930f72ed0f5

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 26 Jan 2011 03:43:17 +0200
parents
children 44f1e7b47fcf
comparison
equal deleted inserted replaced
-1:000000000000 0:f930f72ed0f5
1 /*
2 * Ristipolku
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
4 *
5 * Ohjelmointiprojekti Java-kurssille.
6 *
7 */
8 import java.awt.*;
9 import java.awt.geom.*;
10 import java.awt.font.TextLayout;
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.*;
16
17
18 public class RistiPolku extends JApplet implements Runnable
19 {
20
21 public Graphics2D createGraphics2D(Dimension d) {
22 Graphics2D g2 = null;
23
24 if (bimg == null || bimg.getWidth() != d.w || bimg.getHeight() != d.h) {
25 bimg = (BufferedImage) createImage(d.w, d.h);
26 reset(d);
27 }
28
29 g2 = bimg.createGraphics();
30 g2.setBackground(getBackground());
31 g2.clearRect(0, 0, w, h);
32 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
33 RenderingHints.VALUE_ANTIALIAS_ON);
34 return g2;
35 }
36
37
38 public void paint(Graphics g) {
39 Dimension d = getSize();
40 step(d.width, d.height);
41 Graphics2D g2 = createGraphics2D(d);
42 drawDemo(d.width, d.height, g2);
43 g2.dispose();
44 g.drawImage(bimg, 0, 0, this);
45 }
46
47
48 public void start() {
49 thread = new Thread(this);
50 thread.setPriority(Thread.MIN_PRIORITY);
51 thread.start();
52 }
53
54
55 public synchronized void stop() {
56 thread = null;
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 }