Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 24th, 2004, 09:04 PM
singhnx singhnx is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Chicago, IL
Posts: 9 singhnx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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
Attached Files
File Type: txt SelectItem.txt (3.8 KB, 276 views)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Beginner Applet Question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway