Gestion des processus legere en java

Signaler
Messages postés
2
Date d'inscription
lundi 4 janvier 2010
Statut
Membre
Dernière intervention
26 avril 2010
-
Messages postés
2
Date d'inscription
lundi 4 janvier 2010
Statut
Membre
Dernière intervention
26 avril 2010
-
bonjour

dans cette exemple de code je cherche d'augmenter le nombre de processus poisson
svp aidez moi de faire ceci
merci d'avance:




import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import java.util.Vector;
public class Slapshot2 extends Frame implements
ActionListener, Runnable
{
MenuBar menubar;
Menu menu1;
MenuItem menuitem0,menuitem1,menuitem2,menuitem3;
MediaTracker tracker;
Image aquariumImage, memoryImage;
Graphics memoryGraphics;
Image [] fishImages= new Image[2];
int numberFish,w,h;
Thread thread;
OneFish Fish1;
Vector fishes;
boolean runOK;
int sleepTime;
public Slapshot2()
{
numberFish = 4;
sleepTime=100;
fishes = new Vector();
runOK=true;
menubar = new MenuBar();
menu1 = new Menu("File");
menuitem0 = new MenuItem("Start");
menu1.add(menuitem0);
menuitem0.addActionListener(this);
menuitem1 = new MenuItem("End");
menu1.add(menuitem1);
menuitem1.addActionListener(this);
menuitem2 = new MenuItem("Set speed...");
menu1.add(menuitem2);
menuitem3 = new MenuItem("Exit");
menu1.add(menuitem3);
menuitem3.addActionListener(this);
menubar.add(menu1);
setMenuBar(menubar);
setSize(300,300);
setTitle("Slapshot");
setVisible(true);
tracker=new MediaTracker(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
runOK=false;
System.exit(0);}
}
);
setLayout(null);
fishImages[0] =
Toolkit.getDefaultToolkit().getImage("fish2.gif");
tracker.addImage(fishImages[0], 1);
try {
tracker.waitForID(0);
if (tracker.isErrorAny ()){
throw new IllegalArgumentException ("Images non
chargees");
}
}catch (Exception ex) {
System.out.println(ex.getMessage());
}
w=getSize().width;
h=getSize().height;
aquariumImage=createImage(getSize().width, getSize().height);
aquariumImage.getGraphics().fillRect(0,0,w,h);
setResizable(false);
setVisible(true);
memoryImage = createImage(getSize().width, getSize().height);
memoryGraphics = memoryImage.getGraphics();
thread= new Thread(this);
}
public void run()
{
Rectangle edges = new Rectangle(0 + getInsets().left, 0
+ getInsets().top, getSize().width - (getInsets().left
+ getInsets().right), getSize().height - (getInsets().top
+ getInsets().bottom));
for (int loopIndex = 0; loopIndex < numberFish; loopIndex++){
int dec=loopIndex*20;
fishes.add(new OneFish(dec,fishImages[0], edges, this));
try {
Thread.sleep(20);
}
catch (Exception exp) {
System.out.println(exp.getMessage());
}
}
OneFish fish;
while (runOK){
for (int loopIndex=0;loopIndex<numberFish;loopIndex++){
fish=(OneFish)fishes.elementAt(loopIndex);
fish.run();
}
try {
Thread.sleep(sleepTime);
}
catch (Exception exp){
System.out.println(exp.getMessage());
}
repaint();
}
}
public static void main(String[] args) {
new Slapshot2();
}
public void update(Graphics g){
memoryGraphics.drawImage(aquariumImage,0,0,this);
for (int loopIndex=0;loopIndex<numberFish;loopIndex++){
((OneFish)fishes.elementAt(loopIndex)).drawFishImage(memoryGra
phics);
}
g.drawImage(memoryImage,0,0,this);
}
public void paintComponent(Graphics g) {
g.drawImage(aquariumImage, 0, 0,this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource()==menuitem0){
thread.start();
}
if (event.getSource()==menuitem1){
thread.stop();
repaint();
}
if (event.getSource()==menuitem3){
thread.stop();
System.exit(0);
}
}
}
class OneFish implements Runnable{
Component tank;
Image image1;
Point location;
Rectangle edges;
Random random;
int pos;
public OneFish(int dec,Image image1,Rectangle edges,Component
tank){
random =new Random(System.currentTimeMillis());
this.tank=tank;
this.image1=image1;
this.edges=edges;
pos=40+dec;
this.location=new Point(1,pos);
}
public void run() {
int x,y;
x=location.x+2;
if (x<edges.width-1){
location.x=x;
}else {location.x=1;}
location.y=pos;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void drawFishImage(Graphics g)
{
g.drawImage(image1, location.x, location.y, tank);
}
}

1 réponse

Messages postés
2
Date d'inscription
lundi 4 janvier 2010
Statut
Membre
Dernière intervention
26 avril 2010

aidez moi svp c urgent ...