Solved

MultiThreading

Posted on 1997-04-01
1
340 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 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:
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

17 Experts available now in Live!

Get 1:1 Help Now