The Artima Developer Community
Sponsored Link

Java Answers Forum
Need urgent help in java!

4 replies on 1 page. Most recent reply: Nov 23, 2003 4:06 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Blake

Posts: 3
Nickname: blake
Registered: Nov, 2003

Need urgent help in java! Posted: Nov 22, 2003 11:29 AM
Reply to this message Reply
Advertisement
I really need some help with this, we have to create an animation in java for a class but I'm only in the first year and don't even understand it all properly yet, yet they refuse to help us with it because it is an assignment. I have the drawing part down but I added a wait1second method and cannot get the thing to compile and don't know why, nor do I know how to make things loop so it will be a constant animation :(

Please help.

import element.*;
import java.awt.Color;
 
public class DrawingAssignment2
{
    public static void main(String args[])
    {
    }
    public static void wait1second()
    // post: pause for one second
    {
        long now = System.currentTimeMillis();
        long then = now + 1000; // one second of milliseconds
        while (System.currentTimeMillis() < then)
        {
            // Do Nothing
        }
        
          
        DrawingWindow d = new DrawingWindow(300,600);
        
       //Borders 1 
       
        d.setForeground(Color.red);                    //sets drawing colour to red.
        Rect Border1 = new Rect (5,10,10,580);         //sets coordinates for rectangle   
        d.draw(Border1);                               //draws rectangle
        d.fill(Border1);                               //fills rectangle with current colour
        
        //Border 2
        Rect Border2 = new Rect (5,580,290,10);
        d.draw(Border2);
        d.fill(Border2);
        
        //Border 3
        Rect Border3 = new Rect (5,10,290,10);
        d.draw(Border3);
        d.fill(Border3);
        
        //Border 4
        Rect Border4 = new Rect (285,10,10,580);
        d.draw(Border4);
        d.fill(Border4);
        
        
         wait1Second();
        
       
        //------------------------------------------------------------
        d.clear(Border1);
        d.clear(Border2);
        d.clear(Border3);
        d.clear(Border4);
        //------------------------------------------------------------
        //Borders 5 
        d.setForeground(Color.blue);                    //sets drawing colour to red.
        Rect Border5 = new Rect (5,10,10,580);         //sets coordinates for rectangle   
        d.draw(Border5);                               //draws rectangle
        d.fill(Border5);                               //fills rectangle with current colour
        
        //Border 6
        Rect Border6 = new Rect (5,580,290,10);
        d.draw(Border6);
        d.fill(Border6);
        
        //Border 7
        Rect Border7 = new Rect (5,10,290,10);
        d.draw(Border7);
        d.fill(Border7);
        
        //Border 8
        Rect Border8 = new Rect (285,10,10,580);
        d.draw(Border8);
        d.fill(Border8);
        
        
            wait1Second();
        
        //------------------------------------------------------------
        d.clear(Border5);
        d.clear(Border6);
        d.clear(Border7);
        d.clear(Border8);
        //------------------------------------------------------------
        //Borders 9 
        d.setForeground(Color.yellow);                    //sets drawing colour to red.
        Rect Border9 = new Rect (5,10,10,580);         //sets coordinates for rectangle   
        d.draw(Border9);                               //draws rectangle
        d.fill(Border9);                               //fills rectangle with current colour
        
        //Border 10
        Rect Border10 = new Rect (5,580,290,10);
        d.draw(Border10);
        d.fill(Border10);
        
        //Border 11
        Rect Border11 = new Rect (5,10,290,10);
        d.draw(Border11);
        d.fill(Border11);
        
        //Border 12
        Rect Border12 = new Rect (285,10,10,580);
        d.draw(Border12);
        d.fill(Border12);
         //------------------------------------------------------------
        d.clear(Border9);
        d.clear(Border10);
        d.clear(Border11);
        d.clear(Border12);
        //------------------------------------------------------------
            
        //Car Body
        d.setForeground(Color.red);
        d.moveTo(30,130);
        d.lineTo(30,100);
        d.lineTo(50,100);
        d.lineTo(100,70);
        d.lineTo(160,70);
        d.lineTo(210,100);
        d.lineTo(250,110);
        d.lineTo(280,130);
        d.lineTo(30,130);
        
        //Windows
        d.setForeground(Color.blue);
        d.moveTo(170,77);
        d.lineTo(170,100);
        d.lineTo(210,100);
        
        //Wheels
        d.setForeground(Color.black);
        Circle wheel1 = new Circle (60,130,20);
        d.fill(wheel1);
        Circle wheel2 = new Circle (220,130,20);
        d.fill(wheel2);
        
         //Lights
         d.setForeground(Color.yellow);
         Rect rearl = new Rect(10,110,35,35); // lights
    
        Text t = new Text ("A new sports car!!",90,540);
        Text t2 = new Text ("Driving over all Terrain!",90,560);
        d.draw(t);
        d.draw(t2);
        d.draw(new Rect(t.left(),t.top(),t.width(),t.height()));
             }
}


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Need urgent help in java! Posted: Nov 22, 2003 4:06 PM
Reply to this message Reply
Couple of things

1. Where are you getting the elemnts Package from?

2. you have defind your method as wait1second() with a simple "s" and when invokeing it calling with a capital "S" like wait1Second();

3. You seems to be calling the wait1second() inside that method itself. Its looks to me as a recursive call and I dont see a terminating condition and you might get a stack overflow error.

4. Where do you have these Circle and Rect classes? Are they in the element package?

Blake

Posts: 3
Nickname: blake
Registered: Nov, 2003

Re: Need urgent help in java! Posted: Nov 22, 2003 7:19 PM
Reply to this message Reply
The elements package is in the netbeans folder and this handles all drawing so everything involving shapes and lines for drawing comes from here.

Thanks for your help, hopefully this will work.

BTW, sorry to ask again but would you know how to make that loop?

Blake

Posts: 3
Nickname: blake
Registered: Nov, 2003

Re: Need urgent help in java! Posted: Nov 22, 2003 7:22 PM
Reply to this message Reply
SOrry again but what would be a terminating condition? I added additional brackets but the drawing classes stopped working instead as if i had shut off the elements package.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Need urgent help in java! Posted: Nov 23, 2003 4:06 PM
Reply to this message Reply
Waiting one second by hogging the CPU in a do-nothing while loop is a very bad thing to do.

Have a look at the java.util.Timer class; it is pretty easy to use and using it instead of this wait1second() stuff will give you a much better design. What you do is tell the timer how often to run a task (like drawing one frame of an animation), then it will call that task at each interval.

Essentially, your class will have a run() method. Each time it is called, it draws the next step in the animation.

Flat View: This topic has 4 replies on 1 page
Topic: arrrrgghhh Previous Topic   Next Topic Topic: Lazy Events

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use