Solved

Java Applet - display a flashing dot moving around page

Posted on 2004-09-11
10
552 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 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

23 Experts available now in Live!

Get 1:1 Help Now