Current Position:Home > Problem with threads and simulation: please help

Problem with threads and simulation: please help

Update:10-11Source: network consolidation
please help me figure this out..
i have something like this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawShapes extends JApplet{
     private JButton choices[];
     private String names[]={"line", "square", "oval"};
     private JPanel buttonPanel;
     private DrawPanel drawingArea;
     private int width=300, height=200;
     public void init(){
          drawingArea=new DrawPanel(width, height);
          choices=new JButton[names.length];
          buttonPanel=new JPanel();
          buttonPanel.setLayout(new GridLayout(1, choices.length));
          ButtonHandler handler=new ButtonHandler();
          for(int i=0; i<choices.length; i++){
               choices=new JButton(names[i]);
          Container c=getContentPane();
          c.add(buttonPanel, BorderLayout.NORTH);
          c.add(drawingArea, BorderLayout.CENTER);
     }//end init
     public void setWidth(int w){
          width=(w>=0 ? w : 300);
     public void setHeight(int h){
          height=(h>=0 ? h : 200);
     /*public static void main(String args[]){
          int width, height;
               height=200; width=300;
          JFrame appWindow=new JFrame("An applet running as an application");
               new WindowAdapter(){
                    public void windowClosing(WindowEvent e){
          DrawShapes appObj=new DrawShapes();
          appWindow.setSize(width, height);
     }//end main*/
     private class ButtonHandler implements ActionListener{
          public void actionPerformed(ActionEvent e){
               for(int i=0; i<choices.length; i++){
}//end class DrawShapes
class DrawPanel extends JPanel{
     private int currentChoice=-1;
     private int width=100, height=100;
     public DrawPanel(int w, int h){
          width=(w>=0 ? w : 100);
          height=(h>=0 ? h : 100);
     public void paintComponent(Graphics g){
               case 0:     g.drawLine(randomX(), randomY(), randomX(), randomY());
               case 1: g.drawRect(randomX(), randomY(), randomX(), randomY());
               case 2: g.drawOval(randomX(), randomY(), randomX(), randomY());
     public void setCurrentChoice(int c){
     private int randomX(){
          return (int) (Math.random()*width);
     private int randomY(){
          return (int) (Math.random()*height);
}//end class drawPanel
That one's from a book. I used that code to start with my applet. Mine calls different merthod from the switch cases. Say I have:
case 0: drawStart(g); break;
public void drawStart(Graphics g){
  /* something here */
public void drawMain(graphics g){
g.drawString("test", x, y);
//here's where i'm trying to pause
//i've tried placing Thread.sleep between these lines
g.drawLine(x, y, a, b);
//Thread.sleep here
g.drawRect(x, y, 50, 70);
}I also need to put delays between method calls but I need to synchronize them. Am I doing it all wrong? The application pauses or sleeps but afterwards, it still drew everything all at once. Thanks a lot!

The Best Answer

It is. Sorry about that. Just answer any if you want to. I'd appreciate your help. Sorry again if it caused you anything or whatever. .n_n.
  • Problem with threads and simulation: please help Update:10-11

    please help me figure this out.. i have something like this: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawShapes extends JApplet{      private JButton choices[];      private String names[]={"line", "sq

  • Problem with Thread and InputStream Update:10-11

    Hi, I am having a problem with threads and InputStreams. I have a class which extends Thread. I have created and started four instances of this class. But only one instance finishes its' work. When I check the state of other three threads their state

  • Problem Getting Thread to Stop Update:11-30

    Hello, I am simulating a race. When the winner has been determined, I need to stop the other threads since the race is over. Here is the code that I have with regards to stopping the threads: public void simulateTravelTime() { try { Thread.sleep((int

  • A problem with threads Update:10-11

    I am trying to implement some kind of a server listening for requests. The listener part of the app, is a daemon thread that listens for connections and instantiates a handling daemon thread once it gets some. However, my problem is that i must be ab

  • A problem with Threads and MMapi Update:10-11

    I am tring to execute a class based on Game canvas. The problem begin when I try to Play both a MIDI tone and to run an infinit Thread loop. The MIDI tone "Stammers". How to over come the problem? Thanks in advance Kobi See Code example below: i

  • J2ME problem with threads Update:10-11

    Hi all, I would like to ask you for a help. I need to write a small program at my university. I started to write a midlet which function would be to countdown time for sports activities. I woul like to start a new thread - the one that counts down -

  • Problem with threads within applet Update:10-11

    Hello, I got an applet, inside this applet I have a singleton, inside this singleton I have a thread. this thread is running in endless loop. he is doing something and go to sleep on and on. the problem is, when I refresh my IE6 browser I see more th

  • Problem with Threads and a static variable Update:10-11

    I have a problem with the code below. I am yet to make sure that I understand the problem. Correct me if I am wrong please. Code functionality: A timer calls SetState every second. It sets the state and sets boolean variable "changed" to true. T

  • Problem with threads running javaw Update:10-11

    Hi, Having a problem with multi thread programming using client server sockets. The program works find when starting the the application in a console using java , but when using javaw the program doesnt die and have to kill it in

  • Problem with Threads and "plase wait..."-Window Update:10-11

    Hi everyone, I have a problem that I'm not able to solve in any way... I have a time-consuming task (a file decryption) which I execute in a separate thread; I've used the SwingWorker class, like suggested by sun-tutorial, and it works right. The pro