Mercurial > hg > ristipolku
diff Ristipolku.java @ 0:f930f72ed0f5
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 26 Jan 2011 03:43:17 +0200 |
parents | |
children | 44f1e7b47fcf |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ristipolku.java Wed Jan 26 03:43:17 2011 +0200 @@ -0,0 +1,87 @@ +/* + * Ristipolku + * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> + * + * 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 javax.swing.*; + + +public class RistiPolku extends JApplet implements Runnable +{ + + public Graphics2D createGraphics2D(Dimension d) { + Graphics2D g2 = null; + + if (bimg == null || bimg.getWidth() != d.w || bimg.getHeight() != d.h) { + bimg = (BufferedImage) createImage(d.w, d.h); + reset(d); + } + + g2 = bimg.createGraphics(); + g2.setBackground(getBackground()); + g2.clearRect(0, 0, w, h); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + 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 start() { + thread = new Thread(this); + thread.setPriority(Thread.MIN_PRIORITY); + thread.start(); + } + + + public synchronized void stop() { + thread = null; + } + + + public void run() { + Thread me = Thread.currentThread(); + while (thread == me) { + repaint(); + try { + thread.sleep(10); + } catch (InterruptedException e) { break; } + } + thread = null; + } + + + 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(); + } +}