Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

newbie question concerning the "partical effect"

Thread view: 
Traddles000@gmail.com - 19 Apr 2005 09:54 GMT
hi, I have a fairly decent understanding of java applets.  I know how
to put images in the applet, move them around a bit, etc.  My problem
is, I'm trying to create an effect so that basically when I call some
method makeBang(x, y, image) it will create 2 to 6 random images, which
will start moving away from the source x,y with random speed and random
direction from the source.  Now, I got some code to work that kind of
did this, but it was absurdly messy and ended up slowing up my whole
system, I know there's a better way.  Now to make things even more
complicated, I need those images to start to fade after they've been
moving away from x,y for a second or two and return once the last image
has completly faded.  AND I need this method that does this to run
seperatly from the main program such that the code that calls
makeBang() can continue doing stuff, (namely calling makeBang() several
other times with different values) seperatly while all this image
movement is being done.

somehow I have a feeling I'm going to get a really complicated answer,
but if anyone is willing to explain to me how to do this I'm all about
learning new stuff
sanjay manohar - 20 Apr 2005 00:12 GMT
It is not hard if you understand 1) how to write a good paint() method,
2) how to use threads.
Totally UNTESTED sorry!- but should give you a clue as to how to do it.

PS and you should also make these 3 bits synchronized: 1) relevant
section in the MyPanel.paint method, 2)makeBang, and 3) the call to
currentFragments.remove().

PPS if you are going to have lots of explosions at a time, you will be
better of making makeBang create multiple Timers rather than multiple
Threads. Same logic applies of course.

class MyPanel extends JPanel{
// a fragment stores the information for one piece
class Fragment{
 int x, y, vx, vy, alpha=0;
 Image i;
 public Fragment(int initx, int inity, Image image){
  /* set up pos and velocities */
 }
 public void paint(Graphics2D g){
  Composite oldComposite = g.getComposite();
  g.setComposite(AlphaComposite.getInstance(
     AlphaComposite.SRC_OVER, alpha/10f);
  g.drawImage(i,x,y,null);
  g.setComposite(oldComposite);
 }
 public void update(Graphics g){
  x=x+vx; y=y+vy; alpha++;
 }
}

// each element is an array of fragments representing one explosion
Vector currentFragments = new Vector();

// add an array of fragments to currentFragments, and
// set up a thread running a RunBang to process this.
void makeBang(int x, int y, Image i, int maxFragments){
 Fragment[] f = new Fragment[(int)(Math.random()*maxFragments)];
 for(int i=0;i<f.length;i++) f[i]=new Fragment(x,y,i);
 currentFragments.add(f);
 new Thread(new RunBang(f)).start();
}

// Repeatedly calls Fragment.update() on each fragment
// and repaint() to paint the panel again
class RunBang implements Runnable{
 Fragment[] fragment;
 public RunBang(Fragment[] f){ fragment=f; }
 public void run(){
  for(int i=0;i<10;i++){
   for(int j=0;j<fragment.length;j++){
    fragment[j].update();
   }
   repaint();
   try{Thread.sleep(200);}
   catch(InterruptedException e){e.printStackTrace();}
  }
  currentFragments.remove(f);
 }
}

// standard method to paint your stuff
// and any current explosions.
public void paint(Graphics g){
  /* draw the main part of your game etc. here */
  // then:
  // paint each of the current explosions
  Graphics2D g2 = (Graphics2D) g;
  for(int i=0;i<currentFragments.size();i++){
   Fragment[] fragments=(Fragment[])currentFragments.get(i);
   for(int j=0;j<fragments.length;j++) fragments[j].paintMe(g2);
  }
}
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.