Computer Hope

Software => Computer programming => Topic started by: Patplays852 on May 16, 2009, 10:21:39 AM

Title: [JAVA] Problems running application
Post by: Patplays852 on May 16, 2009, 10:21:39 AM
Hello, Im trying to make a VERY simple application that will draw a castle on the screen (still adding more stuff to it) for a school assignment, but when im working on it in my IDE (netbeans) or any other IDE for that matter, it seems to only run when IT wants to, for example, i can click the run and get this: (good)

(http://img51.imageshack.us/img51/5480/snag0038.jpg)

or this: (bad)
(http://img51.imageshack.us/img51/9628/snag0040.jpg)

Note that i can make NO changes to the file, run it again, and get a different result from the previous run, is there any way to fix this? or what is the cause of it.

I have the latest JAVA JDK installed on my computer btw.
Title: Re: [JAVA] Problems running application
Post by: mickster9 on May 17, 2009, 02:56:02 PM
I've never heard of anything like this happening..
I can only suggest basic troubleshooting, trying to work out what is different when it works or doesn't.
You could also try reinstalling everything and seeing if that helps.
Title: Re: [JAVA] Problems running application
Post by: Patplays852 on May 17, 2009, 02:58:21 PM
Im thinking its cuz im using the Turtle help file that my teacher gave us, because it does the same thing on school computers (using TextPad), and it happens when I use JBuilder, and even Netbeans on my computer at home, one sec and i can give the code for the turtle file, but it is quite large. (for a post) lol

Note: It has to be something wrong in the turtle file, because it happens no matter what kind of application i try to write with it, even when i start from scratch it does it.

Turtle file:
Code: [Select]
//
// Copy this file in its entirety to a file named Turtle.java
// Compile it before trying to compile any program that uses Turtles


import java.awt.*;
import javax.swing.JFrame;
import java.awt.geom.*;
import java.awt.event.*;

public class Turtle extends Object
{ public static final double DEGREE = Math.PI / 180;
public static final int WIDTH = 760, HEIGHT = 600;
public static final Color RED = Color.red, BLUE = Color.blue,
               BLACK = Color.black,      GRAY = Color.gray,
               YELLOW = Color.yellow,    PINK = Color.pink,
               ORANGE = Color.orange,    GREEN = Color.green,
               MAGENTA = Color.magenta,  WHITE = Color.white;
private static Graphics2D page = null;     // for drawing
//////////////////////////////////
private double heading = 0;         // heading initially east
private double xcor = WIDTH / 2;    // centered horizontally
private double ycor = HEIGHT / 2;   // centered vertically too

/** Set the drawing Color to the given value.  */

public void switchTo (Color given)
{ page.setColor (given);
} //======================

/** Write words without changing the Turtle's state.  */

public void say (String message)
{ page.drawString (message, (int) xcor, (int) ycor);
} //======================

/** Pause the animation for wait milliseconds.  */

public void sleep (int wait)
{ long timeToQuit = System.currentTimeMillis() + wait;
while (System.currentTimeMillis() < timeToQuit)
;   // take no action
} //======================

/** Make a circle of the given radius, Turtle at center. */

public void swingAround (double radius)
{ if (radius > 0.0)
page.draw (new Ellipse2D.Double (xcor - radius,
            ycor - radius, 2 * radius, 2 * radius));
} //======================


// the Turtle class, completed

/** Rotate left by left degrees; MOVE for forward steps.*/

public Turtle move (double left, double forward)
{ heading += left * DEGREE;
xcor += forward * Math.cos (heading);
ycor -= forward * Math.sin (heading);
return this;
} //======================

/** Rotate left by left degrees; PAINT for forward steps. */

public Turtle paint (double left, double forward)
{ heading += left * DEGREE;
double x = xcor + forward * Math.cos (heading);
double y = ycor - forward * Math.sin (heading);
page.draw (new Line2D.Double (xcor, ycor, x, y));
xcor = x;
ycor = y;
return this;
} //======================

/** Fill a circle of the given radius, Turtle at center. */

public void fillCircle (double radius)
{ page.fill (new Ellipse2D.Double (xcor - radius,
            ycor - radius, 2 * radius, 2 * radius));
} //======================

/** Fill a rectangle of the given width and height, Turtle at center. */

public void fillBox (double width, double height)
{ page.fill (new Rectangle2D.Double (xcor - width / 2,
           ycor - height / 2, width, height));
} //======================

public Turtle (Graphics g)
{ page = (Graphics2D) g;
} //======================

public Turtle()
{ if (page == null)
{ JFrame area = new JFrame ("Turtle drawings");
area.addWindowListener (new Closer());  // in Chapter 10
area.setSize (WIDTH, HEIGHT);
area.setVisible (true);
page = (Graphics2D) area.getGraphics();
area.paint (page);
page.setColor (WHITE);
page.fillRect (0, 0, WIDTH, HEIGHT);
page.setColor (BLACK);
}
} //======================


private static class Closer implements WindowListener
{
   /** Enable the closer icon to close the window. */

public void windowClosing (WindowEvent e)
{ System.exit (0);
} //======================

public void windowActivated (WindowEvent e)     { }
public void windowDeactivated (WindowEvent e)   { }
public void windowIconified (WindowEvent e)     { }
public void windowDeiconified (WindowEvent e)   { }
public void windowOpened (WindowEvent e)        { }
public void windowClosed (WindowEvent e)        { }
}
}

//


Title: Re: [JAVA] Problems running application
Post by: Patplays852 on May 18, 2009, 12:47:00 PM
Just a note, I am using the most current version of Java. Since the Turtle file is old, I'm thinking that perhaps that is causing some issues?
Title: Re: [JAVA] Problems running application
Post by: Patplays852 on May 19, 2009, 12:46:50 PM
This thread can be closed, I fixed problem by adding a sleep command to the start of my code, for some reason that makes it work 100% every time lol.
Title: Re: [JAVA] Problems running application
Post by: mickster9 on May 21, 2009, 03:50:15 PM
Excellent one of the many mysteries of the universe solved. lol