Automat z napojami dodanie efektów

0

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 
3

Sugerowałbym dodać jakiś ludzki design do tej aplikacji, taki z użyciem klas i z możliwoscią rozszerzenia tego a nie GodObject z jedną UberMetodą ;]

0
Shalom napisał(a):

Sugerowałbym dodać jakiś ludzki design do tej aplikacji, taki z użyciem klas i z możliwoscią rozszerzenia tego a nie GodObject z jedną UberMetodą ;]

Jestem poczatkujacym programista i jezeli wiedzialbym jak to zrobic to pewnie juz dawno bym zrobil. Móglbys wrzucic jakis przyklad kodu ?

0

Korzystajac z Twojego tematu - opłaca się w ogóle zagłębiać w Aplety? Warto na to marnować czas?

0

Czy ktos moglby mi pomoc jak wrzucac ikony

Chodzi mi o to aby przy kazdym wyborze np uzytkownik wybierze kawe wyswietla sie informacja You have choosen coffee i mala ikona kawy. Moglby ktos wrzucic jakis przykladowy kod abym mial odniesienie ? Nie chce aby ktos cos robic za mnie tylko o jakis przyklad, szukalem w google ale nic z tego nie rozumiem.

import javax.swing.JButton;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Assignment2B00092367 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;
JButton clear;






	public void init()
	{

	//create label,setting color of background and text
	welcomscr = new Label("Welcome  !!! Please choose from menu fallowing options ");
	welcomscr.setLocation(10, 500);
	welcomscr.setForeground(Color.blue);
	add(welcomscr);

	clear = new JButton("Restart");
	add(clear);
	clear.addActionListener(this);

	choiceprompt1 = new Label("  Please enter only number 1 to 5: ");
	add(choiceprompt1);
	choiceinput1 = new TextField(10);
	choiceinput1.addActionListener(this);
	add(choiceinput1);

	//create textfield

	//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.setColor(Color.red);
				g.drawString("No option selected yet" , 210,90);
				break;
				case 1:
				g.setColor(Color.green);
				g.drawString("You have chosen Coffee ", 210,90);
				break;
				case 2:
				g.setColor(Color.green);
				g.drawString("You have chosen Tea", 210,90);
				break;
				case 3:
				g.setColor(Color.green);
				g.drawString("You have chosen Soup ", 210,90);
				break;
				case 4:
				g.setColor(Color.green);
				g.drawString("You have chosen Water ", 210,90);
				break;
				case 5:
				g.setColor(Color.red);
				g.drawString("Total amount to pay is "	+total()+ "\u20ac" + "  Items bought " + counter() , 170,90);
				break;

				default:
				g.setColor(Color.red);
				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 int counter()
				{
				return (coffeect+teact+soupct+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

						if (e.getSource() == clear)
						{
							choiceinput1.setText("");
							repaint();

						}


				}//End of ActionPerformed
}//End of public class
0

Wczytaj (w metodzie init) obrazki:

URL url = getCodeBase();
cupOfTea = getImage(url,"tea.gif"); //bez typu zmiennej, cupOfTea musi być polem w klasie
...
    case 2:
          g.setColor(Color.green);
          g.drawString("You have chosen Tea", 210,90);
          g.drawImage(cupOfTea,...,...,null);
          break;

Położenie obrazka musisz sobie policzyć.

P.S. Program wygląda wyjątkowo paskudnie, najbardziej potrzebnym efektem jest poprawienie wyglądu.
Stojąc przed automatem, chciałbym wiedzieć ile będą mnie kosztować dotychczasowe wybory. Kwota do zapłaty powinna być aktualizowana na bieżąco.

0

Hej Ic3m4n, a nie myślałeś, by - tak jak koledzy wspomnieli - zostawić te applety w spokoju (bo nikt tego już nie używa) na korzyść czegokolwiek innego, prostszego w tworzeniu "efektów"? :) Nie wiem, choćby JSF z Primeface - budowa UI prosta jak konstrukcja cepa, bo to framework widgetowy i składasz interfejs jak z klocków. I bardzo niski próg wejścia, sensowny interfejs zrobisz od 0 w ciągu bardzo krótkiego czasu. NetBeansem czy IntelliJ wygenerujesz sobie taki projekt od kopa. W mojej opinii lepsze to niż masterowanie i kolorowanie appletów.

1 użytkowników online, w tym zalogowanych: 0, gości: 1