Solved

Wind direction / Compass bearing applet

Posted on 2004-04-15
5
857 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
correction:

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

Should read "compass.class"
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

8 Experts available now in Live!

Get 1:1 Help Now