Solved

MultiThreading

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 45
Is Applet the way to go for my drag and drop system? 8 35
Eclipse with various Java releases 7 48
junit example issue 2 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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