Welcome to Dream.In.Code
Become a Java Expert!

Join 149,528 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,483 people online right now. Registration is fast and FREE... Join Now!




JApplet help

 
Reply to this topicStart new topic

JApplet help, involes with radiobutton and checkbox

wille2511
25 May, 2007 - 10:15 AM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 46


My Contributions
this program is ordering pizza. program works well. but i theres one glitch not really but for my needs thats when u run it and open in html you will see "1st topping is free",

how can i make that 1st topping(any) free of charge?

so far im just charging all the toppings for there fisrt choose

CODE
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
public class ApPizza extends JApplet implements ItemListener
{
    //TOPPINGS
    private JCheckBox cheeseTopCB, pepTopCB, hamTopCB, sausTopCB, pineTopCB,

baconTopCB;
    //SIZES
    private JRadioButton smallRB, mediumRB, largeRB;
    private ButtonGroup sizeSelectBG;
    
    //COST OF TOPPINGS
    private double toppings = 1.50;

    //TOTAL AMOUNT
    private double cost = 0;


    public void init()
    {
      Container c = getContentPane();
      c.setLayout(null);

      cheeseTopCB = new JCheckBox("Extra Cheese");
      pepTopCB = new JCheckBox("Pepperoni");
      hamTopCB = new JCheckBox("Ham");
      sausTopCB = new JCheckBox("Sausage");
      pineTopCB = new JCheckBox("Pineapple");
      baconTopCB = new JCheckBox("Bacon");

      smallRB = new JRadioButton("Small $4.99");
      mediumRB = new JRadioButton("Med. $9.99");
      largeRB = new JRadioButton("Large $12.99");

      
      cheeseTopCB.setSize(100,30);
      pepTopCB.setSize(100,30);
      hamTopCB.setSize(100,30);
      sausTopCB.setSize(100,30);
      pineTopCB.setSize(100,30);
      baconTopCB.setSize(100,30);
      smallRB.setSize(100,30);
      mediumRB.setSize(100,30);
      largeRB.setSize(100,30);

      cheeseTopCB.setLocation(50,150);
      pepTopCB.setLocation(50,170);
      hamTopCB.setLocation(50,190);
      sausTopCB.setLocation(170,150);
      pineTopCB.setLocation(170,170);
      baconTopCB.setLocation(170,190);
      smallRB.setLocation(170,50);
      mediumRB.setLocation(170,70);
      largeRB.setLocation(170,90);


      cheeseTopCB.addItemListener(this);
      pepTopCB.addItemListener(this);
      hamTopCB.addItemListener(this);
      sausTopCB.addItemListener(this);
      pineTopCB.addItemListener(this);
      baconTopCB.addItemListener(this);
      smallRB.addItemListener(this);
      mediumRB.addItemListener(this);
      largeRB.addItemListener(this);

      
      c.add(cheeseTopCB);
      c.add(pepTopCB);
      c.add(hamTopCB);
      c.add(sausTopCB);
      c.add(pineTopCB);
      c.add(baconTopCB);
      c.add(smallRB);
      c.add(mediumRB);
      c.add(largeRB);

      sizeSelectBG = new ButtonGroup();
      sizeSelectBG.add(smallRB);
      sizeSelectBG.add(mediumRB);
      sizeSelectBG.add(largeRB);
    }

    public void paint(Graphics g)//THE STRINGS BEING CREATED
    {
    DecimalFormat blah = new DecimalFormat("0.00");
    super.paint(g);

    g.setFont(new Font("Comic Sans",Font.PLAIN, 24));
    g.drawString("Welcome to PizzaStuff", 30,30);

    g.setFont(new Font("Comic Sans",Font.BOLD, 14));
    g.drawString("Choose Size:", 30,75);
    
    g.setFont(new Font("Comic Sans",Font.BOLD, 14));
    g.drawString("Choose Toppings:$1.50 per Topping(1st topping Free)", 30,150);

    g.setFont(new Font("Comic Sans",Font.BOLD, 14));
    g.drawString("Total: $" +blah.format(cost), 200,300);

    }
    
    public void itemStateChanged(ItemEvent e)
    {
        //TOPPING BUTTONS
      if(e.getSource() == cheeseTopCB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + toppings;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - toppings;
      }

      if(e.getSource() == pepTopCB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + toppings;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - toppings;
      }

      if(e.getSource() == hamTopCB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + toppings;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - toppings;
      }

      if(e.getSource() == sausTopCB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + toppings;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - toppings;
      }

      if(e.getSource() == pineTopCB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + toppings;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - toppings;
      }

      if(e.getSource() == baconTopCB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + toppings;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - toppings;
      }

        //PIZZA SIZES IN RADIOBUTTON
            if(e.getSource() == smallRB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + 4.99;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - 4.99;
      }

      if(e.getSource() == mediumRB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + 9.99;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - 9.99;
      }

      if(e.getSource() == largeRB)
      {
            if(e.getStateChange() == ItemEvent.SELECTED)
            cost = cost + 12.99;
          if(e.getStateChange() == ItemEvent.DESELECTED)
            cost = cost - 12.99;
      }
    
    repaint();

    }

}


html
CODE

<html>
<head>
<title>BUY PIZZA</title>
<body>
<object code = "ApPizza.class" width = 600 height = 480>
</object>
</body>
</html>

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: JApplet Help
25 May, 2007 - 11:28 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
well I think you should add another if inside every if statement of topping:
if nothing is checked of the toppings then cost=0;


And in the html code instead of object I think there should be applet like :

CODE

<html>
<head>
<title>BUY PIZZA</title>
<body>
<applet code = "ApPizza.class" width = 600 height = 480>
</applet>
</body>
</html>

User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: JApplet Help
25 May, 2007 - 11:37 AM
Post #3

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
I just want to point out again how funny I believe Japplet sounds.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: JApplet Help
25 May, 2007 - 11:40 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
blink.gif ?? blink.gif why is that?

QUOTE
I just want to point out again how funny I believe Japplet sounds.

User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: JApplet Help
25 May, 2007 - 11:47 AM
Post #5

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Because I'm inappropriate
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: JApplet Help
25 May, 2007 - 11:52 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
OOOOOO all righty then
User is offlineProfile CardPM
+Quote Post

wille2511
RE: JApplet Help
25 May, 2007 - 07:44 PM
Post #7

New D.I.C Head
*

Joined: 24 Oct, 2006
Posts: 46


My Contributions
QUOTE(PennyBoki @ 25 May, 2007 - 12:28 PM) *

well I think you should add another if inside every if statement of topping:
if nothing is checked of the toppings then cost=0;


And in the html code instead of object I think there should be applet like :

CODE

<html>
<head>
<title>BUY PIZZA</title>
<body>
<applet code = "ApPizza.class" width = 600 height = 480>
</applet>
</body>
</html>



i was thinking that just the same but what would i put so it would execute, i so far dont know much about applets
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month