Solved

Java Applet - display a flashing dot moving around page

Posted on 2004-09-11
10
551 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now