http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[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?
> 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?  




Now, this is the code I want to convert:
> 
> import java.awt.Graphics;
> import java.awt.Color;
> public class lampapp extends java.applet.Applet implements Runnable {
>     //run1 is the running thread. x is the true/false value determining
>     //wether the lamp is on or off. lampplace is where the first ring
>     //of the lamp string is. lampstate is a value == 0 or 1 which
>     //controls wether the lamp is on or off.
>     Thread run1;
>     boolean x;
>     int lampplace;
>     int lampstate;
> public void init() {
>     setBackground(Color.black);
>     lampstate = 0;
>     }
> public void start () {
>     if (run1 == null) {
>     run1 = new Thread(this);
>     run1.start ();
> }
> }
> public void stop () {
>     if (run1 != null) {
>         run1.stop();
>         run1 = null;
>     }
> }
> public void pause(int time) {
>     try{Thread.sleep(time);}
>     catch (InterruptedException e){ }
> }
> public boolean lampon () {
>     //when lampon is called, it returns true, stating that the lamp is on
>    boolean a = true;
>    return a;
> }
> public boolean lampoff () {
> //when lampoff is called, it returs false,stating that the lamp is off
>     boolean b = false;
>     return b;
> }
> public void run () {
>     while (true) {
>         //continue to cut the light on and off as long as true;which
>         //is forever
>         int i;         //the counter variable for the for loop
>         for (i=70;i <=73; i +=3) {    //this for loop increases
>         //the value of i.i determines where the pull-
>         //chain will be.
>         lampplace = i;  //where the start of the top of the lamp string
>         //will be.I must also repaint it saying that the lamp string
>         //has moved. this is for when it is going down
>         repaint();  //this repaint method is for the down chain
>         pause(400); //stop for 500 milliseconds. pause is my method
>         }
>         int w;
>         for (w=73;w >=70; w -=3) {  //this for loop does the same
>         //thing as the other one. only it is for going up
>         lampplace = w;
>         repaint(); //this repaint method is for the chain going down
>         pause(200);
>         }
>         /*All the stuff above is just for the chain going up then
>         down. Now I will have a variable-int- that will determine
>         wether to call lampon or lampoff. If the variable == 0 then
>         call lampoff and if 1 call lampon or vice versa.*/
>         if (lampstate == 0) {
>             x = lampon();
>             /*I don't make lampstate = 1 here because if I did the
>             statement below this would would just make the lamp dark
>             everytime before it even got painted light. I declare it
>             in the paint method.*/
>         }
>         if (lampstate == 1) {
>             x = lampoff();
>             /*neither do I make lampstate = 0 here because mentally
>             it is just easier for me to recognize what is going on.
>             It, too, is declared in paint*/
>         }
>         repaint();  //I repaint the screen and pause for 2 seconds
>         pause(100);
>     }
> }
> 
> /*Note. I wanted to override the update method, but it may take too
> long. I may add it in later. Right now I want to test everything and
> see if it works.*/
> 
> public void paint(Graphics g) {
>     //draw the lamp
>     //the lamp shade
>     //shade.top(joke)
>     g.setColor(Color.green);
>     g.drawLine(45,40,95,40);
>     //shade.leftside
>     g.drawLine(45,40,35,85);
>     //shade.leftbottom
>     g.drawLine(35,85,53,85);
>     //shade.rightside
>     g.drawLine(95,40,105,85);
>     //shade.rightbottom
>     g.drawLine(105,85,87,85);
>     //lamp.leftside
>     g.setColor(Color.red);
>     g.drawArc(47,80,29,63,90,135);
>     //lamp.rightside
>     g.drawArc(64,80,29,63,90,-135);
>     //lamp.bottomcircle
>     g.drawArc(35,123,64,30,123,278);
>     //bulb.left
>     g.drawLine(61,71,61,81);
>     //bulb.right
>     g.drawLine(79,81,79,71);
>     //bulb.top
>     g.drawArc(58,44,25,30,228,-270);
> 
>     //draw the chain
>     g.setColor(Color.blue);
>     g.drawLine(100,lampplace,100,lampplace + 3);
>     g.drawLine(100,lampplace + 7,100,lampplace + 10);
>     g.drawLine(100,lampplace + 14,100,lampplace + 17);
>     g.drawLine(100,lampplace + 21,100,lampplace + 24);
>     g.drawLine(100,lampplace + 28,100,lampplace + 31);
>     g.drawLine(100,lampplace + 35,100,lampplace + 38);
>     g.drawLine(100,lampplace + 42,100,lampplace + 45);
>     g.fillRect(98,lampplace + 47,4,4);
> 
>     //if lampon is called then x = true and that means the lamp
>     //is on
>     if (x == true) {
>         g.setColor(Color.yellow);
>         g.fillOval(63,53,15,15);
>         lampstate = 1;
>         g.drawString("Lamplight Productions", 30, 8);
>     }
>     //if lampoff is called the x = false and that means the lamp is
>     //off
>     if (x == false) {
>         g.setColor(Color.black);
>         g.fillOval(63,53,15,15);
>         lampstate = 0;
>         g.drawString("                     ", 30, 8);
>     }
> 
> }
> }
> 
> Thanks so much
> 
>                                       Peace and God bless
>                                          Jonathan Smith
> 
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com
> 
> =================================================================
> 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
=================================================================
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