Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Java counter

  Asked By: Robert    Date: Jun 22    Category: Java    Views: 1244
  

I really need urgent help ,

I found the following counter on this website
www.kt.rim.or.jp/~faichan/javacount/indexE.shtml

and I need to know how to change the scrolling numbers inorder to
put my own numbers (that are read from a database but the
connectivity is not the issue)

help is mostly appreciated.


package rand;

// "Counter1.java" Java Counter1 Ver1.10 1999,Feb,06
// copyright: Hiroyuki Murata <faichan@...>
import java.applet.Applet;
import java.awt.*;
import java.net.URL;

public class Counter1 extends Applet implements Runnable {

int digit; // Counter Digit
Thread thread;
String strDigits="9012345678901";
int Width,WidDig,Height,Hpitch,H10,Hall;
int offsetY;
Image iroll,ioff;
Graphics groll,goff;

Graphics[] gsub;
char[] charCount;
Motionc1[] mv;

public void init() {

String strCount_ = getParameter("count");
if (strCount_ == null)
strCount_ = getParamStr("name", "0");

digit = getParamInt("digit",1);

int digMarX = getParamInt("digmarx", 9); // X Digit Margin
int digMarY = getParamInt("digmary", -5); // Y Digit Margin
int appMarX = 4; // X Applet Margin
int appMarY = 4; // Y Applet Margin

Color bgColor = getParamCol("bgcolor", 0);
Color fgColor = getParamCol("fgcolor", 0xff4040);
Color f0Color = getParamCol("f0color", 0x808080);
Color f1Color = getParamCol("f1color", 0xe0e0e0);

String fontname = getParamStr("fontname", "TimesRoman");
int fontstyle = getParamInt("fontstyle", 1);
int fontsize = getParamInt("fontsize", 30);

gsub = new Graphics[digit];
charCount = new char[digit];
mv = new Motionc1[digit];

for (int i=0; i<digit; i++) {
int j = i - (digit - strCount_.length());
if (j<0) charCount[i] = '0';
else charCount[i] = strCount_.charAt(j);
mv[i] = new Motionc1(Character.digit(charCount[i],10));
}

Font font = new Font(fontname, fontstyle, fontsize);
FontMetrics metrics = getFontMetrics(font);

Width = size().width - appMarX * 2;
WidDig = Width / digit;
Height = size().height - appMarY * 2;
int Hsize = metrics.getAscent() + metrics.getDescent();
Hpitch = Hsize + digMarY * 2;
H10 = Hpitch * 10;
Hall = Hpitch * strDigits.length();
offsetY = (Height - Hsize) / 2 - Hpitch;

iroll = createImage(WidDig,Hall);
groll = iroll.getGraphics();
ioff = createImage(size().width, size().height);
goff = ioff.getGraphics();
for (int i=0; i<digit; i++) {
gsub[i] = goff.create(appMarX+WidDig*i, appMarY, WidDig,
Height);
}

groll.setColor(bgColor);
groll.fillRect(0,0,WidDig,Hall);
groll.setFont(font);
groll.setColor(fgColor);
for (int i=0; i<strDigits.length(); i++) {
groll.drawString(
strDigits.substring(i,i+1),
digMarX, Hpitch*i+metrics.getAscent()+digMarY
);
}

goff.setColor(f0Color);
goff.fillRect(0,0, size().width, size().height);
getToolkit().sync();
goff.setColor(bgColor);
goff.fillRect(
appMarX, appMarY,
size().width-appMarX*2, size().height-appMarY*2
);
getToolkit().sync();
goff.setColor(f1Color);
goff.drawLine(0, 0, size().width-1, 0);
goff.drawLine(0, 1, size().width-2, 1);
goff.drawLine(4, size().height-4, size().width-4, size
().height-4);
goff.drawLine(3, size().height-3, size().width-3, size
().height-3);
goff.drawLine(0, 0, 0, size().height-1);
goff.drawLine(1, 0, 1, size().height-2);
goff.drawLine(size().width-4, 4, size().width-4, size
().height-4);
goff.drawLine(size().width-3, 3, size().width-3, size
().height-3);
getToolkit().sync();
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
String getParamStr(String name, String defaultString) {
String ret = getParameter(name);
if (ret == null) {
ret = defaultString;
}
return ret;
}

int getParamInt(String name, int defaultValue) {
try {
return Integer.parseInt(getParameter(name));
} catch (RuntimeException e) {
return defaultValue;
}
}

Color getParamCol(String name, int defaultCol) {
try {
return new Color(Integer.parseInt(getParameter(name),
16));
} catch (RuntimeException e) {
return new Color(defaultCol);
}
}

//-----------------------------------------------------------
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}

public void stop() {
if (thread != null) {
thread.stop();
thread = null;
}
}

public void run() {

for (int i=0; i<digit; i++) {
mv[i].start();
mv[i].brake();
}

while (thread.isAlive()) {
repaint();
try {
thread.sleep(50);
} catch (InterruptedException e) {
break;
}
}
}

//-----------------------------------------------------------
public void update(Graphics g) {
for (int i=0; i<digit; i++) {
gsub[i].drawImage(
iroll, 0,
(int)(-mv[i].go()*Hpitch) + offsetY,
this
);
}
paint(g);
}

public void paint(Graphics g) {
g.drawImage(ioff,0,0,this);
}

//-----------------------------------------------------------
public boolean mouseDown(Event e, int x, int y) {
if (Motionc1.isStillAll(mv)) {
for (int i=0; i<digit; i++) {
mv[i].start();
}
} else if (Motionc1.isFullAll(mv)) {
for (int i=0; i<digit; i++) {
mv[i].brake();
}
}
return true;
}

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int irand(int n) {
return (int)(Math.random()*n);
}
}
/********************************************************************
*****/
class Motionc1
{
static final int MinRot = 1; // minimum rotation for braking
static final double
fMinDist = (double)MinRot * 10.0, // minimum distance for
braking
fInitSpeed = 0.3
;
public boolean still,brake;
double fN,fSpeed,subSpeed,fLastN;
// fN: current position
// fSpeed: current velocity
// subSpeed: braking (negative) acceleration
// fLastN: target position

//-----------------------------------------------------------
static boolean isFullAll(Motionc1[] mv) {
boolean ret = true;
for (int i=0; i<mv.length; i++) {
ret &= !mv[i].still && !mv[i].brake;
}
return ret;
}
static boolean isStillAll(Motionc1[] mv) {
boolean ret = true;
for (int i=0; i<mv.length; i++) {
ret &= mv[i].still;
}
return ret;
}

//-----------------------------------------------------------
Motionc1(int n) {
fLastN = (double)n;
fN = Math.random() * 10.0;
brake = false;
still = true;
}

public void start() {
fSpeed = fInitSpeed + Math.random() * (fInitSpeed * 0.8);
still = brake = false;
}

public void brake() {
brake = true;
double Dist = fLastN - fN;
Dist += (Dist>=0) ? fMinDist : fMinDist*2.0;
double BrkTime = Dist * 2.0 / fSpeed; // braking time
subSpeed = fSpeed / BrkTime;
}

public double go() {
if (brake) {
fSpeed -= subSpeed;
if (fSpeed<=0.0) {
fSpeed = 0.0;
brake = false;
still = true;
}
}
if (!still) {
fN += fSpeed;
if (fN >= 10.0) {
fN -= 10.0;
}
}
return fN;
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Java counter Or get search suggestion and latest updates.




Tagged: