Solved

"Thermometer" Java Applet

Posted on 2004-03-27
8
849 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 241
servlet filter example 37 85
difference between sorce folder and folder in eclipise 3 45
What is the use of Forwarding Class in java 1 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

726 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