source: proiecte/SolarSim/Java/SolarSim/src/solarsim/renderer/Renderer2D.java @ 152

Last change on this file since 152 was 152, checked in by (none), 14 years ago
File size: 2.5 KB
Line 
1package solarsim.renderer;
2
3import solarsim.*;
4import solarsim.common.State;
5import java.awt.*;
6import java.io.FileInputStream;
7import java.io.InputStream;
8import java.util.*;
9import javax.swing.*;
10
11
12public class Renderer2D extends JFrame implements Runnable {
13
14    private final MyCanvas canvas;
15    private final LinkedList<State> states;
16    private volatile boolean running;
17    private Thread thread;
18    static int count=0;
19
20    public Renderer2D() {
21        super("Solar Sim");
22        this.canvas = new MyCanvas();
23        this.states = new LinkedList<State>();
24
25        this.setLayout(new BorderLayout());
26        this.add(canvas);
27
28        this.pack();
29        this.setVisible(true);
30    }
31
32    public void drawState(State s) {
33        if( count++ % RenderConstants.DRAW_EVERY != 0 )
34            return;
35        if( s == null )
36            return;
37        synchronized( states ) {
38            states.addFirst(s);
39            states.notify();
40        }
41    }
42
43    @Override
44    public void run() {
45        this.running = true;
46        this.thread = Thread.currentThread();
47        while( running ) {         
48            synchronized( states ) {
49                while( states.isEmpty() ) {
50                    try {
51                        states.wait();
52                    } catch (InterruptedException ex) { }
53                }
54                State s = states.removeLast();
55                canvas.drawState(s);
56            }
57            try {
58                thread.sleep(40);
59            } catch( Exception ex ) {}
60        }
61    }
62
63    public void stop() {
64        this.running = false;
65        this.thread.interrupt();
66        // close JFrame
67        this.setVisible(false);
68        this.dispose();
69    }
70
71
72    /**
73     * @param args the command line arguments
74     */
75    public static void main(String[] args) {
76        try {
77            Renderer2D renderer = new Renderer2D();
78            new Thread(renderer).start();
79/*
80            ServerSocket ss = new ServerSocket(2200);
81            Socket sock = ss.accept();
82            InputStream is = sock.getInputStream();
83*/
84            InputStream is = new FileInputStream("sim.dat");
85            while(true) {
86                State s = State.readStateFromInputStream(is);
87                if( s == null )
88                    break;
89                renderer.drawState(s);
90            }
91
92            System.out.println("Stopping.");
93
94            Thread.currentThread().sleep(1000 * 60);
95            renderer.stop();
96  //          sock.close();
97        } catch(Exception e) {
98            e.printStackTrace();
99        }
100    }
101}
Note: See TracBrowser for help on using the repository browser.