Solved

"Thermometer" Java Applet

Posted on 2004-03-27
8
819 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HSSFWorkbook cannot be resolved error 10 66
split string containing \r\n in Java 46 43
hibernate insert example 13 28
mysql jsp example issue 32 32
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

813 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

18 Experts available now in Live!

Get 1:1 Help Now