Solved

Wind direction / Compass bearing applet

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

777 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