Computer Science Canada

Graphics Movement?

Author:  [Gandalf] [ Wed Oct 05, 2005 4:29 pm ]
Post subject:  Graphics Movement?

I was trying to make an applet with graphics, and I know how to draw things, but how do I do movement? You can draw various things, but how do I make it update the screen multiple times? Here is what I was trying so far:
Java:
import java.awt.*;
import java.applet.Applet;

public class AppletGraphics6 extends Applet
{       
        int i = 1;

        public void paint(Graphics g)
        {
                g.drawLine(1, 1, i, i);
                i += 1;
                paint (g);
                delay (500);
        }              
       
        public void delay(int milliSec)
        {
        try
        {
            Thread.sleep(milliSec);
        } catch (Exception e)
        {
            System.err.println(e);
        }
    }
}


Also, if someone could tell me how to do the same thing using Swing, or point me to somewhere that could help me. Thanks.

Author:  wtd [ Wed Oct 05, 2005 5:29 pm ]
Post subject: 

Java:
import javax.swing.*;
import java.awt.*;

public class AppletGraphics6 extends JApplet
{       
   int i;

   public void init()
   {
      i = 1;
   }

   public void paint(Graphics g)
   {
      g.drawLine(1, 1, i, i);
      i += 1;
      delay(500);
      paint(g);
   }       

   void delay(int milliSec)
   {
      try
      {
         Thread.sleep(milliSec);
      }
      catch (Exception e)
      {
         System.err.println(e);
      }
   }
}


Does this work?

Author:  [Gandalf] [ Wed Oct 05, 2005 5:38 pm ]
Post subject: 

Indeed it does! Thanks wtd Smile.

Anyone know where I can find a way to do this for an application?

*edit*
I am trying to set the colour of a drawn object throuhg g.setColor(), like so:
code:
g.setColor(white);

and I get a compile time error message - "cannot find symbol variable white". This happens for any colour I use.

Author:  Hikaru79 [ Wed Oct 05, 2005 10:21 pm ]
Post subject: 

[Gandalf] wrote:
*edit*
I am trying to set the colour of a drawn object throuhg g.setColor(), like so:
code:
g.setColor(white);

and I get a compile time error message - "cannot find symbol variable white". This happens for any colour I use.

Well, what is white? It hasn't been defined, right? Smile It's actually a static field of the Color class. So, try g.setColor(Color.white); . Should work ^_^ (You may need to import java.awt.Color if you haven't already)

Author:  wtd [ Wed Oct 05, 2005 10:51 pm ]
Post subject: 

Hikaru79 wrote:
[Gandalf] wrote:
*edit*
I am trying to set the colour of a drawn object throuhg g.setColor(), like so:
code:
g.setColor(white);

and I get a compile time error message - "cannot find symbol variable white". This happens for any colour I use.

Well, what is white? It hasn't been defined, right? Smile It's actually a static field of the Color class. So, try g.setColor(Color.white); . Should work ^_^ (You may need to import java.awt.Color if you haven't already)


Could work if you use:

Java:
import static java.awt.Color.*;


Wink

Author:  [Gandalf] [ Thu Oct 06, 2005 5:10 pm ]
Post subject: 

I thought I tried that, but I guess I didn't since it worked...

I don't need to import those since I already have import java.awt.*.

Author:  rizzix [ Thu Oct 06, 2005 10:20 pm ]
Post subject: 

i advise against using static imports.. use it when absoloutly necessary... like when porting C API's to JAVA..

Author:  [Gandalf] [ Fri Oct 07, 2005 9:29 pm ]
Post subject: 

The ball will neither move nor appear in the proper spot... The start location should be what is passed to the constructor, and the ball should move according to move(). It works, but doesn't work properly Smile.
Java:
import javax.swing.*;
import java.awt.*;

class Ball
{
        private int x, y;
       
        public Ball(int xStart, int yStart)
        {
                x = xStart;
                y = yStart;
        }
       
        public void move()
        {
                x += 1;
                y += 1;
        }
       
        public int getX()
        {
                return x;
        }       
       
        public int getY()
        {
                return y;
        }
}

public class BallMovement extends JApplet
{       
        Ball ball = new Ball(300, 300);
        public static int maxx, maxy;

        public void init()
        {
                maxx = 600;
                maxy = 600;
        }

        public void paint(Graphics g)
        {
                g.setColor(Color.white);
                g.drawOval(getX(), getY(), 10, 10);
                ball.move();
                g.setColor(Color.red);
                g.drawOval(getX(), getY(), 10, 10);
                delay(5);
                paint(g);
        }       
       
        public void delay(int milliSec)
        {
                try
                {
                        Thread.sleep(milliSec);
                }
                catch (Exception e)
                {
                        System.err.println(e);
                }
        }
}

Thanks for any help.

*edit* Ok, so far I've figured out the problem is with my getX() and getY() methods, but why? how? hmm..

Author:  Aziz [ Fri Oct 07, 2005 10:04 pm ]
Post subject: 

Just one correction, ol' buddy...

Quote:
I don't need to import those since I already have import java.awt.*.


Don't assume this.

For example, at the top of my current project's file, I have these lines:

Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;


I must have these lines. Yes, I have imported java.awt.* but that does not include all packages within that package. So if you're importing java.awt.* you're not importing Color's white member. You're including Color, but not importing it, you have to reference it. I hope that makes sense

Author:  [Gandalf] [ Fri Oct 07, 2005 10:18 pm ]
Post subject: 

Alright, but that's what the Color.<colour> is for, right? Since each colour is static...

How about my current program? Smile

Author:  Aziz [ Fri Oct 07, 2005 10:36 pm ]
Post subject: 

Right. So you can use 'Color.white', but not just 'white'

Author:  wtd [ Fri Oct 07, 2005 10:37 pm ]
Post subject: 

Aziz wrote:
Right. So you can use 'Color.white', but not just 'white'


Unless you're using 1.5.x or greater and use a static import. Smile

Author:  rizzix [ Fri Oct 07, 2005 11:03 pm ]
Post subject: 

once again refrain from doing that Evil or Very Mad

Author:  wtd [ Fri Oct 07, 2005 11:05 pm ]
Post subject: 

Well, first off... it's good to understand things, even if understanding them leads you to avoid them (sometimes especially in those cases).

Second... why? What's so wrong with using static imports?

Author:  rizzix [ Sat Oct 08, 2005 12:03 am ]
Post subject: 

its counter-java way of getting things done. In java everything is part of an instance of a class or part of the class it self.. (with the only exception being primitives).. that's the philosophy!.. and refrain from code that will eventually result in YOU trying to figure out where those constants were once defined (maybe some time in the future..). this is especially troublesome when you have "more than one" of those static imports.

After all "Color.white" is a lot more meaningful than "white" alone.

Author:  wtd [ Sat Oct 08, 2005 12:22 am ]
Post subject: 

rizzix wrote:
its counter-java way of getting things done. In java everything is part of an instance of a class or part of the class it self.. (with the only exception being primitives).. that's the philosophy!


Static imports don't change that. Smile

They just provide syntactic sugar to make using static fields and methods easier.

What about methods?

For instance, importing the sort method from Arrays and Collections.

Author:  rizzix [ Sat Oct 08, 2005 7:55 am ]
Post subject: 

yea but it's the syntataic sugar that drifts you away from the java-way-of-coding.. I'm not say to _never_ use it.. i can be very useful specially when porting like C-code to java.. as in OpenGL and stuff.

Author:  Aziz [ Sat Oct 08, 2005 11:10 am ]
Post subject: 

It's like a duel... I have no more to say on this matter, but it would get confusing if you imported more than one 'white' member, wouldn't it?

Author:  wtd [ Sat Oct 08, 2005 12:13 pm ]
Post subject: 

Aziz wrote:
It's like a duel... I have no more to say on this matter, but it would get confusing if you imported more than one 'white' member, wouldn't it?


The compiler would stop you.

Author:  [Gandalf] [ Sat Oct 08, 2005 4:19 pm ]
Post subject: 

Thanks for the help Smile.


: