I am wondering how to make the red aquare jump around in the script. This is a menu. I would like the red squareto jump around every 1 or 2 seconds (doesn't have to be exact). Right now I have it when someone moves the mouse but I woule like to change that.

The code so far:

// The "Timer" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Menu extends Applet

    implements MouseListener, MouseMotionListener
{
    int mx, my, lastmousex, lastmousey;
    Font title = new Font ("SHOWCARD GOTHIC", 10, 50);
    Font buttonfont = new Font ("Ariel", 10, 18);
    Image img, button1, button2, button3, button4;
    double squarex, squarey;
    int squarex2, squarey2;
    boolean pressbutton = false;

    // Place instance variables here

    public void init ()
    {
        img = getImage (getDocumentBase (), "Background.gif");
        button1 = getImage (getDocumentBase (), "button.gif");
        button2 = getImage (getDocumentBase (), "button.gif");
        button3 = getImage (getDocumentBase (), "button.gif");
        button4 = getImage (getDocumentBase (), "button.gif");


        // Place the body of the initialization method here
    } // init method


    public void mouseEntered (MouseEvent e)
    {
    }


    public void mouseExited (MouseEvent e)
    {
    }


    public void mouseMoved (MouseEvent e)
    {
        mx = e.getX ();
        my = e.getY ();

        squarex = Math.random () * 100;
        squarey = Math.random () * 100;
        squarex2 = (int) Math.round (squarex);
        squarey2 = (int) Math.round (squarey);
        lastmousex = mx;
        lastmousey = my;
        repaint ();
        e.consume ();
    }


    public void mouseDragged (MouseEvent e)
    {
    }


    public void mousePressed (MouseEvent e)
    {
        pressbutton = true;
        repaint ();
        e.consume ();
    }


    public void mouseReleased (MouseEvent e)
    {
        pressbutton = false;
        e.consume ();

    }


    public void mouseClicked (MouseEvent e)
    {
        repaint ();
        e.consume ();
    }


    public void paint (Graphics g)
    {



        g.drawImage (img, 0, 0, 1280, 750, this);
        g.drawImage (button1, 400, 600, 100, 50, this);
        g.drawImage (button2, 550, 600, 100, 50, this);
        g.drawImage (button3, 700, 600, 100, 50, this);
        g.drawImage (button4, 850, 600, 100, 50, this);

        g.setColor (Color.red);
        g.setFont (title);
        g.drawString ("Running Square!!!", 425, 50);
        g.setColor (Color.black);
        g.setFont (buttonfont);
        g.drawString ("Start", 415, 625);
        g.drawString ("Level", 565, 620);
        g.drawString ("Select", 565, 640);
        g.drawString ("Credits", 865, 625);
        g.drawString ("Settings", 715, 625);
        if (lastmousex != mx || lastmousey != my){
            g.setColor (Color.red);
            g.fillRect (squarex2, squarey2, 30, 30);
          }


        // Place the body of the drawing method here
    } // paint method
} // Timer class

sorry about no comments

You can create a Timer object (from the swing package, not the util package). You give it a delay (in milliseconds) and an ActionListener, and then you start the timer. After that, every time the timer goes off, the actionPerformed method in the ActionListener is called.

So to make your red square jump around, for example, move or copy the code from your mouseMoved method into actionPerformed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.