/** * Copyright (C) 2011 Aarhus University * * This software is provided under the MIT-license: * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is furnished to do * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * * * @author Rasmus W. Lauritsen (rala@iha.dk, rwl@post.au.dk, rwl@cs.au.dk) * @date 2011-12-28 *
*/ package dk.au.eng;
private Map<String,FlyingObject> fos = new HashMap<String,FlyingObject>(); privatelong scanTime = 20; // Time to make a step in ms private JFrame window; private ImageIcon background;
// Display a dot and transponder code for o if it is inside scan cone. privatevoid putObject(FlyingObject o, Graphics pen)
{
if (isInScanCone(o.longtitude, o.latitude))
{ // The flying oject o, just entered the scan range if (!o.isShown)
{
o.isShown=true;
o.showlat = o.latitude;
o.showlong = o.longtitude;
} int[] coords = radarCrdToScreenCrd(o.showlong, o.showlat);
Color c = pen.getColor();
pen.setColor(Color.BLUE);
pen.fillOval(coords[0], coords[1], 8, 8);
pen.setColor(Color.WHITE);
pen.drawString(o.transponder, coords[0]+10, coords[1]);
pen.setColor(c);
} else
o.isShown = false;
}
privatestaticint[] radarCrdToScreenCrd(int x, int y)
{ int[] res = newint[2];
res[0] = x + 200;res[1] = 200-y; return res;
}
privatevoid putLineOnRadar(int x, int y, int x1, int y1, Color c)
{ if (pen != null)
{
Color oc = pen.getColor();
pen.setColor(c); int[] startCrds = radarCrdToScreenCrd(x, y); int[] stopCrds = radarCrdToScreenCrd(x1, y1);
pen.drawLine(startCrds[0],startCrds[1],stopCrds[0],stopCrds[1]);
pen.setColor(oc);
}
publicstaticint crdToAngle(int x, int y)
{ double len = Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
double xAngle = Math.acos( x / len );
// Q1 => No extra xAngle is the answer // Q2 => No extra xAngle is the answer // Q3 => x is negative and hence xAngle is 90 degrees off // on the vertical axis xAngle is 180 off // Q4 => No extra xAngle is 270 degrees off double extra = y < 0 ? 360 - radianToDegrees(xAngle) : radianToDegrees(xAngle);
return (int)(Math.round(extra));
}
privateboolean isInScanCone(int x, int y)
{ int fromAngle = scanAngle - scanWidth; int toAngle = scanAngle; return crdToAngle(x, y) > fromAngle && crdToAngle(x, y) < toAngle;
}
public Radar()
{
background = new ImageIcon(Radar.class.getResource("radardisc.jpg")); this.setBackground(Color.BLACK);
font = new Font("Helvetica",Font.BOLD,12);
setPreferredSize(new Dimension(400, 400));
scanAngle = (90 % step == 0 ) ? 1 : 0; this.window = makeWindow(this);
}
// Draw center dot
pen.setColor(Color.BLACK);
pen.fillOval(195,195,10,10);
// Draw rings for(int d = 10; d < 200;d+=10)
pen.drawOval(200-d, 200-d, 2*d, 2*d);
// Draw the flying objects in this radar synchronized(fos)
{ for(FlyingObject fo : fos.values())
putObject(fo, pen);
}
// Draw the scan line
putLineOnRadar(0, 0,
(int)(200*Math.cos(degreesToRadians(scanAngle))),
(int)(200*Math.sin(degreesToRadians(scanAngle))), Color.YELLOW);
} return;
}
// Not the dispatcher thread lets delegate this invocation try {
SwingUtilities.invokeAndWait(new Runnable() {
// -- AddFlyingObject: int * int * int * seq of char ==> () public Value AddFlyingObject(Value longtitude, Value latitude, Value altitude, Value transponder)
{ synchronized(this.fos)
{
IntegerValue iv = (IntegerValue)longtitude;
IntegerValue la = (IntegerValue)latitude;
// -- RemFlyingObject: seq of char ==> () public Value RemFlyingObject(Value transponder)
{ synchronized(this.fos)
{
fos.remove(transponder); returnnew VoidValue();
}
}
// -- UpdateFlyingObject: seq of char * int * int ==> () public Value UpdateFlyingObject(Value transponder, Value longtitude, Value latitude)
{ int lon = (int)((IntegerValue)longtitude).value; int lat = (int)((IntegerValue)latitude).value; synchronized(this.fos)
{
FlyingObject o = fos.get(transponder); if (o != null)
{
o.longtitude = lon;
o.latitude = lat;
} returnnew VoidValue();
}
}
// -- SetStepSize: int ==> () public Value SetStepSize(Value newStep)
{ this.step = (int)((IntegerValue)newStep).value; returnnew VoidValue();
}
// -- StepRadar: () ==> () public Value StepRadar()
{ // If the window is not shown yet, show it! if (!isShowing)
{
isShowing = true;
(window).setVisible(true);
}
// step the radar this.stepScan();
// wait a bit to simulate it takes time to scan try { Thread.sleep(scanTime);
} catch (InterruptedException e) { Thread.currentThread().interrupt();
} returnnew VoidValue();
}
// -- SetScanWidth: int ==> () public Value SetScanWidth(Value width)
{
// -- SetScanTime: int ==> () public Value SetScanTime(Value scanTime)
{ this.scanTime = (int)((IntegerValue)scanTime).value; returnnew VoidValue();
}
// -- SetWindowPosition: int * int ==> () public Value SetWindowPosition(Value ix, Value iy)
{ int x = (int)((IntegerValue)ix).value; int y = (int)((IntegerValue)iy).value; this.window.setLocation(x, y); returnnew VoidValue();
}
// -- SetTitle: seq of char ==> () public Value SetTitle(Value v)
{
window.setTitle(v.toString()); returnnew VoidValue();
}
// -- SetScanAngle: int ==> () public Value SetScanAngle(Value v)
{
scanAngle = (int)((IntegerValue)v).value; returnnew VoidValue();
}
// -- static ToCharSeq: token ==> seq of char publicstatic Value ToCharSeq(Value v)
{ if (v != null)
{
System.out.println(v.getClass());
if (v instanceof NumericValue)
{
NumericValue nv = (NumericValue)v; returnnew SeqValue("Number:"+Double.toString(nv.value));
}
if (v != null) returnnew SeqValue(v.toString());
} else System.out.println("V is null, Why the face."); returnnew SeqValue();
}
publicstaticvoid main(String[] args)
{
Radar r = new Radar();
r.StepRadar();
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.10Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.