Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

To Thread

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear friend
Help me out
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class stars_2 extends Applet implements Runnable
{
Thread t1;int x,y;Thread t2;
public void init()
{ setBackground(Color.black); }
public void start()
{ t1=new Thread(this,"tone"); t1.setName("tone"); t1.start();
t2=new Thread(this,"ttwo"); t1.setName("ttwo"); t2.start();
}

public void run()
{ while(true)
{
try{ t1.sleep(50);}catch(Exception e){}
try{ t2.sleep(50);}catch(Exception e){}
x=(int)((getSize().width)*Math.random());
y=(int)((getSize().height)*Math.random());
repaint();
}
}
public void update(Graphics g)
{ paint(g);}
public void paint(Graphics g)
{ /*
if (this.getName()=="tone")
{ g.setColor(Color.yellow);
g.drawString("*"+getName(),x,y); return;;
}
if (this.getName()=="ttwo")
{ g.setColor(Color.yellow);
g.drawString("*"+getName(),x,y);return;
}
*/
int q=(int)(5*Math.random());
switch(q)
{ case 1: g.setColor(Color.yellow);break;
case 2: g.setColor(Color.red);break;
case 3: g.setColor(Color.green);break;
case 4: g.setColor(Color.blue);break;
case 5: g.setColor(Color.white);break;
}
g.drawString("*",x,y);
// g.drawString("*"+getName()+Thread.activeCount(),x,y);
// g.drawString("*"+getName(),x,y);
}
}
the program works but i want to uncomment the thread code and
i want the same functionality ie print different colored stars
using different thread and not using random function. how do i do it.
help
bapi


 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class stars_2 extends Applet implements Runnable
{
Thread t1;int x,y;Thread t2;
public void init()
{ setBackground(Color.black); }
public void start()
{ t1=new Thread(this,"tone"); t1.setName("tone"); t1.start();
}
public void run()
{
while(Thread.currentThread() != null)
{
try{ t1.sleep(50);}catch(Exception e){}
try{ t2.sleep(50);}catch(Exception e){}
x=(int)((getSize().width)*Math.random());
y=(int)((getSize().height)*Math.random());
repaint();
}
}
public void update(Graphics g)
{ paint(g);}
public void paint(Graphics g)
{
g.setColor(new Color(1 + (int)(Math.random() * 255),
1 + (int)(Math.random() * 255),
1 + (int)(Math.random() * 255)));
g.drawString("*",x,y);
}
}

------------------
Val SCJP
going for SCJD
 
    Bookmark Topic Watch Topic
  • New Topic