Solved

Java Applet - display a flashing dot moving around page

Posted on 2004-09-11
10
559 Views
Last Modified: 2013-11-23
I'm very much a novice of java, but part of may app, I need a java applet that is a blinking/flashing dot(smal circle) that moves around the page.  The position of the dot/circle will be determined by (x,y) coordinates passed to it, probabaly from a database(Access).

I realize this is a lot to ask, but just wondering how hard this would be to build, and if anyone has the code for this already.

Thanks
0
Comment
Question by:Malek103197
  • 2
  • 2
  • 2
  • +1
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 43 total points
ID: 12037582
Yes, that's possible and straightforward. Your best bet is to adapt a 'bouncing ball' applet:

http://javaboutique.internet.com/tutorials/Java_by_Example/section6_9.html
0
 
LVL 3

Assisted Solution

by:mte01
mte01 earned 41 total points
ID: 12037646
For a bit simpler program, you can use this:

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

public class FMApplet extends JApplet
{


  public FMApplet()
  {
  }

  public void init()
  {
    try
    {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    Mover m = new Mover(this,200);
    this.setSize(new Dimension(400,300));
  }

}




import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;


public class Mover
{
    Timer timer;
    JApplet myapp;

    public Mover(JApplet a,int milliseconds)
    {
      myapp = a;
        timer = new Timer();
        timer.schedule(new MoverTask(myapp), milliseconds);
    }

}




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

class MoverTask extends TimerTask
{
    Thread runner;
    JApplet myapp;
    final int h;
    final int w;
    double inc1;
    double inc2;
    double x;
    double y;

    public MoverTask(JApplet a)
    {
      myapp = a;
      h = 30;
      w = 30;
      inc1 = 1.5;
      inc2 = 2;
      x = 150;
      y = 150;
    }

    public void start()
    {
        if (runner == null)
        {
                runner = new Thread (this);
                runner.start();
        }
    }


    public void run()
    {
      while(true)
      {
      try
      {
        runner.sleep(15);
      }
        catch (Exception e) { }

      myapp.getGraphics().drawOval((int)x,(int)y,w,h);
      x += inc1;
        y += inc2;

        if(x<0 || x>myapp.getWidth()-w)
        {
            inc1=-inc1;
            x+=inc1;
        }

       if(y<0 || y>myapp.getHeight()-h)
        {
           inc2=-inc2;
           y+=inc2;
        }
        myapp.repaint();
      }
    }
}

0
 
LVL 92

Expert Comment

by:objects
ID: 12040247
you're probably going to need to sign your applet, what exactly is the purpose of the applet?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Malek103197
ID: 12049818
Thanks,

I wish to have a dot moving around over top of an image. The image is a map, and the dot represents coordiante swhere a vehilce has driven. I've seen someone else do this solution and it looks pretty good. the dot blinks/flashes on/of and moves around the page, where the position of the dot (x,y) are determined by passing it (x,y).
0
 
LVL 92

Assisted Solution

by:objects
objects earned 41 total points
ID: 12049888
Use a couple of JLabel's, one for the map image, and the other for the dot.

JLabel map = new JLabel(new ImageIcon(mapimage));
JLabel dot = new JLabel(new ImageIcon(dotimage));
map.setLayout(null);
map.add(dot);

then use setLocation() method of JLabel to move the dot as required.

0
 

Author Comment

by:Malek103197
ID: 12062151
Thanks for your suggestions. I will be a while before I try this out since I need help with setting up my java etc...but I will get back.
0
 
LVL 3

Expert Comment

by:mte01
ID: 12278434
I feel that I deserve a bit of the credit (few points not much :))
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question