Solved

MultiThreading

Posted on 1997-04-01
1
347 Views
Last Modified: 2010-04-16
I want to creat a continuous display that consists of falling sparkles (like snow on TV).  I have written the follwing code to do this.  However, my code requires that I call MultiThread for each sparkle that I want to display, which seems to be a fairly inelegant implementation.  I would like to have several hundred sparkles, apearing at random locations, on the screen at any instant.  Any ideas on a more elegant solution?

Here is my code:

// Sparkles using multiple threads
import java.awt.*;
import java.applet.Applet;
import java.lang.Math;

public class Sparkles extends Applet implements Runnable {

  String name;  //name of thread
  Thread t;     //instance of thread

  public void MultiThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    t.start();  //start the sparkle thread
  }

  // This is the entry point for each sparkle thread
  public void run() {
    repaint();
  }  // end run

  // MakeSparkle
  public void paint(Graphics g) {

    int x = 0;
    int y = 0;
    double dx;
    double dy;
    int OvalWidth = 10;
    int OvalHeight = 10;

    g.setColor(Color.red);

    dx = Math.random() * 500;
    dy = Math.random() * 500;
    x = (int) dx;
    y = (int) dy;
    for (int n = 0; n < 10; n++){
      y = y - n;
      g.fillOval(x, y, OvalWidth, OvalHeight);
    };
  }  //end MakeSparkle

  public void init() {

    validate();
    setBackground(Color.white);

    MultiThread("name0");
    MultiThread("name1");
    MultiThread("name2");
    MultiThread("name3");
    MultiThread("name4");
    MultiThread("name5");
    MultiThread("name6");
    MultiThread("name7");
    MultiThread("name8");
    MultiThread("name9");
  }
}
0
Comment
Question by:cortex
[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
1 Comment
 
LVL 2

Accepted Solution

by:
majkl earned 50 total points
ID: 1219375
Well. If you use thread for each sparkle, then your applet will eat up lot of resources and run very slowly :((
This isn't the best solutions. Why use for each sparkle one thread ?? Probably you want to move each one with different speed? This can also be done with random generator.
Create class, that will hold informations about position of sparkle and then move every sparkle with random help.
See how it is done on my page. It is simulation of starfield. Stars are  moving with constant velocity but with little change you can do it that they will move random and aply it in your program.This will do. Thread for every sparkle was not such good idea.

well, here is my url, you will find also source code there :))
http://storm.uni-mb.si/docs/personal/milan/ 
 
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

688 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