Solved

"Thermometer" Java Applet

Posted on 2004-03-27
8
805 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
ID: 10699083
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
ID: 10699095
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
ID: 11743410
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
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.

 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11746989
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
ID: 11747724
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
ID: 11754449
Then I suggest that you do some searches on the web or post new a question in EE's Java.
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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

862 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

30 Experts available now in Live!

Get 1:1 Help Now