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 13 of 13

Thread: Translating old program to java

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

    Default Translating old program to java

    Ok so this is a personal project. I had made a QBasic program years ago and now i am translating it to be a java applet so I can show it off. I found a book on java programming and went through it completely before I started. I've got it down to just 7 errors.

    Here is the code:

    /* 
      StarScape Applet By Jon Crawford
      first Java tranlastion
     
    */
     
    import java.util.*;
    import java.util.Random;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class StarScape extends JApplet {
      public void paint(Graphics g){
        int maxStars = 200, starSpeed = 2, starRadius = 1, speedA;
        int v, w, c, l;
        int stars[][] = new int[maxStars][7];
        setBackground(Color.black);
        do {
          for(int ctrl = 0; ctrl < maxStars; ctrl++) {
            //if star is out of range, and all of them are at start,
            //erase last star position, and give star a new random start
            if( stars[ctrl][1] <= 1 || stars[ctrl][2] <= 1 || stars[ctrl][1] >= 799 || stars[ctrl][2] >= 599) {
              g.setColor(Color.black);
              g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
              Random rand = new Random();
              //start in small box in center
              v = rand.nextInt(200) - 99;
              w = rand.nextInt(200) - 99;
              if (v == 0) {
                v = 1;
              }
              if (w == 0) {
                w = 1;
              }
              //random color variable, specifies which of the three layers this star is in.
              //white = front, light grey = middle layer, dark grey = back layer
              c = rand.nextInt(3);
              stars[ctrl][7] = c;
              //adjust values to big box
              stars[ctrl][1] = v + 399;
              stars[ctrl][2] = w + 299;
              stars[ctrl][5] = starRadius;
              stars[ctrl][6] = rand.nextInt(10);
              //get angle
              //first error, these have been declared integers, yet compiler says "precision loss, these are doubles"? 
              l = Math.sqrt((v * v)+(w * w));
              //adjust speed acording to color/layer
              switch (stars[ctrl][7]) {
                case 3:
                  speedA = starSpeed;
                  break;
                case 2:
                  speedA = starSpeed / 4;
                  break;
                case 1:
                  speedA = starSpeed / 2;
                  break;
                default:
                  speedA = starSpeed;
              }//end case
              //calculate new star position from old star position
              stars[ctrl][3] = v / l * speedA;
              stars[ctrl][4] = w / l * speedA;
            }//end if
            //erase old star
            g.setColor(Color.black);
            g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
            //set old position to new position
            stars[ctrl][1] = stars[ctrl][1] + stars[ctrl][3];
            stars[ctrl][2] = stars[ctrl][2] + stars[ctrl][4];
            stars[ctrl][6] = stars[ctrl][6] + 1;
            //stars get faster as they get closer every 15 steps
            if (stars[ctrl][6] / 15 == (int)(stars[ctrl][6] / 15) && stars[ctrl][5] < 5) {
              stars[ctrl][5] = stars[ctrl][5] + 1;
            }
            //use color/layer number to set the color
            switch (stars[ctrl][7]) {
              case 1:
                g.setColor(Color.white);
                break;
              case 2:
                g.setColor(Color.lightGray);
                break;
              case 3:
                g.setColor(Color.darkGray);
                break;
            }//end case
            //draw new Star
            g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
          }//end for
        //check for key press
        //this is not working either
        keyCode = kE.getKeyText(kE.getKeyCode());
        }while (keyCode.equalsIgnoreCase ("")); //continue until a key is pressed
      }//end class
    }//end Starscape

    I have also comment the sections where errors are found. Please help I am getting frustrated.

    EDIT: one error fixed, code updated to reflect fix.
    Last edited by Spidey1980; August 7th, 2011 at 11:24 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Translating old program to java

    It would be easier if your copied and pasted the full and exact error message separately and not embed what you think the error message says in the middle of your code so we have to hunt them down.

    //stars get faster as they get closer every 15 steps
    //compiler says a lot of errors on this line, including "symbol not found, method parseInt(Int) not found"
    if (stars[ctrl][6] / 15 = Integer.parseInt(stars[ctrl][6] / 15)

    You have two problems on this line. The stars array holds int values so why are you trying to parseInt them? One equals sign is assingment. Two equals sign is equality.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    Spidey1980 (August 7th, 2011)

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

    Default Re: Translating old program to java

    for ex: 20/15 <> 20/15 with no decimals, however 30/15 == 30/15 with no decimals.

    Your right, I meant double equals, but that did not fix the error. my .java now contains this fix.

    how can I copy and paste from CMD promt?

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Translating old program to java

    Quote Originally Posted by Spidey1980 View Post
    for ex: 20/15 <> 20/15 with no decimals, however 30/15 == 30/15 with no decimals.
    If you are talking about 9 / 2 gives 4 and not 4.5 that is because you are doing integer division and the result will be an integer. If you want a floating point result then you will need to do floating point division instead. Cast at least one value to a double or float.

    how can I copy and paste from CMD promt?
    In the top left hand corner is a small icon. Left click on that and go to the edit submenu.
    Improving the world one idiot at a time!

  6. The Following User Says Thank You to Junky For This Useful Post:

    Spidey1980 (August 7th, 2011)

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

    Default Re: Translating old program to java

    The Errors:
     
    C:\JSource\Chapter02>javac StarScape.java
    StarScape.java:46: possible loss of precision
    found   : double
    required: int
              l = Math.sqrt((v * v)+(w * w));
                           ^
    StarScape.java:92: cannot find symbol
    symbol  : variable keyCode
    location: class StarScape
        keyCode = kE.getKeyText(kE.getKeyCode());
        ^
    StarScape.java:92: cannot find symbol
    symbol  : variable kE
    location: class StarScape
        keyCode = kE.getKeyText(kE.getKeyCode());
                                ^
    StarScape.java:92: cannot find symbol
    symbol  : variable kE
    location: class StarScape
        keyCode = kE.getKeyText(kE.getKeyCode());
                  ^
    StarScape.java:93: cannot find symbol
    symbol  : variable keyCode
    location: class StarScape
      }while (keyCode.equalsIgnoreCase ("")); //continue until a key is pressed
              ^
    5 errors
     
    C:\JSource\Chapter02>

    EDIT: one error fixed, now removed from list
    Last edited by Spidey1980; August 7th, 2011 at 11:20 PM.

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

    Default Re: Translating old program to java

    Next issue. I've declared l, v, and w as integers. But the compiler says that they are doubles and need to be integers. This one is really confusing.

    And if anyone could tell me a better way to check for a key press it would be greatly appreciated. I want the Applet to run until a key is pressed.

    Edit: took out junk, made it simple
    Last edited by Spidey1980; August 7th, 2011 at 11:33 PM.

  9. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Translating old program to java

    possible loss of precision

    The return value of the Math.sqrt method is a double. You are trying to assign it to an int. This means that if the method returns 1.5 and you cram it into an int it can only store 1 and the 0.5 is lost.

    All other errors are due to you trying to use a variable that has not been declared or you declared it in the wrong scope.
    Improving the world one idiot at a time!

  10. The Following User Says Thank You to Junky For This Useful Post:

    Spidey1980 (August 7th, 2011)

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

    Default Re: Translating old program to java

    Thank you! adding (int) before Math.sqrt() fixed that one. Still having trouble with the key press checker.

    I got it to compile after commenting out the key checker. Guess CTRL ^C will have to do for now
    Last edited by Spidey1980; August 8th, 2011 at 12:21 AM.

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

    Default Re: Translating old program to java

    here it is, this will compile, but output using AppletViewer nor via html is not as expected.
    note: my resolution is high, so that's why I have this to be 800x600.

     
    /* 
      StarScape Applet By Jon Crawford
      first Java tranlastion
     
    */
     
    import java.util.*;
    import java.util.Random;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class StarScape extends JApplet {
      public void paint(Graphics g){
        int maxStars = 200, starSpeed = 2, starRadius = 1, speedA;
        int v, w, c, l;
        int stars[][] = new int[maxStars+1][8];
        String i = "";
        setBackground(Color.black);
        do {
          for(int ctrl = 0; ctrl < maxStars; ctrl++) {
            //if star is out of range, and all of them are at start,
            //erase last star position, and give star a new random start
            if( stars[ctrl][1] <= 1 || stars[ctrl][2] <= 1 || stars[ctrl][1] >= 799 || stars[ctrl]
     
    [2] >= 599) {
              g.setColor(Color.black);
              g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
              Random rand = new Random();
              //start in small box in center
              v = rand.nextInt(200) - 99;
              w = rand.nextInt(200) - 99;
              if (v == 0) {
                v = 1;
              }
              if (w == 0) {
                w = 1;
              }
              //random color variable, specifies which of the three layers this star is in.
              //white = front, light grey = middle layer, dark grey = back layer
              c = rand.nextInt(3);
              stars[ctrl][7] = c;
              //adjust values to big box
              stars[ctrl][1] = v + 399;
              stars[ctrl][2] = w + 299;
              stars[ctrl][5] = starRadius;
              stars[ctrl][6] = rand.nextInt(10);
              //get angle
              l = (int)(Math.sqrt((v * v)+(w * w)));
              //adjust speed acording to color/layer
              switch (stars[ctrl][7]) {
                case 3:
                  speedA = starSpeed;
                  break;
                case 2:
                  speedA = starSpeed / 4;
                  break;
                case 1:
                  speedA = starSpeed / 2;
                  break;
                default:
                  speedA = starSpeed;
              }//end case
              //calculate new star position from old star position
              stars[ctrl][3] = v / l * speedA;
              stars[ctrl][4] = w / l * speedA;
            }//end if
            //erase old star
            g.setColor(Color.black);
            g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
            //set old position to new position
            stars[ctrl][1] = stars[ctrl][1] + stars[ctrl][3];
            stars[ctrl][2] = stars[ctrl][2] + stars[ctrl][4];
            stars[ctrl][6] = stars[ctrl][6] + 1;
            //stars get faster as they get closer every 15 steps
            if (stars[ctrl][6] / 15 == (int)(stars[ctrl][6] / 15) && stars[ctrl][5] < 5) {
              stars[ctrl][5] = stars[ctrl][5] + 1;
            }
            //use color/layer number to set the color
            switch (stars[ctrl][7]) {
              case 1:
                g.setColor(Color.white);
                break;
              case 2:
                g.setColor(Color.lightGray);
                break;
              case 3:
                g.setColor(Color.darkGray);
                break;
            }//end case
            //draw new Star
            g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
          }//end for
          //check for key press
          //i = i + e.getKeyChar();
        }while (i == "");
      }//end paint
    }//end Stascape

    StarScape.html:
    <html>
    	<head>
    		<title>StarScape Applet</title>
    	</head>
    	<body>
    		<h1 align=center>StarScape Applet</h1>
    		<center>
    			<applet name="StarScape" 
    				code="StarScape.class" 
    				width=800 
    				height=600>
    			</applet>
    		</center>
    	</body>
    </html>

    And here is the original Qbasic code, this reads variables from a text file (maxStars, start size or radius, and start speed) these have been hard coded in the java code:
    'Starscape by Jon Crawford
    'Starscap.txt format is variablle to set
    'on one line and the value on the next
    'line. Variables can go in any order.
     
    REM $INCLUDE: 'future.bi'
     
    OPEN "I", 1, "starscap.txt"
    FOR c = 1 TO 3
    	INPUT #1, B$
    	IF B$ = "maxstars" THEN INPUT #1, maxstars
    	IF B$ = "speed" THEN INPUT #1, speed
    	IF B$ = "radius" THEN INPUT #1, Radius
    NEXT
    CLOSE #1
     
    Set1024x768 8
    a$ = ""
    a = 7
    DIM stars(maxstars, a)
     
    DO
     FOR n = 1 TO maxstars
    	IF stars(n, 1) < 1 OR stars(n, 2) < 1 OR stars(n, 1) > 1024 OR stars(n, 2) > 768 THEN
    		Future.FILLCIRCLE INT(stars(n, 1)), INT(stars(n, 2)), stars(n, 5), 0
    		RANDOMIZE TIMER
    		v = INT(RND(1) * 200) - 99: w = INT(RND(1) * 200) - 99
    		IF v = 0 THEN v = 1: IF w = 0 THEN w = 1
    		c = INT(RND(1) * 3) + 1
    		SELECT CASE c
    			CASE 1
    				stars(n, 7) = 15
    			CASE 2
    				stars(n, 7) = 8
    			CASE 3
    				stars(n, 7) = 7
    		END SELECT
    		stars(n, 1) = v + 512: stars(n, 2) = w + 384
    		stars(n, 5) = Radius: stars(n, 6) = INT(RND(1) * 10)
    		l = SQR((v * v) + (w * w))
    		SELECT CASE stars(n, 7)
    			CASE 15
    				speeda = speed
    			CASE 8
    				speeda = speed / 4
    			CASE 7
    				speeda = speed / 2
    		END SELECT
    		stars(n, 3) = v / l * speeda: stars(n, 4) = w / l * speeda
    	END IF
    	Future.FILLCIRCLE INT(stars(n, 1)), INT(stars(n, 2)), stars(n, 5), 0
    	stars(n, 1) = stars(n, 1) + stars(n, 3)
    	stars(n, 2) = stars(n, 2) + stars(n, 4)
    	stars(n, 6) = stars(n, 6) + 1
    	IF stars(n, 6) / 15 = INT(stars(n, 6) / 15) AND stars(n, 5) < 5 THEN stars(n, 5) = stars(n, 5) + 1
    	Future.FILLCIRCLE INT(stars(n, 1)), INT(stars(n, 2)), stars(n, 5), stars(n, 7)
     NEXT
     a$ = INKEY$
    LOOP UNTIL a$ <> ""
    Palette.FadeOut
    ReSetScreen
    END

    the StarScap.txt for the Qbasic:

    [code]
    maxstars
    300
    radius
    2
    speed
    15
    [\code]

    This is a beautiful program in Qbasic, if anyone still has that on their system feel free to compile the QB code and run it via dosbox. You will need the future.bi QB library to run it. (like a class in Java)

    I'll work more on this tomorrow.
    Real quick question, setting the background color to black did not work, what am I doing wrong?!?!
    setBackground(Color.black);
    Last edited by Spidey1980; August 8th, 2011 at 12:50 AM.

  13. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Translating old program to java

    Quote Originally Posted by Spidey1980 View Post
    Real quick question, setting the background color to black did not work, what am I doing wrong?!?!
    setBackground(Color.black);
    Java is case sensitive.
    Improving the world one idiot at a time!

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

    Default Re: Translating old program to java

    Color (Java 2 Platform SE v1.4.2)

    Colors can be either caps or lower, i.e BLACK or black (not BlAcK, ect.)

    Anyways I found that setBackground(Color.black) does not set the background color. However this does:

    g.setColor(Color.black);
    g.fillRect(0,0,799,599);

    It's getting closer, the first few stars move. You can see it now that the white stars are on a field of black:
    (compile and run if you wish)
    /* 
      StarScape Applet By Jon Crawford
      first Java tranlastion
     
    */
     
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class StarScape extends JApplet {
      public void paint(Graphics g){
        int maxStars = 200, starSpeed = 2, starRadius = 1, speedA;
        int v, w, c, l;
        int stars[][] = new int[maxStars+1][8];
        String i = "";
        g.setColor(Color.black);
        g.fillRect(0,0,799,599);
        do {
          for(int ctrl = 0; ctrl < maxStars; ctrl++) {
            //if star is out of range, and all of them are at start,
            //erase last star position, and give star a new random start
            if( stars[ctrl][1] <= 1 || stars[ctrl][2] <= 1 || stars[ctrl][1] >= 799 || stars[ctrl]
     
    [2] >= 599) {
              g.setColor(Color.black);
              g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
              Random rand = new Random();
              //start in small box in center
              v = rand.nextInt(200) - 99;
              w = rand.nextInt(200) - 99;
              if (v == 0) {
                v = 1;
              }
              if (w == 0) {
                w = 1;
              }
              //random color variable, specifies which of the three layers this star is in.
              //white = front, light grey = middle layer, dark grey = back layer
              c = rand.nextInt(3);
              stars[ctrl][7] = c;
              //adjust values to big box
              stars[ctrl][1] = v + 399;
              stars[ctrl][2] = w + 299;
              stars[ctrl][5] = starRadius;
              stars[ctrl][6] = rand.nextInt(10);
              //get angle
              l = (int)(Math.sqrt((v * v)+(w * w)));
              //adjust speed acording to color/layer
              switch (stars[ctrl][7]) {
                case 3:
                  speedA = starSpeed;
                  break;
                case 2:
                  speedA = starSpeed / 4;
                  break;
                case 1:
                  speedA = starSpeed / 2;
                  break;
                default:
                  speedA = starSpeed;
              }//end case
              //calculate new star position from old star position
              stars[ctrl][3] = v / l * speedA;
              stars[ctrl][4] = w / l * speedA;
            }//end if
            //erase old star
            g.setColor(Color.black);
            g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
            //set old position to new position
            stars[ctrl][1] = stars[ctrl][1] + stars[ctrl][3];
            stars[ctrl][2] = stars[ctrl][2] + stars[ctrl][4];
            stars[ctrl][6] = stars[ctrl][6] + 1;
            //stars get faster as they get closer every 15 steps
            if (stars[ctrl][6] / 15 == (int)(stars[ctrl][6] / 15) && stars[ctrl][5] < 5) {
              stars[ctrl][5] = stars[ctrl][5] + 1;
            }
            //use color/layer number to set the color
            switch (stars[ctrl][7]) {
              case 1:
                g.setColor(Color.white);
                break;
              case 2:
                g.setColor(Color.lightGray);
                break;
              case 3:
                g.setColor(Color.darkGray);
                break;
            }//end case
            //draw new Star
            g.drawOval(stars[ctrl][1], stars[ctrl][2], starRadius, starRadius);
          }//end for
          //check for key press
          //i = i + e.getKeyChar();
        }while (i == "");
      }//end paint
    }//end Stascape

    I probably have a few typos in my math, At least I have the working QB version to go from. I would think the math to be the same save for syntax across programming languages.
    Last edited by Spidey1980; August 8th, 2011 at 08:25 PM.

  15. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Translating old program to java

    Quote Originally Posted by Spidey1980 View Post
    Colors can be either caps or lower, i.e BLACK or black (not BlAcK, ect.)
    Well **** me. I always thought the constants in the Color class were all uppercase. Using setBackground should work. Did you get an error or did it just not work. If you got an error post it here. If it didn't work then perhaps you got things in the wrong order.
    Improving the world one idiot at a time!

  16. The Following User Says Thank You to Junky For This Useful Post:

    Spidey1980 (August 10th, 2011)

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

    Default Re: Translating old program to java

    I read somewhere that it it sets the far backgroud color, it's a windows bug not Java. I could see the black background briefly when resizing the window.

    Right now i'm working on drawing the stars correctly, and trying to figure out what went wrong in my vector math in translation. It's looks good, like a straight over copy with a little different syntax, but it just is not working.

    Note, I start my star in a 200x200 box adjusted to -99 to 99 by -99 to 99 so I have 0,0 in the center. Vector math would not work otherwise. AFTER the angle is calculated then the coordintas are adjusted to the center of a 800x600 box that has 0,0 at top left to match the screen pixel coordinate system.

    Also, can I use math.hypot(x, y) since it's output is the same as math.sqrt(x * x, y * y)? (source: Math (Java Platform SE 6)). I didn't do well past algebra 2. Yes, somebody else gave me the vector math (thank you Dr. Evil, Omni-bot creator), I was using (x-1,y-1) and (x-1,y+1) and (x+1,y-1) and (x+1,y+1) which allows for only 4 angles, vector math of course allows for thousands of angles within the 360 degree circle.

    Do you think I should rewrite it so that it is more object orientated? right now it is more like an old school code from before object programming. Could that be the problem with my math?
    Last edited by Spidey1980; August 10th, 2011 at 08:57 AM.

Similar Threads

  1. Need help on Java program
    By middleonedb2001 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 1st, 2011, 09:42 PM
  2. having problems with my pig latin translator (the translating portion)
    By WontonSoup in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2011, 06:45 PM
  3. java program help
    By lolalala in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2010, 05:42 PM
  4. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  5. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM