• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

MultiThreading

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
cortex
Asked:
cortex
1 Solution
 
majklCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now