Results 1 to 9 of 9
  1. #1
    Nimblyjacks is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default Can't get graphics to move opposite direction

    So lets start with the code:

    PHP Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    public class Ball extends JPanel implements Runnable
    {
        private boolean keepGoing;
        private Graphics g;
        private int x = 100, xChange = 1, xStop;
        private int y = 100, yChange = 1, yStop;
        private int diameter = 10;
        private int rectLeftX = 0, rectRightX = 100;
        private int rectTopY = 0, rectBottomY = 100;
    
        public Ball(Graphics graphics, int mouseX, int mouseY)
        {
            xStop = mouseX;
            yStop = mouseY;
            g = graphics;
            keepGoing = true;
        }
    
        public void pleaseStop()
        {
            keepGoing = false;
            System.out.println("False means STOP, WTF?!?!?!");
        }
    
        public void run()
        {
            while(keepGoing)
            {
                System.out.println("Dot is at " + x + ", " + y);
    
                g.setColor(Color.white);
                g.fillOval(x, y, diameter, diameter);
    
                if (x < xStop)
                {
                    xChange = xChange;
                    System.out.println("x < xStop... Dot should be moving right");
                }
                if (x > xStop)
                {
                    xChange = -xChange;
                    System.out.println("x > xStop... Dot should be moving left");
                }
                if (x == xStop)
                {
                    xChange = 0;
                    System.out.println("x = xStop... Dot should be moving up/down only");
                }
                if (y < yStop)
                {
                    yChange = yChange;
                    System.out.println("y < yStop... Dot should be moving down");
                }
                if (y > yStop)
                {
                    yChange = -yChange;
                    System.out.println("y > yStop... Dot should be moving up");
                }
                if (y == yStop)
                {
                    yChange = 0;
                    System.out.println("y = yStop... Dot should only be moving left/right");
                }
                
                
    
                x = x + xChange;
                y = y + yChange;
                System.out.println("Dot is at " + x + ", " + y);
    
                g.setColor(Color.red);
                g.fillOval(x, y, diameter, diameter);
    
                try
                {
                    Thread.sleep(500);
                }
    
                catch(InterruptedException e)
                {
                    System.err.println("Sleep exception");
                    return;
                }
            }
        }
    
        public void setParameters(Graphics graphics, int mouseX, int mouseY)
        {
            xStop = mouseX;
            yStop = mouseY;
            g = graphics;
            keepGoing = true;
        }
    }
    I've been trying to make a Ball class which goes towards a point that has been clicked inside the applet. The thing is, I can't get the object to move left or up! I've been trying many different ways, and none seem to be working. Any ideas?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm sorry to say, but there are many things wrong with that code, and this suggests that you would benefit greatly by reading the Sun tutorials on how to paint in Swing.

    A quick bit of suggestions:
    1) override JPanels paintComponent and do your painting there.
    2) Lord knows why your JPanel class requires a Graphics object parameter since those objects are fleeting, and so will be your graphics if you use this.
    3) use a Swing Timer as your game timer, not threading with Thread.sleep(...).
    etc...

  3. #3
    Nimblyjacks is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    This class used to be for AWT and not Swing, and I've been learning how to change towards swing lately. If you can tell me what else is wrong with this, it would help so much.

    The Graphics parameter allowed me to use getGraphics, part of awt. How would I be doing this with Swing?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Nimblyjacks is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Alright, so I've added some stuff that should make it a swing class. Tell me if I need to change anything else, or if maybe the timer is done wrong... I don't quite understand how it works.

    PHP Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.Timer;
    
    
    public class Ball extends JPanel implements Runnable
    {
        private boolean keepGoing;
        private Graphics g;
        private int x = 100, xChange = 1, xStop;
        private int y = 100, yChange = 1, yStop;
        private int diameter = 10;
        private int rectLeftX = 0, rectRightX = 100;
        private int rectTopY = 0, rectBottomY = 100;
        private Timer t;
    
        public Ball(int mouseX, int mouseY)
        {
            xStop = mouseX;
            yStop = mouseY;
            keepGoing = true;
        }
    
        public void pleaseStop()
        {
            keepGoing = false;
            System.out.println("Stopped");
        }
    
        public void run()
        {
            xChange = 1;
            yChange = 1;
            t = new Timer(1000, null);
            while(keepGoing)
            {           
                if (x < xStop)
                {
                    xChange = xChange;
                    System.out.println("x < xStop... Dot should be moving right");
                }
                if (x > xStop)
                {
                    xChange = -xChange;
                    System.out.println("x > xStop... Dot should be moving left");
                }
                if (x == xStop)
                {
                    xChange = 0;
                    System.out.println("x = xStop... Dot should be moving up/down only");
                }
                if (y < yStop)
                {
                    yChange = yChange;
                    System.out.println("y < yStop... Dot should be moving down");
                }
                if (y > yStop)
                {
                    yChange = -yChange;
                    System.out.println("y > yStop... Dot should be moving up");
                }
                if (y == yStop)
                {
                    yChange = 0;
                    System.out.println("y = yStop... Dot should only be moving left/right");
                }
                if (x == xStop && y == yStop)
                {
                    keepGoing = false;
                    System.out.println("Dot has reached its destination");
                }
    
    
                x = x + xChange;
                y = y + yChange;
                System.out.println("Dot is at " + x + ", " + y);            
    
                try
                {
                    t.start();
                }
                catch (Exception e)
                {
                    t.stop();
                }
            }
        }
    
        public void setParameters(int mouseX, int mouseY)
        {
            xStop = mouseX;
            yStop = mouseY;
            keepGoing = true;
        }
    
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillOval(x, y, diameter, diameter);
            repaint();
        }
    }
    Still can't get it to move left or up...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What is this supposed to do?
    Java Code:
    t = new Timer(1000, null);
    Have you read the tutorial on using Swing timers?

    Also, you might want to

    1) Search this and other Java forums for examples of Swing animation, and

    2) create and post an SSCCE so we can see your problem to.
    Last edited by Fubarable; 12-22-2009 at 01:06 AM.

  7. #7
    Nimblyjacks is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    t = new Timer(1000, null);
    That is suppose to create a new timer which will delay the movement of the ball 1 second(which it isn't), and since I'm not using any ActionListener events, I set it to null. I have read the swing timer tutorial, and I don't know how else to use it in this code.

    Here's a more simpler driver class that runs Ball:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class MovingBall extends JFrame
    {
        private int mouseX, mouseY;
        private JLabel mouseLabel;
        Ball ball = new Ball(mouseX, mouseY);
    
        public MovingBall()
        {
            super("A Moving Ball");
    
            Container c = getContentPane();
            
            mouseLabel = new JLabel();
            c.add(mouseLabel, BorderLayout.NORTH);
            c.add(ball, BorderLayout.CENTER);
            setSize(1024, 768);
            show();
    
            addMouseListener(new MouseTest()
            {
                public void mouseClicked(MouseEvent e)
                {
                    mouseX = e.getX();
                    mouseY = e.getY();
                    mouseLabel.setText("Clicked at " + mouseX + ", " + mouseY);
                    ball.setParameters(mouseX, mouseY);
                    new Thread(ball).start();
                }
            });
        }
    
        public static void main(String[] args) 
        {
            MovingBall app1 = new MovingBall();
            app1.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });
        }
    
    }
    Oh, and the ball doesn't exactly move to where the mouse clicked. It might be because I centered it, but even if I remove it from being centered, it still doesn't go towards the point clicked.

    By the way, thanks for all the help and suggestions. It's been helping me learn swing a lot faster.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Nimblyjacks View Post
    Java Code:
    t = new Timer(1000, null);
    That is suppose to create a new timer which will delay the movement of the ball 1 second(which it isn't), and since I'm not using any ActionListener events, I set it to null. I have read the swing timer tutorial, and I don't know how else to use it in this code.
    since you are not adding an action listener into that constructor, that timer will do zip, nada, nothing. I recommend that you re-read the Timer tutorial. It requires an ActionListener object since it is that listener's actionPerformed method that gets called every xxx milliseconds. In your code above, null or nothing will be called.

    Here's a more simpler driver class that runs Ball:
    It doesn't compile for me since your Ball class doesn't have an int, int constructor, and since I don't have your MouseTest class.
    Last edited by Fubarable; 12-22-2009 at 02:14 AM.

  9. #9
    Nimblyjacks is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Oh, sorry. Change the code:
    Java Code:
    addMouseListener(new MouseTest()
            {
                public void mouseClicked(MouseEvent e)
                {
                    mouseX = e.getX();
                    mouseY = e.getY();
                    mouseLabel.setText("Clicked at " + mouseX + ", " + mouseY);
                    ball.setParameters(mouseX, mouseY);
                    new Thread(ball).start();
                }
            });
    to

    Java Code:
    addMouseListener(new MouseListener()
            {
                public void mouseClicked(MouseEvent e)
                {
                    mouseX = e.getX();
                    mouseY = e.getY();
                    mouseLabel.setText("Clicked at " + mouseX + ", " + mouseY);
                    ball.setParameters(mouseX, mouseY);
                    new Thread(ball).start();
                }
                public void mousePressed(MouseEvent e){}
                public void mouseReleased(MouseEvent e){}
                public void mouseEntered(MouseEvent e){}
                public void mouseExited (MouseEvent e){}
            });
    and, are you using the original Ball class or the newer one I posted today? The newer one has Ball with an int, int constructor instead of Graphics, int, int.

    I'll read up on the timer tutorial again, but from what you're saying, I don't really see how my ball class can have an actionPerformed if all it needs are values of where I clicked in the driver class.

Similar Threads

  1. Looking for direction...
    By ewomack in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 12:00 AM
  2. Replies: 3
    Last Post: 12-31-2008, 06:57 AM
  3. [SOLVED] Need direction...
    By hotice1027 in forum New To Java
    Replies: 5
    Last Post: 11-28-2008, 10:03 AM
  4. I need direction too. output control
    By bornwithnoname in forum New To Java
    Replies: 0
    Last Post: 11-23-2008, 04:57 AM
  5. Replies: 14
    Last Post: 09-18-2008, 05:41 AM

Posting Permissions

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