What's new?
|
Help
|
Directory
|
Sign in
heron-language
Heron is an object-oriented programming language for model driven architecture
Project Home
Downloads
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
Source Path:
svn
/
trunk
/
output
/
HeronApplication.java
‹ r44
r47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.*;
public class HeronApplication extends JApplet implements KeyListener
{
public class Dispatcher extends Thread
{
public synchronized void run()
{
while (true) {
try {
// TODO: compute exactly the right amount of time to wait.
wait(10);
HeronApplication.theApp.dispatchNextSignal();
}
catch (InterruptedException e) {
}
}
}
}
static HeronObject heronKeyListener;
private static final long serialVersionUID = -4394966026114834504L;
final static int maxCharHeight = 15;
final static int minFontSize = 6;
public static ArrayList<Shape> shapes = new ArrayList<Shape>();
final static Color bg = Color.white;
final static Color fg = Color.black;
final static Color red = Color.red;
final static Color white = Color.white;
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);
final static float dash1[] = { 10.0f };
final static BasicStroke dashed = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
Dimension totalSize;
FontMetrics fontMetrics;
public static JFrame frame = null;
public static HeronApplication theApp = null;
public Dispatcher dispatcher;
public void init() {
theApp = this;
setBackground(bg);
setForeground(fg);
dispatcher = new Dispatcher();
dispatcher.start();
}
synchronized public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Dimension d = getSize();
Image image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(d.width, d.height);
Graphics2D imgG = (Graphics2D)image.getGraphics();
imgG.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
imgG.setBackground(bg);
imgG.clearRect(0, 0, d.width, d.height);
imgG.setPaint(fg);
for (int i=0; i < shapes.size(); ++i) {
imgG.draw(shapes.get(i));
}
g2.drawImage(image, 0, 0, this);
}
public static void drawChord(double x, double y, double rad, double start, double finish) {
drawOpenArc(x - rad, y - rad, 2 * rad, 2 * rad, start, finish);
}
public static void drawOpenArc(double x, double y, double width, double height, double start, double finish) {
shapes.add(new Arc2D.Double(x, y, width, height, radToDeg(start), radToDeg(finish), Arc2D.OPEN));
}
public static void drawLine(double x0, double y0, double x1, double y1) {
shapes.add(new Line2D.Double(x0, y0, x1, y1));
}
public static void drawCircle(double x, double y, double rad) {
drawChord(x, y, rad, 0, Math.PI * 2);
}
public static double degToRad(int deg) {
return (Math.PI * 2 * deg) / 360;
}
public static double radToDeg(double rad) {
return (360 * rad) / (2 * Math.PI);
}
public static double pi = Math.PI;
public static double sqr(double x) {
return x * x;
}
public static double acos(double x) {
return Math.acos(x);
}
public static double sqrt(double x) {
return Math.sqrt(x);
}
public synchronized static void clear() {
shapes.clear();
}
public static HeronDateTime now() {
return new HeronDateTime();
}
public synchronized static void render() {
theApp.repaint();
//Graphics myGraphics = getPaintGraphics();
}
public static void baseMain(HeronApplication app) {
theApp = app;
frame = new JFrame("Heron Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.addKeyListener(theApp);
frame.getContentPane().add("Center", theApp);
frame.pack();
frame.setSize(new Dimension(550, 500));
frame.setVisible(true);
render();
theApp.init();
}
public static void error(String s) {
System.console().printf(s);
}
//================================================================================
// Signal/Event handling stuff
public ArrayList<HeronSignal> signals = new ArrayList<HeronSignal>();
public void sendIn(HeronObject target, Object data, int msec) {
sendAt(target, data, (new HeronDateTime()).addMSec(msec));
}
public void sendAt(HeronObject target, Object data, HeronDateTime time) {
HeronSignal sig = new HeronSignal();
sig.data = data;
sig.scheduledTime = time;
sig.target = target;
signals.add(sig);
}
public HeronSignal getNextSignal()
{
// TODO: Improve by using a priority queue
HeronSignal ret = null;
HeronDateTime now = new HeronDateTime();
for (int i=0; i < signals.size(); ++i) {
HeronSignal tmp = signals.get(i);
if (tmp.scheduledTime.before(now))
{
if (ret == null)
ret = signals.get(i);
else if (tmp.scheduledTime.before(ret.scheduledTime))
ret = signals.get(i);
}
}
if (ret != null)
signals.remove(ret);
return ret;
}
synchronized public void dispatchNextSignal() {
HeronSignal sig = getNextSignal();
if (sig != null) {
sig.target.onSignal(sig);
}
}
public void keyPressed(KeyEvent e) {
if (heronKeyListener != null) {
HeronSignal sig = new HeronSignal();
sig.data = e;
sig.scheduledTime = new HeronDateTime();
sig.target = heronKeyListener;
heronKeyListener.onSignal(sig);
}
}
public static void registerKeyListener(HeronObject o) {
heronKeyListener = o;
}
public void keyReleased(KeyEvent e) {
// do nothing
}
public void keyTyped(KeyEvent e) {
// do nothing
}
}
Show details
Hide details
Change log
r46
by cdiggins on Apr 08, 2008
Diff
Go to:
/trunk/heron_grammar.hpp
/trunk/heron_to_java.hpp
/trunk/input/Demo.hrn
/trunk/jaction_grammar.hpp
/trunk/output/Ball.java
/trunk/output/Collection.java
/trunk/output/Demo.java
/trunk/output/HeronApplication.java
/trunk/output/Line.java
/trunk/output/Painter.java
/trunk/output/Point.java
/trunk/output/Shooter.java
/trunk/output/Vector.java
Project members,
sign in
to write a code review
Older revisions
r44
by cdiggins on Mar 26, 2008
Diff
Improved parsing. Java code generation still not great.
r41
by cdiggins on Feb 23, 2008
Diff
All revisions of this file
File info
Size: 5781 bytes, 214 lines
View raw file