aspose file tools*
The moose likes Applets and the fly likes petrol station Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "petrol station" Watch "petrol station" New topic
Author

petrol station

Anish Patel
Greenhorn

Joined: May 25, 2001
Posts: 7
I am currently doing a project which involves constructing a petrol station
However I think it is my thread that is not working. The situation works likes this, the
User clicks one of the checkboxes which represents a type of fuel then that should start the indicator
Flashing to tell the controller someone is requesting fuel (however not working also) from there the
Controller presses the enable button which turns the indicator light of f(switches to black) and then clears at the same time the controllers textbox, the price textbox and the litres text box (that is working). However if you look in the item state changed part in the pump class you can see I have the price set as the variable and the formula to work it out at the bottom. But when I press the pump button the metre does not go round like at petrol station, nothing happens, and when the pump button is pressed again the meter should stop
Displaying the cost of the fuel and amount of litres used and the price should also be displayed on the controllers textbox as well. If you could help I would be very greatfull thankyou. (code shown below)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class fuel extends Applet
{//1
private Controller controlObj;//decleration part
private Pump pumpObj;
private Panel petrolPan, conPan, pumpPan;
public fuel()
{//fuel

}//fuel
public void update(Graphics g) //avoids flicker
{
paint(g);
}

public void paint(Graphics g)
{//here the paint( ) method is reading variable LEDlight that is changed by our thread
//run toggles LEDlight every 1/2 sec; note that run explicitly calls repaint()
//which calls update()
//if (LEDlight == true) //LEDon?
g.setColor(Color.red);
//else//if not black
g.setColor(Color.black);
g.fillOval(100,100,50,50);
}//end paint

public void init()//set up the fuel system
{//init
petrolPan = new Panel();
petrolPan.setLayout(new GridLayout(2,0));//two rows,
add(petrolPan);//attach to applet surface

conPan = new Panel();//panel to display controller
conPan.setBackground(new Color(67,25,175));
petrolPan.add(conPan);//dispaly on market panel

pumpPan = new Panel();//panel for a till
pumpPan.setBackground(new Color(110,103,43));
petrolPan.add(pumpPan);//dispaly on market panel

controlObj = new Controller();//instantiation part
pumpObj = new Pump();// new Pump object
}//init

class Controller implements ActionListener, Runnable
{
private TextField totalTF;
private Button conBut;
Button startBut,stopBut;
Thread LEDThread; //step 3
boolean LEDon; //Changed - can now be set to false by Stop Button
boolean LEDlight=false;


public Controller()
{
totalTF = new TextField(12);
totalTF.setText("");
conPan.add(totalTF);

conBut = new Button("Enable");//new customer
conBut.addActionListener(this);
conPan.add(conBut);
}
public void actionPerformed(ActionEvent ae)
{//ap
if (ae.getSource() == conBut)
showStatus ("jeff");
controlObj.resetPump();
}//ap

public void resetPump()
{//16
totalTF.setText("88888888");
pumpObj.priceTF.setText("88888888");
pumpObj.litresTF.setText("88888888");
waitaMo( );
totalTF.setText("");
pumpObj.litresTF.setText("");
pumpObj.priceTF.setText("�00.00");

}//16
public void waitaMo( )
{//17
long nowTime = System.currentTimeMillis( );
long t = 0;
do
{//18
t = t + 1; //or, do nothing at all and save a bit of typing
}while(System.currentTimeMillis()<(nowTime+1000));//18
}//end waitamo


public void run() //step 6
{
//for(; //loop forever REMOVED NOW
while (LEDon == true)
{
LEDlight = !LEDlight; //toggle the LED status
repaint();//call paint so it draws the LED in its new state
try{//we'll talk about try..catch next week
LEDThread.sleep(500); //wait for 500 milliseconds (1/2 second)
}catch(InterruptedException e)
{
showStatus("Thread timed out");
}
showStatus("LED = "+LEDlight);
}//end while
//}//endfor REMOVED
}//endrun


}
class Pumpimplements ActionListener, ItemListener, Runnable
{//classpump
private Label petrolLab, litresLab, priceLab;
private TextField petrolTF, litresTF, priceTF;
private Button pumpBut;
private boolean tillStatus = false;
private CheckboxGroup bGrp;
private Checkbox lrp, ul, dsl;
Thread counterThread;
boolean on=false;
private double value=0;
private double litre =1;
boolean pumping = true;
double pumpPrice, total;
public Pump()
{//pubpump
petrolLab = new Label ("TYPE");
pumpPan.add(petrolLab);

petrolTF = new TextField(12);
petrolTF.setText("");
pumpPan.add(petrolTF);

litresLab = new Label("LITRES");
pumpPan.add(litresLab);

litresTF = new TextField(12);
litresTF.setText("");
pumpPan.add(litresTF);

priceLab = new Label("�");
pumpPan.add(priceLab);

priceTF = new TextField(12);
priceTF.setText("");
pumpPan.add(priceTF);

pumpBut = new Button("PUMP");//new customer
pumpBut.addActionListener(this);
pumpPan.add(pumpBut);

bGrp = new CheckboxGroup();
lrp = new Checkbox("LRP", false, bGrp);
ul = new Checkbox("UL", false, bGrp);
dsl = new Checkbox("DSL", false, bGrp);
lrp.addItemListener(this);
ul.addItemListener(this);
dsl.addItemListener(this);
pumpPan.add(lrp);
pumpPan.add(ul);
pumpPan.add(dsl);

}//pubpump


public void actionPerformed(ActionEvent ae)
{//ap
if(ae.getSource()==pumpBut)
{
pumpOn();
}
if(ae.getSource()==pumpBut)
{
pumpOff();
}
}//ap

public void itemStateChanged(ItemEvent ie)
{//ic
if (ie.getSource() == lrp)
petrolTF.setText("lrp");
pumpPrice = 00.79;

if (ie.getSource() == ul)
petrolTF.setText("ul");
pumpPrice = 00.83;

if (ie.getSource() == dsl)
petrolTF.setText("dsl");
pumpPrice = 00.67;

}//ic

public void pumpOn()
{
showStatus("pump me");
on = true;
counterThread = new Thread (this, "");
counterThread.start();
}//end pumpon

public void pumpOff()
{
on = false;
}

public void run()
{
while (on==true)
{
value = value+litre;
litresTF.setText("" + value);
total = value * pumpPrice;
priceTF.setText("" + total); // output needs formatting properly
repaint();
try
{
counterThread.sleep(100);
}
catch (InterruptedException ie)
{
}
}// end while
}//end run

}//classpump
}//1
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Anish,
I have looked at your code, cleaned up a little and fixed it so that it performs the correct functions. Some notes:
1. Try and limit the number of class variables. Most of yours were not required to be class variables. I made most of them local variables.
2. Problem: litre and value class variables were not being reset when the enable button was hit more than once. Fix: made litre and value local variables inside run method.
3. sleep method is static method. Therefore we don't require a thread reference to call it.
4. NOTE: IMO you might want to setEnable( false ) on the toggle buttons (petrol type) after the Enable button has been hit. This could potentially cause you much trouble.
5. I hate typing, you should get used to using all available parameters that can be sent to constructors (i.e., TextField has constructor to set initial value).
6. DecimalFormat class is handy for formatting decimal output.

Enjoy,
Manfred.
Anish Patel
Greenhorn

Joined: May 25, 2001
Posts: 7
Thanks so munch manfred that means alot to me what you have done.
one last small thing once the user presses the pump button the second time i want the cost to be displayed on the controllers text box so how would i do that!!!
P.S THANX AGAIN
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Anish,
Sorry it took me so long to reply.
Since your layout is already somewhat limiting, the easiest approach would be to move the 2 declarations:
TextField priceTF;
TextField totalTF;
and place them into the fuel class as variables. That way all inner classes can access them. That will allow you to remove the pumpObj references inside the resetPump method. Then you can add the following line to the actionPerformed method of the Pump class:
totalTF.setText( priceTF.getText() );
Enjoy,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: petrol station
 
Similar Threads
Database ...need to implement
JDBC applets
linux file access
Help please.
setting contents visible/invisible