?
Solved

Java Applet - display a flashing dot moving around page

Posted on 2004-09-11
10
Medium Priority
?
588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 172 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 164 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 164 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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