Solved

Wind direction / Compass bearing applet

Posted on 2004-04-15
5
866 Views
Last Modified: 2008-02-01
I'm trying to implement a simple wind direction applet on our Intranet. As this is a side project, I don't have the time to start from scratch. I've searched the net with limited success

Does anyone know of an applet that will do what I require, namely:
Read a compass bearing and display the face of a compass, with the wind direction drawn in a fashion similar to that of a compass needle.
0
Comment
Question by:da_h
  • 4
5 Comments
 
LVL 8

Accepted Solution

by:
HaydnH earned 500 total points
ID: 10841016
Hi da_h,

How do you want this applet to get the wind direction? Do you want it to keep updating or just load once showing the wind direction when the page is first loaded? If the later it would make things much easier... here's  very simple java applet to show how you could draw a compass: (you'll need a java compiler to compile it)

________________________________________________________________________

/* Draw a basic compass */

import java.awt.*;
import java.applet.*;

public class compass extends Applet
{

  // Variables
  Color compassColor;
  Color needleColor;
  Color bgColor;
  int bearing;
  int needleOpp;
  int needleAdj;

  public void init()
  {
    compassColor = new Color(160,160,160);
    needleColor = new Color(0,0,0);
    bgColor = new Color(255,255,255);
    setBackground(bgColor);
  }

  public void stop()
  {
  }

  // Draw...
  public void paint(Graphics g)
  {

    // The compass (circle: int x, int y, int width, int height,int startAngle, int arcAngle)
    g.setColor(compassColor);
    g.fillArc(10,10,80,80,0,360);

    bearing = Integer.parseInt(getParameter("bearing"), 10);

    needleAdj = (int)(Math.cos(bearing * (Math.PI / 180)) * 35);
    needleOpp = (int)(Math.sin(bearing * (Math.PI / 180)) * 35);

   
    // Draw needle (line: int x1, int y1, int x2, int y2)
    g.setColor(needleColor);
    g.drawLine(50 + needleOpp, 50 - needleAdj, 50 - needleOpp, 50 + needleAdj);

    g.setColor(Color.black);

  }
}
________________________________________________________________________


And then call it from your html as follows:

<applet code="compass.class" width="100" height="100">
  <PARAM NAME="bearing" VALUE="35">
</applet>


Where bearing=0 is vertically up, 90 is poitning right etc.


Haydn.


0
 

Author Comment

by:da_h
ID: 10855933
Thanks Haydn

Compass looks good. I'd like to update it regularly from text file data, although can supply the bearings in any format required.

What would be the best way to force the compass to re-read a new bearing?

Dave
0
 
LVL 8

Expert Comment

by:HaydnH
ID: 10858802
Hi Dave,

How will you be getting the text file? will it be available on a shared drive onyour network? Or would it have to be done through the web server? If you can read a text file on the network from the clients machines:

compass.java:
_______________________________________________________________________
/* Draw a basic compass */

import java.awt.*;
import java.applet.*;
import java.io.*;

public class compass extends Applet implements Runnable {

  // Define the global variables
  Thread timer;
  StringBuffer buffer;
  Color compassColor;
  Color needleColor;
  int bearing;

  // Init the applet
  public void init() {
    compassColor = new Color(160,160,160);
    needleColor = new Color(0,0,0);
    Color bgColor = new Color(255,255,255);
    setBackground(bgColor);
    int bearing = 0;
  }

  // Start, run and stop are required by "implements runnable" (the timer thread)
  public void start() {
    timer = new Thread(this);
    timer.start();
  }

  public void run() {
    while (Thread.currentThread() == timer) {
      repaint();
          
      try {
        Thread.sleep(Integer.parseInt(getParameter("updateDelay"), 10) * 1000);
      } catch (InterruptedException e) {
        break;
      }
    }
  }

  public void stop() {
    timer = null;
  }

  // What the paint
  public void paint(Graphics g) {
    update(g);
  }

  // How to upadte the graphics
  public void update(Graphics g) {
    g.setColor(compassColor);
    g.fillArc(10,10,80,80,0,360);

    buffer = new StringBuffer();
    readTextFile(getParameter("FileToRead"));

    bearing = Integer.parseInt(buffer.toString(), 10);

    int needleAdj = (int)(Math.cos(bearing * (Math.PI / 180)) * 35);
    int needleOpp = (int)(Math.sin(bearing * (Math.PI / 180)) * 35);
   
    // Draw needle (line: int x1, int y1, int x2, int y2)
    g.setColor(needleColor);
    g.drawLine(50 + needleOpp, 50 - needleAdj, 50 - needleOpp, 50 + needleAdj);

    g.setColor(Color.black);

  }

  // To read a file
  public void readTextFile(String s) {
    String thisLine;
    try {
      InputStream is = getClass().getResourceAsStream(s);
      BufferedReader br = new BufferedReader
        (new InputStreamReader(is));
      while ((thisLine = br.readLine()) != null) {  
        System.out.println(thisLine);
        buffer.append(thisLine);
      }

    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
_______________________________________________________________________



SomeFile.html:
_______________________________________________________________________
<HTML>
<HEAD>
<SCRIPT>
</SCRIPT>

</HEAD>
<BODY>
<applet code="compass.class" width="100" height="100">
  <PARAM NAME="FileToRead" VALUE="datafile.txt">
  <PARAM NAME="updateDelay" VALUE="1">
</applet>
</BODY>
</HTML>
_______________________________________________________________________



datafile.txt: (same directory as the java applet on the client!!)
_______________________________________________________________________
106
_______________________________________________________________________
0
 
LVL 8

Expert Comment

by:HaydnH
ID: 10859463
Having the text file on each client might be a bit annoying... having a text file on the web server in the same directory as the applet may be a better idea:

compass.java:
_________________________________________________________________

/* Draw a basic compass */

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class compass extends Applet implements Runnable {

  // Define the global variables
  Thread timer;
  StringBuffer buffer;
  Color compassColor;
  Color needleColor;
  int bearing;

  // Init the applet
  public void init() {
    compassColor = new Color(160,160,160);
    needleColor = new Color(0,0,0);
    Color bgColor = new Color(255,255,255);
    setBackground(bgColor);
    int bearing = 0;
  }

  // Start, run and stop are required by "implements runnable" (the timer thread)
  public void start() {
    timer = new Thread(this);
    timer.start();
  }

  public void run() {
    while (Thread.currentThread() == timer) {
      repaint();
          
      try {
        Thread.sleep(Integer.parseInt(getParameter("updateDelay"), 10) * 1000);
      } catch (InterruptedException e) {
        break;
      }
    }
  }

  public void stop() {
    timer = null;
  }

  // What the paint
  public void paint(Graphics g) {
    update(g);
  }

  // How to upadte the graphics
  public void update(Graphics g) {
    readTextURL();

    g.setColor(compassColor);
    g.fillArc(10,10,80,80,0,360);

    int needleAdj = (int)(Math.cos(bearing * (Math.PI / 180)) * 35);
    int needleOpp = (int)(Math.sin(bearing * (Math.PI / 180)) * 35);
   
    // Draw needle (line: int x1, int y1, int x2, int y2)
    g.setColor(needleColor);

    g.drawLine(50 + needleOpp, 50 - needleAdj, 50 - needleOpp, 50 + needleAdj);

    g.setColor(Color.black);

  }

  // To read a file from the URL
  public void readTextURL() {
    try {
      URL theurl = new URL(getParameter("FileURL"));
      BufferedReader in = new BufferedReader(new InputStreamReader(theurl.openStream()));

        bearing = Integer.parseInt(in.readLine());

      in.close();

     } catch (Exception e) {}

  }
}
_________________________________________________________________


SomeFile.html
_________________________________________________________________
<HTML>
<BODY>
<applet code="comptest.class" width="100" height="100">
  <PARAM NAME="FileURL" VALUE="http://www.somewhere.com/datafile.txt">
  <PARAM NAME="updateDelay" VALUE="2">
</applet>
</BODY>
</HTML>
_________________________________________________________________


And make sure datafile.txt exists in the same directory as the applet with just the bearing:
_________________________________________________________________
94
_________________________________________________________________


Haydn.
0
 
LVL 8

Expert Comment

by:HaydnH
ID: 10859471
correction:

   <applet code="comptest.class" width="100" height="100">

Should read "compass.class"
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question