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

Thread: Rectangles

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rectangles

    I'm not sure why this program doesn't make a moving rectangle:

    public static void main(String[] args)
    {
     
        import java.awt.*;
        import javax.swing.*;
     
        public class Advertisement extends JApplet implements Runnable
        {
            Thread animator;
            int yPosition;
     
            public void init()
            {
                JRootPane rootPane = this.getRootPane();
                rootPane.putClientProperty("defeatSystemEventQueueCheck",
                    Boolean.TRUE);
            }
     
            public void start()
            {
                xPosition = 0;
                yPosition = 0;
                animator = new Thread(this);
                animator.start();
            }
            public void paint(Graphics g)
            {
                g.setColor(Color.cyan);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(Color.red);
                g.fillRect(xPosition, yPosition, 100, 100);
     
            }
     
            public void run()
            {
                while (Thread.currentThread() == animator)
                {
                    xPosition = xPosition +1;                
                    yPosition = yPosition +1;
                    repaint();
     
                    if (yPosition >350)
                    {
                        yPosition = 50;
                    }
                    if (xPosition >350)
                    {
                        xPosition = 50;
                    }
                   try
                   {
                       Thread.sleep(10);
                    }
                    catch (InterruptedException e)
                    {
                        break;
                    }
                }
            }
     
        public void stop()
        {
            animator = null;
        }
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Rectangles

    You update some position variables, but you never repaint so those new position variables are never used.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rectangles

    That worked. Thanks.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Rectangles

    You're welcome.

Similar Threads

  1. Help With Moving A 2D Array Of Rectangles
    By billg118 in forum Collections and Generics
    Replies: 1
    Last Post: April 22nd, 2012, 03:23 PM
  2. Question on Rectangles
    By parkBENch in forum Java Theory & Questions
    Replies: 1
    Last Post: March 22nd, 2012, 12:13 AM
  3. adding rectangles in images
    By mserrao in forum AWT / Java Swing
    Replies: 4
    Last Post: September 1st, 2011, 07:27 AM
  4. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 01:59 PM
  5. How to form a rectangle from the union of two Rectangles.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 3rd, 2010, 08:22 AM