Solved

"Thermometer" Java Applet

Posted on 2004-03-27
8
799 Views
Last Modified: 2013-11-23
I am looking for a Java applet that can read in a value of a flat file or .html document and display it graphically in a "thermometer" type applet.  The goal is to reach $150,000 so the top of the thermometer should represent that amount.  I am looking for a complete example and need it to be implemented without a database.
0
Comment
Question by:xcusmwah
  • 4
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
funnyveryfunny earned 250 total points
Comment Utility
Try my Sutra applet,

This applet reads in one Text file (contains all the filenames of my images) then it loads these images.

/////////////////Applet\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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

public class Sutra extends Applet implements ActionListener{

  String[] files;
  int index=0;
  Font f = new Font("Ariel",Font.BOLD,20);
  Label name;
  Panel textPane;
  TextArea textArea;
  Image[] images;
  Panel navigationPane;
  Button backwards, forwards;
  final String SPACE ="       ";
  SoundLoader sound;
  public Label soundIndicator;

  public void init(){
   this.setBackground(Color.black);
   this.setLayout(new BorderLayout());
   try{
      URL loc = this.getCodeBase();
      URL inLoc = new URL(loc,"filelist.txt");
      URLConnection con = inLoc.openConnection();
      con.connect();
      System.out.println(con.toString());
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      int size = Integer.parseInt(in.readLine());
      files= new String[size];
      images = new Image[size];
      for(int i=0;i<files.length;i++)
       files[i] = in.readLine();
    }catch(Exception e){}
    textPane = new Panel(new BorderLayout());
    textPane.setBackground(Color.black);
    navigationPane = new Panel(new BorderLayout());
    navigationPane.setBackground(Color.black);
    name = new Label("",Label.CENTER);
    name.setBackground(Color.black);
    name.setForeground(Color.white);
    name.setFont(f);
    backwards = new Button("<<<");
     backwards.setBackground(Color.black);
     backwards.setForeground(Color.white);
     backwards.addActionListener(this);
    navigationPane.add(backward,BorderLayout.WEST);
    navigationPane.add(name,BorderLayout.CENTER);
    forwards = new Button(">>>");
     forwards.setBackground(Color.black);
     forwards.setForeground(Color.white);
     forwards.addActionListener(this);
    navigationPane.add(forward,BorderLayout.EAST);
    textPane.add(navigationPane,BorderLayout.NORTH);
    textArea = new TextArea("",12,14,TextArea.SCROLLBARS_VERTICAL_ONLY);
    textArea.setBackground(Color.black);
    textArea.setForeground(Color.white);
    textArea.setEditable(false);
    textPane.add(textArea,BorderLayout.CENTER);
    soundIndicator = new Label("",Label.CENTER);
    soundIndicator.setBackground(Color.black);
    soundIndicator.setForeground(Color.white);
    textPane.add(soundIndicator,BorderLayout.SOUTH);
    this.add(textPane,BorderLayout.SOUTH);
  }

  public void start(){
    sound = new SoundLoader(this);
    sound.start();
  }

  public void stop(){
  }

  public void actionPerformed(ActionEvent e){
    if(e.getSource()==backwards){
      updateIndex(-1);
      this.repaint();
    }
    if(e.getSource()==forwards){
      updateIndex(1);
      this.repaint();
    }
  }

  public void myPaint(Graphics g){
    if(images[index]==null){
      images[index] = getImage(getCodeBase(),files[index]);
      MediaTracker tracker = new MediaTracker(this);
      tracker.addImage(images[index],0);
      try{
        tracker.waitForID(0);
        }catch(InterruptedException e){}
    }

    int l = files[index].indexOf(".jpg");
    String name1 = files[index].substring(0,l);

    try{
      URL loc = this.getCodeBase();
      URL inLoc = new URL(loc,name1+".txt");
      URLConnection con = inLoc.openConnection();
      con.connect();
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      g.clearRect(0,0,500,500);
      int w = images[index].getWidth(this);
      int h = images[index].getHeight(this);
      g.drawImage(images[index],(320-w)/2,(250-h)/2,this);
      name.setText(name1);
      textArea.setText("");
      String t = in.readLine();
      while(t!=null && t!=""){
        textArea.append(SPACE+t+"\n");
        t = in.readLine();
      }
    }catch(Exception e){}
  }

  public void updateIndex(int unit){
    index+=unit;
    if(index==files.length)
      index=0;
    else if(index<0)
      index = files.length+unit;
  }

  public void paint(Graphics g){
    myPaint(g);
  }
}
///////////////////////////Applet end\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


////////////////A thread class that loads my audio file\\\\\\\\\\\\\\\\\\\\\\\\\\
import java.applet.*;
import java.net.URL;
import java.util.*;

class SoundLoader extends Thread {
    Sutra applet;
    AudioClip audioClip;
    boolean running = true;
   
    public SoundLoader(Sutra applet) {
        this.applet = applet;
        setPriority(MAX_PRIORITY);
      audioClip = applet.getAudioClip(applet.getCodeBase(),"love2love.wav");
    }


    public void run() {
      while(true){
        if(audioClip!=null){
           applet.soundIndicator.setText("Playing Sound");
           audioClip.play();
        }
        try {            
            sleep(new Random().nextInt(30000));
        } catch (InterruptedException e) {}
      }
   }
}
///////////////////////End\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
0
 
LVL 4

Expert Comment

by:funnyveryfunny
Comment Utility
Constructor: Initialises the main frame and get filename.txt.

Paint(): At every call of this method, it loads the required image which is tracked by an index that points to filename[] array.

That is it really, go over it and you will understand. Also because each of my image has a description therefore paint() also reads a text file with the same name as image's filename, i.e image1.jpg & image1.txt.

Essentially it's an image viewer.
0
 
LVL 2

Expert Comment

by:John Fistere
Comment Utility
I have the same need, that is a thermometer to show progress towards a donations goal.  I don't see how the answer provided by funnyveryfunny related to the question.  I want a simple component that I can manually set to show the progress toward a goal.  I would like to have a choice of styles.

Thank you,
John Fistere
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:funnyveryfunny
Comment Utility
Hi John,

I classified the original problem into two parts:

1) How does an applet access/read a file?
2) How do we demonstrate this graphically?

I answered the first part by showing how to read a file (I believed this is what the author tried to ask). With regards to the look-and-feel of the thermometer, I considered this as merely an animation that presents the data in some fancy ways. Therefore the choice of styles is upto you :-)

fvf
0
 
LVL 2

Expert Comment

by:John Fistere
Comment Utility
I see.  I will re-read your comments in that light.  However, I am at a different level of web development.  What I am really looking for is a thermometer component that I can simply place on a page and set the level.

Thanks,
John
0
 
LVL 4

Expert Comment

by:funnyveryfunny
Comment Utility
Then I suggest that you do some searches on the web or post new a question in EE's Java.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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

7 Experts available now in Live!

Get 1:1 Help Now