?
Solved

MultiThreading

Posted on 1997-04-01
1
Medium Priority
?
348 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 100 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

801 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