Solved

Wind direction / Compass bearing applet

Posted on 2004-04-15
5
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 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