Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with threads

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Problem with threads

    My StarScape is still not running on its own after adding threading. I still have to click or resize to see the next frame of animation.

    new StarScape:
    // starscape.java
    // by Jon Crawford
    import Star.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.io.*;
    import java.math.*;
    import java.lang.*;
     
    public class StarScape extends JApplet implements Runnable
    {
      final int maxStars = 1000;
      String key = "";
      String valid;
      int index;
      double myX;
      double myY;
      int myRadius;
      int myLayer;
      int rectX;
      int rectY;
      int rectXX;
      int rectYY;
      private long myStarDelay = 20;
      Star stars[] = new Star[maxStars+1];//make 1000 star objects
     
      public void init()
      {
        for (index = 0; index < maxStars; index++)
        {      
          stars[index] = new Star();//fill array with stars
        }//end for 
      }//end init
     
      public void start() 
      {
        Thread t = new Thread();
        t.start();
      }
     
      public void run() 
      {
        Graphics g = getGraphics();
        do 
        {
          paint(g);
          repaint();
        }while (key == "");
      }
     
      public void paint(Graphics g)
      {
        g.setColor(Color.black);
        g.fillRect(0,0,800,600);// make the background black
        for (index = 0; index < maxStars; index++) 
        {
          stars[index].moveStar();//move the star
          valid = stars[index].starValid();
          if (valid == "false")
          {//is star out of bounds?
            stars[index].resetStar();
          }  
          myX = stars[index].getStarX();
          myY = stars[index].getStarY();
          myRadius = stars[index].getStarRadius();//get data agian because star was moved
          myLayer = stars[index].getStarLayer();//layer just denotes shade of star. 
          if (myLayer == 3) 
          {
            g.setColor(Color.white);
          }else if (myLayer == 2) 
          {
            g.setColor(Color.lightGray);
          }else if (myLayer == 1) 
          {
            g.setColor(Color.darkGray);
          }else 
          {
            g.setColor(Color.white);//these set color based on layer
          }
          rectX = (int)(myX) - myRadius + 399;
          rectY = (int)(myY) - myRadius + 299;
          rectXX = myRadius * 2;
          rectYY = myRadius * 2;//set up the new star I mean rectangle
          g.fillOval(rectX, rectY, rectXX, rectYY);//draw star
        }//end for
      }//end paint
     
    }//end starscape
    // star.java
    // by Jon Crawford
    package Star;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import java.io.*;
    import java.math.*;
     
    public class Star {
     
      private final int starSpeed = 1;
      private final int starRadius = 1;
      private final int starSteps = 0;
      private final float[] invalidValues = new float[]{ -399f, -299f, 399f, 299f, 0f, 0f};
      private String isValid = "false";
      private double starAngle;
      private double starField[] = new double[9];
      private double starX;
      private double starY;
      private int starLayer;
      public Star()
      {
        starX = Math.random() * 200 - 99;//star starts in a small square
        starY = Math.random() * 200 - 99;//in the center
        starLayer = (int)(Math.random() * 3);//random layer
        starAngle = (Math.sqrt((starX * starX)+(starY * starY)));//calculate the hypotenuse
        starField[0] = starX;//set data to star's arrary
        starField[1] = starY;//this is for current position
        starField[2] = starX / starAngle * starSpeed;//these set the next position
        starField[3] = starY / starAngle * starSpeed;//this is reltive current position
        starField[4] = starLayer;//back, mid, or foreground layer
        starField[5] = starRadius;//star starting size
        starField[6] = starSpeed * starLayer;//the starting speed
        starField[7] = starSteps;//star has taken 0 steps, every 5 steps star grows and speeds up
        starField[8] = starAngle;//store the hypotenuse
      }//end constructor
     
      public void moveStar() 
      {//mutator method
        System.out.println("") ;    
        starField[0] = starField[0] + starField[2];//next position becomes the curent position 
        starField[1] = starField[1] + starField[3];       
        starField[2] = starField[0] / starField[8] * starField[6];//new next position
        starField[3] = starField[1] / starField[8] * starField[6];
        starField[7] = starField[7] + 1;//increment steps
        if (starField[7] / 5 == (int)(starField[7] / 5) && starField[5] < 20)
        {//if star has taken 5 steps
          starField[5] = starField[5] + 1;//increment size
          starField[6] = starField[6] + 1;//increment speed
        }//end if      
      }//end moveStar
     
      public void resetStar()
      {//resetStar method
        starX = Math.random() * 200 - 99;//set new starting position
        starY = Math.random() * 200 - 99;
        starLayer = (int)(Math.random() * 3);//new random layer
        starAngle = (Math.sqrt((starX * starX)+(starY * starY)));//calculate new hypotenuse
        starField[0] = starX;//set data to star's array
        starField[1] = starY;//same as default constructor
        starField[2] = starX / starAngle * starSpeed;
        starField[3] = starY / starAngle * starSpeed;
        starField[4] = starLayer;
        starField[5] = starRadius;
        starField[6] = starSpeed * starLayer;
        starField[7] = starSteps;
        starField[8] = starAngle;
      }//end resetStar
     
      public String starValid()
      {//isValid method
        if(starField[0] > invalidValues[0] && starField[1] > invalidValues[1] && starField[0] < 
     
    invalidValues[2] && starField[1] < invalidValues[3])
        {//in bounds?
          isValid = "true";
        }else
        {//out of bounds
          isValid = "false";
        }//end if
        return isValid;
      }//end is valid
     
      public int getStarX() 
      {//get method
        return (int)(Math.round(starField[0]));
      }//end getStarX
     
      public int getStarY() 
      {//get method
        return (int)(Math.round(starField[1]));
      }//end getStarY
     
      public int getStarRadius() 
      {//get method
        return (int)(Math.round(starField[5]));
      }//end getStarRadius
     
      public int getStarLayer() 
      {//get method
        return (int)(Math.round(starField[4]));
      }//end getStarLayer
     
    }//end class star
    Last edited by Spidey1980; August 19th, 2011 at 09:42 PM.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Problem with threads

    My StarScape is still not running on its own after adding threading
    What is the purpose of multithreading for your program?

    Anyway, this code snippet is wrong:

      public void start() 
      {
        Thread t = new Thread();
        t.start();
      }

    You created a new empty thread then start it. This is useless because the thread does nothing.

    Following is a proper way:


      public void start() 
      {
        Thread t = new Thread(this);
        t.start();
      }

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:32 PM.

  3. The Following User Says Thank You to ha.minh.nam For This Useful Post:

    Spidey1980 (August 20th, 2011)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with threads

    Also get rid of the call to getGraphics and to paint. The call to repaint will automatically cause a call to be made to your paint method with a Graphics object.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Spidey1980 (August 20th, 2011)

  6. #4
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Problem with threads

    I musta been tired last night. Thread(this) gave me a "variable this not found in StarScape.class" so I had removed it.

    however this morning I tried it and it works perfectly. I guess I just can't type that late at night.

    Thank you issue solved check out my new show off thread i'm about to start......

Similar Threads

  1. Threads in Web Crawler
    By relion65 in forum Threads
    Replies: 5
    Last Post: June 28th, 2011, 11:48 PM
  2. threads
    By crazed8s in forum Threads
    Replies: 2
    Last Post: December 14th, 2010, 05:33 AM
  3. Threads and JDBC
    By shorawitz in forum Threads
    Replies: 1
    Last Post: November 11th, 2010, 09:24 PM
  4. Problem with designing threads & abstraction
    By gilme in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 14th, 2010, 06:48 AM
  5. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM