|
||
|
GP Mailing List
ATXGPSIG List
|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: java applets and applications:difference
Jonathan Smith wrote:
>
> I wrote an applet that has a lamp cutting on and off while the lamp is
> blinking. I was wondering(I do a lot of that), how could I turn this applet
> into an application. I like to create applications more than applets, but
> the book I have is more applet centered. My hope is that someone could teach
> me how to convert applets to applications; in it's basic form such as the
> start method, init method and so on. Also, how do I use the Graphics and
> color classes in my applications?
I've attached a program that is both an applet and an application. An
application is a class that implements main() an applet is a class that
extends applet and implements start(), init(), and so on. A class that
extends applet and implements main() is both. The trick is to use main
to establish a context for the rest of the applet oriented code. This
isn't quite as complete as it sounds. The code doesn't implement any of
the browser interface classes. But, it works for a lot of code.
> One last question: Is it really easier to create applets than applications?
> It just seems that way to me because the majority of java programs I see are
> applets?
No harder to do one than the other. I happen to think it is harder to do
applets!
The following code implements a simple animator class and uses it to
animate a bouncing ball. Just something I consed together to test an
idea I had the other night.
Bob P.
/*
* bounce.java
*
* @author Bob Pendleton
* @version 0.0.0
*/
import java.lang.*;
import java.math.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
interface Animatee
{
public void paint(Graphics g);
public void setAnimator(Animator a);
}
class bouncer implements Animatee
{
Animator a = null;
Random rand = new Random();
int width = 20;
int height = 20;
int x = -1;
int y = -1;
int dx = -1;
int dy = -1;
bouncer()
{
}
public void setAnimator(Animator a)
{
this.a = a;
x = rand.nextInt(a.getWidth());
dx = rand.nextInt(1) > 0 ? 1 : -1;
y = rand.nextInt(a.getHeight());
dy = rand.nextInt(1) > 0 ? 1 : -1;
}
int fiddle(int i)
{
if (i > 0)
{
i = 1 + rand.nextInt(10);
}
else
{
i = -1 - rand.nextInt(10);
}
return i;
}
public void paint(Graphics g)
{
if (a == null)
{
return;
}
int w = a.getWidth();
int h = a.getHeight();
g.setColor(Color.white);
g.fillRect(0, 0, w, h);
x += dx;
y += dy;
if (x < 0 || x > (w - width))
{
x = Math.max(x, 0);
x = Math.min(x, w - width);
dx = -dx;
dx = fiddle(dx);
}
if (y < 0 || y > (h - height))
{
y = Math.max(y, 0);
y = Math.min(y, h - height);
dy = -dy;
dy = fiddle(dy);
}
//System.out.println("x=" + x +" dx=" + dx + " y=" + y + " dy="
+ dy);
g.setColor(Color.red);
g.fillArc(x, y, width, height, 0, 360);
g.setColor(Color.black);
g.drawString("Frame " + a.getFrameCount(), 0, 30);
g.drawString("Time " + a.getCurrentTime(), 0, 50);
g.drawString("FPS " + a.getFPS(), 0, 70);
}
}
class anim implements Animatee
{
Animator a = null;
anim()
{
}
public void setAnimator(Animator a)
{
this.a = a;
}
public void paint(Graphics g)
{
if (a == null)
{
return;
}
g.setColor(Color.red);
g.fillRect(0, 0, a.getWidth(), a.getHeight());
g.setColor(Color.black);
g.drawString("Frame " + a.getFrameCount(), 0, 30);
g.drawString("Time " + a.getCurrentTime(), 0, 50);
g.drawString("FPS " + a.getFPS(), 0, 70);
}
}
class Animator extends Canvas implements Runnable
{
private double startTime = -1;
private double frameTime = -1;
private long frameCount = -1;
private double fps = -1;
private double delay = -1; // milliseconds per frame
private Thread animatorThread = null;
private int width = 0;
private int height = 0;
private Image backBuf = null;
private Graphics backG = null;
private Animatee anim = null;
Animator()
{
this(-1, null);
}
Animator(int fps)
{
this(fps, null);
}
Animator(int fps, Animatee anim)
{
this.anim = anim;
if (fps > 0)
{
delay = (1000.0 / fps);
}
else
{
delay = 100.0;
}
}
public void setFPS(int fps)
{
if (fps > 0)
{
delay = (1000.0 / fps);
}
}
public double getFPS()
{
return fps;
}
public long getFrameCount()
{
return frameCount;
}
public double getCurrentTime()
{
return frameTime;
}
public void setAnimatee(Animatee anim)
{
this.anim = anim;
this.anim.setAnimator(this);
}
public void start()
{
//Start animating!
if (animatorThread == null)
{
animatorThread = new Thread(this);
}
animatorThread.start();
}
public void stop()
{
//Stop the animating thread.
animatorThread = null;
}
public void run()
{
Thread currentThread = Thread.currentThread();
long diff = -1;
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
startTime = frameTime = ((double)System.currentTimeMillis());
//This is the animation loop.
while (currentThread == animatorThread)
{
frameCount++;
if (frameTime > startTime)
{
fps = (frameCount * 1000) / (frameTime - startTime);
}
repaint();
try
{
frameTime += delay;
diff = (long)(frameTime -
((double)System.currentTimeMillis()));
Thread.sleep(Math.max(0, diff));
}
catch (InterruptedException e)
{
break;
}
}
}
public void setSize(int w, int h)
{
super.setSize(w, h);
if (backG != null)
{
backG.dispose();
backG = null;
}
if (backBuf != null)
{
backBuf.flush();
backBuf = null;
}
}
public void update(Graphics g)
{
paint(g);
}
//Draw the current frame of animation.
public void paint(Graphics g)
{
if (null == backBuf)
{
width = getWidth();
height = getHeight();
backBuf = createImage(width, height);
backG = backBuf.getGraphics();
}
if (anim != null)
{
anim.paint(backG);
}
g.drawImage(backBuf, 0, 0, this);
}
}
public class bounce extends Applet
{
static Applet applet = null;
Animator c = null;
public bounce()
{
// Empty
}
public void init()
{
c = new Animator(60);
c.setSize(300, 300);
c.setAnimatee(new bouncer());
add(c);
}
public void start()
{
c.start();
}
public void stop()
{
c.stop();
}
public void destroy()
{
// Empty
}
public static void main(String[] args)
{
applet = new bounce();
Frame f = new Frame("bounce");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
applet.stop();
applet.destroy();
System.exit(0);
}
});
f.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
f.pack();
f.setVisible(true);
}
} // bounce
=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once
To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html
|
|