Solved

Wind direction / Compass bearing applet

Posted on 2004-04-15
5
861 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create 4 <div> below each other in php 2 59
cookies analysis tools 2 73
Element alignment and word wrapping 9 62
Stupid git question 2 64
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…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now