?
Solved

"Thermometer" Java Applet

Posted on 2004-03-27
8
Medium Priority
?
884 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 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