Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Graphics Movement?
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
[Gandalf]




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Oct 05, 2005 5:29 pm   Post subject: (No 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?
[Gandalf]




PostPosted: Wed Oct 05, 2005 5:38 pm   Post subject: (No 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.
Hikaru79




PostPosted: Wed Oct 05, 2005 10:21 pm   Post subject: (No 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)
wtd




PostPosted: Wed Oct 05, 2005 10:51 pm   Post subject: (No 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
[Gandalf]




PostPosted: Thu Oct 06, 2005 5:10 pm   Post subject: (No 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.*.
rizzix




PostPosted: Thu Oct 06, 2005 10:20 pm   Post subject: (No subject)

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




PostPosted: Fri Oct 07, 2005 9:29 pm   Post subject: (No 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..
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Fri Oct 07, 2005 10:04 pm   Post subject: (No 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
[Gandalf]




PostPosted: Fri Oct 07, 2005 10:18 pm   Post subject: (No subject)

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

How about my current program? Smile
Aziz




PostPosted: Fri Oct 07, 2005 10:36 pm   Post subject: (No subject)

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




PostPosted: Fri Oct 07, 2005 10:37 pm   Post subject: (No 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
rizzix




PostPosted: Fri Oct 07, 2005 11:03 pm   Post subject: (No subject)

once again refrain from doing that Evil or Very Mad
wtd




PostPosted: Fri Oct 07, 2005 11:05 pm   Post subject: (No 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?
rizzix




PostPosted: Sat Oct 08, 2005 12:03 am   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: