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

Username:   Password: 
 RegisterRegister   
 me again, with another program i cant get to work >.<!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
Bo0sT




PostPosted: Fri Mar 07, 2008 3:53 pm   Post subject: me again, with another program i cant get to work >.<!

this program is supose to draw a blue ball on the screen and then make it move towards the mouse every time you click, it does this, sorta. It moves the ball towards the mouse but by 1 increment or decrement at a time, rather than all the way to the mouse, here have a look and plz respond if u can help me.

code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GoToMouse extends JFrame implements MouseListener
{
    private static final String formTitle = "Go To The Mouse";
    private static final int sizeX = 500;
    private static final int sizeY = 500;
    private static int ballX = 200;
    private static int ballY = 200;
    public static void main (String[] args)
    {
        new GoToMouse ();
    }


    public GoToMouse ()
    {
        setTitle (formTitle);
        setSize (sizeX, sizeY);
        setVisible (true);
        setDefaultCloseOperation (EXIT_ON_CLOSE);
        addMouseListener (this);
    }


    public void paint (Graphics g)
    {
        clear (g);
        g.setColor (Color.blue);
        g.fillOval (ballX, ballY, 50, 50);
    }


    public void clear (Graphics g)
    {
        g.setColor (Color.white);
        g.fillRect (0, 0, sizeX, sizeY);
        g.setColor (Color.black);
    }


    public void delay (int timer)
    {
        try
        {
            Thread.sleep (timer);
        }
        catch (Exception e)
        {
        }
    }


    public void mouseEntered (MouseEvent e)
    {

    }


    public void mouseExited (MouseEvent e)
    {

    }


    public void mousePressed (MouseEvent e)
    {

    }


    public void mouseReleased (MouseEvent e)
    {

    }


    public void mouseClicked (MouseEvent e)
    {
        if (e.getX () > ballX)
        {
            do
            {
                ballX++;
                repaint ();
            }
            while (ballX > e.getX ());
        }
        if (e.getX () < ballX)
        {
            do
            {
                ballX--;
                repaint ();
            }
            while (ballX < e.getX ());
        }
        if (e.getY () > ballY)
        {
            do
            {
                ballY++;
                repaint ();
            }
            while (ballY > e.getY ());
        }
        if (e.getY () < ballY)
        {
            do
            {
                ballY--;
                repaint ();
            }
            while (ballY < e.getY ());
        }
    }
}

 
Sponsor
Sponsor
Sponsor
sponsor
Bo0sT




PostPosted: Fri Mar 07, 2008 3:55 pm   Post subject: Re: me again, with another program i cant get to work >.<!

ok i think i got something, but this time it is moveing to fast and if i add a delay, it only freezes it

code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GoToMouse extends JFrame implements MouseListener
{
    private static final String formTitle = "Go To The Mouse";
    private static final int sizeX = 500;
    private static final int sizeY = 500;
    private static int ballX = 200;
    private static int ballY = 200;
    public static void main (String[] args)
    {
        new GoToMouse ();
    }


    public GoToMouse ()
    {
        setTitle (formTitle);
        setSize (sizeX, sizeY);
        setVisible (true);
        setDefaultCloseOperation (EXIT_ON_CLOSE);
        addMouseListener (this);
    }


    public void paint (Graphics g)
    {
        clear (g);
        g.setColor (Color.blue);
        g.fillOval (ballX, ballY, 50, 50);
    }


    public void clear (Graphics g)
    {
        g.setColor (Color.white);
        g.fillRect (0, 0, sizeX, sizeY);
        g.setColor (Color.black);
    }


    public void delay (int timer)
    {
        try
        {
            Thread.sleep (timer);
        }
        catch (Exception e)
        {
        }
    }


    public void mouseEntered (MouseEvent e)
    {

    }


    public void mouseExited (MouseEvent e)
    {

    }


    public void mousePressed (MouseEvent e)
    {

    }


    public void mouseReleased (MouseEvent e)
    {

    }


    public void mouseClicked (MouseEvent e)
    {
        if (e.getX () > ballX)
        {
            do
            {
                ballX++;
                repaint ();
            }
            while (ballX < e.getX ());
        }
        if (e.getX () < ballX)
        {
            do
            {
                ballX--;
                repaint ();
            }
            while (ballX > e.getX ());
        }
        if (e.getY () > ballY)
        {
            do
            {
                ballY++;
                repaint ();
            }
            while (ballY < e.getY ());
        }
        if (e.getY () < ballY)
        {
            do
            {
                ballY--;
                repaint ();
            }
            while (ballY > e.getY ());
        }
    }
}
 
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: