Results 1 to 8 of 8

Thread: Programming in Java

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    chris_uk_83's Avatar Physicist
    Join Date
    Feb 2007
    Location
    Lancaster, England
    Posts
    818

    Default Programming in Java

    I'm sure there must be at least one person on this forum who can do some programming .

    I'm trying to teach myself Java because I'm bored at work, and I occasionally run into some walls that I can't get through. Would anybody be willing to help me out with my learning?

    At the moment I'm attempting to add a delay to a series of things that happen in an applet, but all that happens is the delay goes on for the total time and the applet jumps straight to the final state, skipping all the intermediate ones.

    Help anyone?

  2. #2

    Default Re: Programming in Java

    Post your code (using the code tags in the forums) please.

  3. #3
    Erik's Avatar Dux Limitis
    Join Date
    Aug 2004
    Location
    Amsterdam
    Posts
    15,653

    Default Re: Programming in Java

    Yes, you need to post your code before we can help you.



  4. #4
    chris_uk_83's Avatar Physicist
    Join Date
    Feb 2007
    Location
    Lancaster, England
    Posts
    818

    Default Re: Programming in Java

    Fair enough, it's a bit convoluted but here I go. This is just an example of something I'd like to do to test my delay code. It's supposed to draw a line, wait 2 seconds and then draw another line below it. What actually happens is that the window opens, waits 2 seconds then sets the background and draws all the lines simultaneously.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import util.*;
    
    class Lines extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawLine(0, 50, 50, 50);
            Time.delay(2000);
            g.drawLine(0, 100, 100, 100);
        }
    }
    
    public class JustALine extends JApplet {
        Lines line = new Lines();
        public void init() {
            Container cp = getContentPane();
            line.setBackground(Color.BLUE);
            cp.add(line);
        }
     }
    now my util package contains the delay() method and looks like this:

    Code:
    package util;
    
    
    
    public class Time {
        /**
     *Makes your program wait the specified number of milliseconds before continuing
     *@param delay The number of milliseconds you want to wait for
     **/
        public static void delay(int delay){
            boolean cont = false;
            long startTime = System.currentTimeMillis() + delay;
            while(cont == false){
                if(startTime <= System.currentTimeMillis())
                    cont = true;
            }        
        }        
    }
    If I use System.out.println() to print a series of messages to the console with a delay in between them, my code works perfectly but it refuses to cause a sequence of delayed events to happen in an applet.

    I'm really just a beginner to programming and I've been teaching myself so I might need telling what's wrong in baby terms. I do know how to use the jdk documentation to look up methods and things though.

    Cheers guys

  5. #5
    chris_uk_83's Avatar Physicist
    Join Date
    Feb 2007
    Location
    Lancaster, England
    Posts
    818

    Default Re: Programming in Java

    Hmm, turns out any loop must complete before anything is updated in my applets. Just attempted the standard bouncing ball applet using Swing and it won't draw at all! It's stuck in its infinite while loop (which is intentional) and never repaint()s.

  6. #6
    chris_uk_83's Avatar Physicist
    Join Date
    Feb 2007
    Location
    Lancaster, England
    Posts
    818

    Default Re: Programming in Java

    I think I solved my own problem. I was trying to make animations happen in the init() method, when they should really have been happening in run(). :doh

  7. #7
    Erik's Avatar Dux Limitis
    Join Date
    Aug 2004
    Location
    Amsterdam
    Posts
    15,653

    Default Re: Programming in Java

    Like you probably discovered you were drawing the two lines, with the delay, to a panel that wasn't visible yet.
    The panel only became visible after it was complete.

    Usually this is desirable behavior because in most cases when you are making a panel that takes some time to draw you don't want to show the drawing procedure to the user.

    If you want to make an animation, do it something like this:
    -Store currentTimeMills() in a variable, let's call it drawingStartTime.
    -Draw frame 1 to a buffer.
    -Copy the buffer to the screen.
    -Wait until currentTimeMills() >= drawingStartTime + (1000 / maximumFrameRate)
    -Update drawingStartTime with currentTimeMills().
    -Draw frame 2 to a buffer.
    -Copy the buffer to the screen.
    etc. etc.
    Notice that this way you'll get constant frame rates even when your frames take longer to draw (just as long as the drawing time doesn't exceed the frame rate of course).
    And the user will never see incomplete frames, because frames only become visible after they are drawn.

    Also:
    instead of
    while(cont == false){
    if(startTime <= System.currentTimeMillis())
    cont = true;
    }
    Why don't you just do:
    while(startTime <= System.currentTimeMillis());
    Last edited by Erik; March 27, 2007 at 12:58 PM.



  8. #8
    chris_uk_83's Avatar Physicist
    Join Date
    Feb 2007
    Location
    Lancaster, England
    Posts
    818

    Default Re: Programming in Java

    Ah, I see what you mean (I think). This has something to do with drawing a frame off the screen that you can't see doesn't it? I still have some way to go before I'll know how to do that I'm afraid, but at least now I know what direction to start looking in! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •