Witam wszystkich
Chcialbym dodac pare rzeczy do swojego programu, ale nie do konca wiem jak.
Np po wybraniu danej opcji chcialbym aby tekst wyswietlal sie w innym kolorze.
Moze sami macie jakies pomysly co moglbym dodac do tej aplikacji.
Dzieki z góry pozdrawiam.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Assignment2 extends Applet implements ActionListener
{
//declare label and textfield
Label choiceprompt1, welcomscr;
TextField choiceinput1;
//declare variables
int coffee, tea, soup, anwser;
int coffeect, teact, soupct, waterct;
double water;
public void init()
{
//create label,setting color of background and text
welcomscr = new Label("Welcome to my vending machine please choose from fallowing options ");
add(welcomscr);
//welcomscr.setForeground(Color.blue);
//welcomscr.setBackground(Color.orange);
choiceprompt1 = new Label("Please enter number 1 to 5:");
add(choiceprompt1);
//create textfield
choiceinput1 = new TextField(10);
choiceinput1.addActionListener(this);
add(choiceinput1);
//choiceprompt1.setForeground(Color.blue);
//choiceprompt1.setBackground(Color.orange);
}
//Drawing menu of vending machine
public void paint(Graphics g)
{
//setBackground(Color.orange);
g.drawLine(20,130,550,130);
g.drawLine(20,310,550,310);
g.drawString("Menu",50, 150);
g.drawString("No: 1 Coffee 2 \u20ac Total bought " +coffeect, 50,180);
g.drawString("No: 2 Tea 2 \u20ac Total bought " +teact, 50,210);
g.drawString("No: 3. Soup 2 \u20ac Total bought " +soupct, 50,240);
g.drawString("No: 4. Water 1.50 \u20ac Total bought " +waterct, 50,270);
g.drawString("No: 5. Finish and pay ", 50,300);
if (anwser ==5 && total()==0) //If user ended progrma and didn't buy anything
g.drawString("You didn't buy anything ", 220,110);
switch(anwser) //After an option is selected this will be prompt to screen
{
case 0:
g.drawString("No option selected yet" , 210,90);
break;
case 1:
g.drawString("You have chosen Coffee ", 210,90);
break;
case 2:
g.drawString("You have chosen Tea", 210,90);
break;
case 3:
g.drawString("You have chosen Soup ", 210,90);
break;
case 4:
g.drawString("You have chosen Water ", 210,90);
break;
case 5:
g.drawString("Total amount to pay is " +total()+ "\u20ac", 210,90);
break;
default: g.drawString("Error please select an option between 1-5 !!" , 170,90);
}
}//end of Paint
public double total() //Calculation method
{
return ((coffee*coffeect)+(tea*teact)+(soup*soupct)+(water*waterct));
}
public void actionPerformed(ActionEvent e)
{
anwser = Integer.parseInt(choiceinput1.getText()); // Scanning user input
choiceinput1.setText("");
repaint(); // painting value to screen
switch(anwser) // incementation and price of product
{
case 1:
coffeect++;
coffee=2;
break;
case 2:
tea =2;
teact++;
break;
case 3:
soup = 2;
soupct++;
break;
case 4:
water=1.5;
waterct++;
break;
case 5:
choiceinput1.setEditable(false); //If user will press 5 program will end with no option to reenter choice
break;
} //Emd of switch
}//End of ActionPerformed
}//End of public class