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

Thread: program will not end

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default program will not end

    I'm new to java and have NO idea why this won't work :S. I think its the Boolean but I'm not sure why.

    public abstract class Core {

    public int[] player = null;{
    player = new int[5];
    player[0] = 1000;
    player[1] = 100;
    player[2] = 25;
    player[3] = 10;
    }

    public int[] norris = null;{
    norris = new int [5];
    norris[0] = 1000;
    norris[1] = 100;
    norris[2] = 25;
    norris[3] = 10;
    }

    public boolean trackanimation = false;


    // FIREBALL
    public void fballdmg(){
    norris[0] = norris[0] - (player[2]*2); //50
    }

    public void fireball(){
    faniloop();
    if (trackanimation = true){
    fballdmg();
    refresh();
    s.update();
    trackanimation = false;
    }
    }

    private static DisplayMode modes[] = {
    new DisplayMode(1440,900,32,0),
    new DisplayMode(1440,900,24,0),
    new DisplayMode(1440,900,16,0),
    };
    public void loadimg(){
    // PICTURES
    playerF = new ImageIcon("E:\\ProjectSTUFF\\PlayerINFOFRAME.jpg") .getImage();
    playerN = new ImageIcon("E:\\ProjectSTUFF\\NorrisINFOFRAME.jpg") .getImage();
    // BACKGROUND
    bg = new ImageIcon ("E:\\ProjectSTUFF\\BG.jpg").getImage();
    Image fire1 = new ImageIcon("E:\\ProjectSTUFF\\FIREBALL1.jpg").getIm age();
    Image fire2 = new ImageIcon("E:\\ProjectSTUFF\\FIREBALL2.jpg").getIm age();
    Image fire3 = new ImageIcon("E:\\ProjectSTUFF\\FIREBALL3.jpg").getIm age();
    Image fire4 = new ImageIcon("E:\\ProjectSTUFF\\FIREBALL4.jpg").getIm age();
    // ANIMATION
    f = new Animation();
    f.addScene(fire1, 500);
    f.addScene(fire2, 200);
    f.addScene(fire1, 200);
    f.addScene(fire2, 250);
    f.addScene(fire1, 250);
    f.addScene(fire2, 250);
    f.addScene(fire1, 250);
    f.addScene(fire2, 250);
    f.addScene(fire3, 500);
    f.addScene(fire4, 250);
    loaded = true;
    }

    // ANIMATIONS
    private Animation f;

    // PICTURES
    private Image bg;
    private Image playerF;
    private Image playerN;

    // BOOLEAN
    private boolean loaded;
    public boolean running;

    // SCREEN
    protected Screen s;

    // stop
    public void stop(){
    running = false;
    }

    // call
    public void run(){
    try{
    init();
    loadimg();
    gameloop();
    }finally{
    s.restoreScreen();
    }
    }

    // set to full screen
    public void init(){
    s = new Screen();
    DisplayMode dm = s.findFirstCompatibleMode(modes);
    s.setFullScreen(dm);
    Window w = s.getFullScreenWindow();
    w.setFont(new Font("Arial", Font.BOLD,50));
    w.setBackground(Color.BLACK);
    w.setForeground(Color.BLACK);
    running = true;
    }

    // game loop
    public void gameloop(){
    long startTime = System.currentTimeMillis();
    long runTime = startTime;
    refresh();
    s.update();
    while(running){
    }
    }

    // animation loop
    public void faniloop(){
    long startingTime = System.currentTimeMillis();
    long runTime = startingTime;
    while (runTime - startingTime <3150){
    long timePassed = System.currentTimeMillis() - runTime;
    runTime += timePassed;
    Graphics2D g = s.getGraphics();
    f.update(timePassed);
    refresh();
    drawf(g);
    s.update();
    g.dispose();
    trackanimation = true;
    try{
    Thread.sleep(5);
    }catch(Exception ex){}
    }
    }

    // update screen
    public void update(long timePassed){}

    // paint
    public synchronized void paintUI(Graphics g){
    if(loaded){
    g.drawImage(bg, 0,0,null);
    g.drawImage(playerN, 1030,0,null);
    g.drawImage(playerF, 0,700,null);
    }
    }

    //refresh
    public void refresh(){
    Graphics2D g = s.getGraphics();
    paintUI(g);
    String norrisHP = new Integer(norris[0]).toString();
    String playerHP = new Integer(player[0]).toString();
    g.drawString(norrisHP, 1097, 116 );
    g.drawString(playerHP, 70, 818);
    g.dispose();
    }


    // draw f
    public synchronized void drawf(Graphics2D g){
    g.drawImage(f.getImage(), 50,280,null);
    }
    }



    THIS IS TH EKEYPRESS i have the whole keypress working just not the ESCAPE

    // Key pressed
    public void keyPressed(KeyEvent e){
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_ESCAPE){
    System.out.println("KEYPREZ ESC");
    stop();
    }
    // FIREBALL
    if(keyCode == KeyEvent.VK_1){
    fireball();
    e.consume();
    }
    else{
    e.consume();
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: program will not end

    When posting code, please use code tags, and preferably boil your problem down to an SSCCE that we can copy and paste to see the issue.

    Why are you doing your painting like that? Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    What loop does this get stuck in?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program will not end

    It never gets past the gameloop

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: program will not end

    Quote Originally Posted by Zilender View Post
    It never gets past the gameloop
    Okay, so when do you expect it to exit that? What happens instead?

    Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program will not end

    When i put a system.out.pr in they key press ESCAPE it prints it, but it doesn't execute the stop(): properly. It should change the Boolean running to false and end the gameloop, then going to finally and restoring the screen. It just does nothing.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: program will not end

    So what happens when you put a println in the stop() method? Does it properly set the boolean to false?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program will not end

    public void stop(){
    running = false;
    System.out.println(running);
    }


    it will print false

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: program will not end

    Is that the same variable your loop is checking against? Are you sure?

    Without an SSCCE I'm only guessing, but you're either using two different variables with the same name, or you're dealing with multiple threads that are caching the variable value. Try declaring the variable volatile.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program will not end

    Odd i was following up with the while loop.
    public void gameloop(){
    long startTime = System.currentTimeMillis();
    long runTime = startTime;
    refresh();
    s.update();
    while(running){
    System.out.println("stuck");
    }
    }

    This made it go threw, just needed a command to continue.

Similar Threads

  1. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 9th, 2012, 12:47 AM
  2. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM