|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Beginner Applet Question
Hello. I had to write a simple applet and draw a string onto a canvas as a grphics object. I was able to do this, however, the \n did not create a new line. So, I got around the issue by creating multiple strings to the canvas (i.e. totalDisplay1, 2, and 3).
However, I thought that I'm probably just missing something basic that would allow me to print multiple lines using one string. FYI, we have to use the older awt coding. Thanks for your time. My code follows below ... //This applet prompts the user to select either bread, eggs, or milk //then the quantity ordered is used to determine the total price to be charged. import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.text.NumberFormat; public class SelectItem extends Applet { private Panel topPanel; private List foodItem = new List (4, false); private Label costPerQuantity = new Label ("Select Food Item"); private Label quantity = new Label ("Quantity:"); private TextField amount = new TextField (5); private Button order = new Button ("Order"); private CanCan can = new CanCan(); private final double costMilk = 1.47; private final double costBread = 2.12; private final double costEggs = 1.90; private double totalCost = 0; private String totalDisplay1; private String totalDisplay2; private String totalDisplay3; private NumberFormat nf; public void init() { topPanel = new Panel(); topPanel.setLayout (new GridLayout(1,5) ); topPanel.add (foodItem); foodItem.add ("Milk"); foodItem.add ("Bread"); foodItem.add ("Eggs"); topPanel.add (costPerQuantity); topPanel.add (quantity); topPanel.add (amount); topPanel.add (order); setLayout(new BorderLayout() ); add (topPanel, "North"); add (can, "Center"); can.setBackground(Color.blue); nf = NumberFormat.getCurrencyInstance(); foodItem.addItemListener(can); order.addActionListener(can); setSize(750,200); }//end of init() method class CanCan extends Canvas implements ActionListener, ItemListener { //changes the costPerQuantity Textfield depending on which foodItem is selected public void itemStateChanged(ItemEvent e) { if ( foodItem.getSelectedItem().equals("Milk") ) costPerQuantity.setText(nf.format(costMilk) + " per Quart"); //sets the price for milk else if ( foodItem.getSelectedItem().equals("Bread") ) costPerQuantity.setText(nf.format(costBread) + " per Loaf"); //sets the price for bread else costPerQuantity.setText(nf.format(costEggs) + " per Dozen"); //sets the price for eggs }//end of itemStateChanged() method //changes strings to reflect the total price of order public void actionPerformed(ActionEvent e) { int amountDesired = Integer.parseInt(amount.getText() );//reads in amount desired. if ( foodItem.getSelectedItem().equals("Milk") ) { totalCost = amountDesired * costMilk; totalDisplay1 = "Item: Milk"; totalDisplay2 = ("Quantity: " + amount.getText() ); totalDisplay3 = ("Total Cost: " + nf.format(totalCost) ); repaint(); }//end of milk case else if ( foodItem.getSelectedItem().equals("Bread") ) { totalCost = amountDesired * costBread; totalDisplay1 = "Item: Bread"; totalDisplay2 = ("Quantity: " + amount.getText() ); totalDisplay3 = ("Total Cost: " + nf.format(totalCost) ); repaint(); }//end of the bread case else { totalCost = amountDesired * costEggs; totalDisplay1 = "Item: Eggs"; totalDisplay2 = ("Quantity: " + amount.getText() ); totalDisplay3 = ("Total Cost: " + nf.format(totalCost) ); repaint(); }//end of eggs case }//end of actionPerformed() method //draws the total price of the selected item into the canvas public void paint (Graphics g) { g.setFont(new Font("Arial", Font.PLAIN, 16) ); g.setColor(Color.white); g.fillRect(75, 25, 500, 100); g.setColor(Color.black); g.drawString(totalDisplay1, 85, 40); g.drawString(totalDisplay2, 85, 60); g.drawString(totalDisplay3, 85, 80); }//end of paint() method }//end of CanCan class }//end of SelectItem class |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Beginner Applet Question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|