Applet I18N : Applet « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » AppletScreenshots 
Applet I18N
  
/**
 @version 1.2 1999-10-13
 @author Cay Horstmann
 */

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Retire extends JApplet {
  public void init() {
    GridBagLayout gbl = new GridBagLayout();
    getContentPane().setLayout(gbl);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 100;
    gbc.weighty = 0;

    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.EAST;
    add(languageLabel, gbc, 0011);
    add(savingsLabel, gbc, 0111);
    add(contribLabel, gbc, 2111);
    add(incomeLabel, gbc, 4111);
    add(currentAgeLabel, gbc, 0211);
    add(retireAgeLabel, gbc, 2211);
    add(deathAgeLabel, gbc, 4211);
    add(inflationPercentLabel, gbc, 0311);
    add(investPercentLabel, gbc, 2311);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.WEST;
    add(localeCombo, gbc, 1021);
    add(savingsField, gbc, 1111);
    add(contribField, gbc, 3111);
    add(incomeField, gbc, 5111);
    add(currentAgeField, gbc, 1211);
    add(retireAgeField, gbc, 3211);
    add(deathAgeField, gbc, 5211);
    add(inflationPercentField, gbc, 1311);
    add(investPercentField, gbc, 3311);

    computeButton.setName("computeButton");
    computeButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        getInfo();
        updateData();
        updateGraph();
      }
    });
    add(computeButton, gbc, 5311);

    gbc.weighty = 100;
    gbc.fill = GridBagConstraints.BOTH;
    add(retireCanvas, gbc, 0441);
    add(new JScrollPane(retireText), gbc, 4421);
    retireText.setEditable(false);
    retireText.setFont(new Font("Monospaced", Font.PLAIN, 10));

    info.setSavings(0);
    info.setContrib(9000);
    info.setIncome(60000);
    info.setCurrentAge(35);
    info.setRetireAge(65);
    info.setDeathAge(85);
    info.setInvestPercent(0.1);
    info.setInflationPercent(0.05);

    localeCombo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        setCurrentLocale(localeCombo.getSelectedIndex());
      }
    });

    locales = new Locale[] { Locale.US, Locale.CHINA, Locale.GERMANY };

    int localeIndex = 0// US locale is default selection

    for (int i = 0; i < locales.length; i++)
      // if current locale one of the choices, we'll select it
      if (getLocale().equals(locales[i]))
        localeIndex = i;

    setCurrentLocale(localeIndex);
  }

  public void add(Component c, GridBagConstraints gbc, int x, int y, int w,
      int h) {
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    getContentPane().add(c, gbc);
  }

  public void setCurrentLocale(int localeIndex) {
    currentLocale = locales[localeIndex];

    localeCombo.removeAllItems();
    for (int i = 0; i < locales.length; i++) {
      String language = locales[i].getDisplayLanguage(currentLocale);
      localeCombo.addItem(language);
    }
    localeCombo.setSelectedIndex(localeIndex);

    res = ResourceBundle.getBundle("RetireResources", currentLocale);
    currencyFmt = NumberFormat.getCurrencyInstance(currentLocale);
    numberFmt = NumberFormat.getNumberInstance(currentLocale);
    percentFmt = NumberFormat.getPercentInstance(currentLocale);

    updateDisplay();
    updateInfo();
    updateData();
    updateGraph();
  }

  public void updateDisplay() {
    languageLabel.setText(res.getString("language"));
    savingsLabel.setText(res.getString("savings"));
    contribLabel.setText(res.getString("contrib"));
    incomeLabel.setText(res.getString("income"));
    currentAgeLabel.setText(res.getString("currentAge"));
    retireAgeLabel.setText(res.getString("retireAge"));
    deathAgeLabel.setText(res.getString("deathAge"));
    inflationPercentLabel.setText(res.getString("inflationPercent"));
    investPercentLabel.setText(res.getString("investPercent"));
    computeButton.setText(res.getString("computeButton"));

    validate();
  }

  public void updateInfo() {
    savingsField.setText(currencyFmt.format(info.getSavings()));
    contribField.setText(currencyFmt.format(info.getContrib()));
    incomeField.setText(currencyFmt.format(info.getIncome()));
    currentAgeField.setText(numberFmt.format(info.getCurrentAge()));
    retireAgeField.setText(numberFmt.format(info.getRetireAge()));
    deathAgeField.setText(numberFmt.format(info.getDeathAge()));
    investPercentField.setText(percentFmt.format(info.getInvestPercent()));
    inflationPercentField.setText(percentFmt.format(info
        .getInflationPercent()));
  }

  public void updateData() {
    retireText.setText("");
    MessageFormat retireMsg = new MessageFormat("");
    retireMsg.setLocale(currentLocale);
    retireMsg.applyPattern(res.getString("retire"));

    for (int i = info.getCurrentAge(); i <= info.getDeathAge(); i++) {
      Object[] args = new Integer(i)new Double(info.getBalance(i)) };
      retireText.append(retireMsg.format(args"\n");
    }
  }

  public void updateGraph() {
    retireCanvas.setColorPre((Colorres.getObject("colorPre"));
    retireCanvas.setColorGain((Colorres.getObject("colorGain"));
    retireCanvas.setColorLoss((Colorres.getObject("colorLoss"));
    retireCanvas.setInfo(info);
    repaint();
  }

  public void getInfo() {
    try {
      info.setSavings(currencyFmt.parse(savingsField.getText())
          .doubleValue());
      info.setContrib(currencyFmt.parse(contribField.getText())
          .doubleValue());
      info.setIncome(currencyFmt.parse(incomeField.getText())
          .doubleValue());
      info.setCurrentAge(numberFmt.parse(currentAgeField.getText())
          .intValue());
      info.setRetireAge(numberFmt.parse(retireAgeField.getText())
          .intValue());
      info.setDeathAge(numberFmt.parse(deathAgeField.getText())
          .intValue());
      info.setInvestPercent(percentFmt
          .parse(investPercentField.getText()).doubleValue());
      info.setInflationPercent(percentFmt.parse(
          inflationPercentField.getText()).doubleValue());
    catch (ParseException exception) {
    }
  }

  private JTextField savingsField = new JTextField(10);

  private JTextField contribField = new JTextField(10);

  private JTextField incomeField = new JTextField(10);

  private JTextField currentAgeField = new JTextField(4);

  private JTextField retireAgeField = new JTextField(4);

  private JTextField deathAgeField = new JTextField(4);

  private JTextField inflationPercentField = new JTextField(6);

  private JTextField investPercentField = new JTextField(6);

  private JTextArea retireText = new JTextArea(1025);

  private RetireCanvas retireCanvas = new RetireCanvas();

  private JButton computeButton = new JButton();

  private JLabel languageLabel = new JLabel();

  private JLabel savingsLabel = new JLabel();

  private JLabel contribLabel = new JLabel();

  private JLabel incomeLabel = new JLabel();

  private JLabel currentAgeLabel = new JLabel();

  private JLabel retireAgeLabel = new JLabel();

  private JLabel deathAgeLabel = new JLabel();

  private JLabel inflationPercentLabel = new JLabel();

  private JLabel investPercentLabel = new JLabel();

  private RetireInfo info = new RetireInfo();

  private Locale[] locales;

  private Locale currentLocale;

  private JComboBox localeCombo = new JComboBox();

  private ResourceBundle res;

  private NumberFormat currencyFmt;

  private NumberFormat numberFmt;

  private NumberFormat percentFmt;
}

class RetireInfo {
  public double getBalance(int year) {
    if (year < currentAge)
      return 0;
    else if (year == currentAge) {
      age = year;
      balance = savings;
      return balance;
    else if (year == age)
      return balance;
    if (year != age + 1)
      getBalance(year - 1);
    age = year;
    if (age < retireAge)
      balance += contrib;
    else
      balance -= income;
    balance = balance * ((investPercent - inflationPercent));
    return balance;
  }

  public double getSavings() {
    return savings;
  }

  public double getContrib() {
    return contrib;
  }

  public double getIncome() {
    return income;
  }

  public int getCurrentAge() {
    return currentAge;
  }

  public int getRetireAge() {
    return retireAge;
  }

  public int getDeathAge() {
    return deathAge;
  }

  public double getInflationPercent() {
    return inflationPercent;
  }

  public double getInvestPercent() {
    return investPercent;
  }

  public void setSavings(double x) {
    savings = x;
  }

  public void setContrib(double x) {
    contrib = x;
  }

  public void setIncome(double x) {
    income = x;
  }

  public void setCurrentAge(int x) {
    currentAge = x;
  }

  public void setRetireAge(int x) {
    retireAge = x;
  }

  public void setDeathAge(int x) {
    deathAge = x;
  }

  public void setInflationPercent(double x) {
    inflationPercent = x;
  }

  public void setInvestPercent(double x) {
    investPercent = x;
  }

  private double savings;

  private double contrib;

  private double income;

  private int currentAge;

  private int retireAge;

  private int deathAge;

  private double inflationPercent;

  private double investPercent;

  private int age;

  private double balance;
}

class RetireCanvas extends JPanel {
  public RetireCanvas() {
    setSize(400200);
  }

  public void setInfo(RetireInfo newInfo) {
    info = newInfo;
  }

  public void paint(Graphics g) {
    if (info == null)
      return;

    double minValue = 0;
    double maxValue = 0;
    int i;
    for (i = info.getCurrentAge(); i <= info.getDeathAge(); i++) {
      double v = info.getBalance(i);
      if (minValue > v)
        minValue = v;
      if (maxValue < v)
        maxValue = v;
    }
    if (maxValue == minValue)
      return;

    int barWidth = getWidth()
        (info.getDeathAge() - info.getCurrentAge() 1);
    double scale = getHeight() (maxValue - minValue);

    for (i = info.getCurrentAge(); i <= info.getDeathAge(); i++) {
      int x1 = (i - info.getCurrentAge()) * barWidth + 1;
      int y1;
      double v = info.getBalance(i);
      int height;
      int yOrigin = (int) (maxValue * scale);

      if (v >= 0) {
        y1 = (int) ((maxValue - v* scale);
        height = yOrigin - y1;
      else {
        y1 = yOrigin;
        height = (int) (-v * scale);
      }

      if (i < info.getRetireAge())
        g.setColor(colorPre);
      else if (v >= 0)
        g.setColor(colorGain);
      else
        g.setColor(colorLoss);
      g.fillRect(x1, y1, barWidth - 2, height);
      g.setColor(Color.black);
      g.drawRect(x1, y1, barWidth - 2, height);
    }
  }

  public void setColorPre(Color color) {
    colorPre = color;
  }

  public void setColorGain(Color color) {
    colorGain = color;
  }

  public void setColorLoss(Color color) {
    colorLoss = color;
  }

  private RetireInfo info = null;

  private Color colorPre;

  private Color colorGain;

  private Color colorLoss;
}

class RetireResources extends java.util.ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }

  static final Object[][] contents = // BEGIN LOCALIZE
  "language""Language" }"computeButton""Compute" },
      "savings""Prior Savings" },
      "contrib""Annual Contribution" },
      "income""Retirement Income" }"currentAge""Current Age" },
      "retireAge""Retirement Age" },
      "deathAge""Life Expectancy" },
      "inflationPercent""Inflation" },
      "investPercent""Investment Return" },
      "retire""Age: {0,number} Balance: {1,number,currency}" },
      "colorPre", Color.blue }"colorGain", Color.white },
      "colorLoss", Color.red }
  // END LOCALIZE
  };
}

///////////////////////////////////////////////////
///////////////////////////////////////////////////
class RetireResources_de
   extends java.util.ListResourceBundle
{  public Object[][] getContents() { return contents; }
   static final Object[][] contents =
   {  // BEGIN LOCALIZE
      "language""Sprache" },
      "computeButton""Rechnen" },
      "savings""Vorherige Ersparnisse" },
      "contrib""Jrliche Einzahlung" },
      "income""Einkommen nach Ruhestand" },
      "currentAge""Jetziges Alter" },
      "retireAge""Ruhestandsalter" },
      "deathAge""Lebenserwartung" },
      "inflationPercent""Inflation" },
      "investPercent""Investitionsgewinn" },
      "retire""Alter: {0,number} Guthaben: {1,number,currency}" },
      "colorPre", Color.yellow },
      "colorGain", Color.black },
      "colorLoss", Color.red }

      // END LOCALIZE
   };
}


///////////////////////////////////////////////////
///////////////////////////////////////////////////
/**
 @version 1.2 1999-10-13
 @author Cay Horstmann
 */


/*

import java.util.*;
import java.awt.*;

public class RetireResources_zh
   extends java.util.ListResourceBundle
{  public Object[][] getContents() { return contents; }
   static final Object[][] contents =
   {  // BEGIN LOCALIZE
      { "language", "\u8bed\u8a00" },
      { "computeButton", "\u8ba1\u7b97" },
      { "savings", "\u65e2\u5b58" },
      { "contrib", "\u6bcf\u5e74\u5b58\u91d1" },
      { "income", "\u9000\u4f11\u6536\u5165" },
      { "currentAge", "\u73b0\u5cad" },
      { "retireAge", "\u9000\u4f11\u5e74\u9f84" },
      { "deathAge", "\u9884\u671f\u5bff\u547d" },
      { "inflationPercent", "\u901a\u8d27\u81a8\u6da8" },
      { "investPercent", "\u6295\u8d44\u62a5\u916c" },
      { "retire",
         "\u5e74\u9f84: {0,number} \u603b\u7ed3: {1,number,currency}" },
      { "colorPre", Color.red },
      { "colorGain", Color.blue },
      { "colorLoss", Color.yellow }

      // END LOCALIZE
   };
}


*/
           
         
    
  
Related examples in the same category
1. Icon Demo Applet
2. Socket Applet
3. Applet Socket Quote
4. Applet Print
5. Applet System Properties
6. Applet Sound
7. Show Document
8. Applet communication (talk to each other)
9. Get Applets
10. JDBC applet
11. An application and an appletAn application and an applet
12. Applet and Swing ComponentsApplet and Swing Components
13. Icon behavior in JbuttonsIcon behavior in Jbuttons
14. Signed Applet
15. Load resource in Applet
16. Scribble AppletScribble Applet
17. Toggle buttonToggle button
18. Bouncing CircleBouncing Circle
19. Applet Menu Bar Demo
20. Applet clock demoApplet clock demo
21. Applet event testerApplet event tester
22. Applet with parameters
23. First applet
24. AppletViewer - a simple Applet Viewer program
25. A simple loan calculator appletA simple loan calculator applet
26. URLButton -- a Button-like object that jumps to a URL
27. Get list of APPLET tags in one HTML file
28. Demonstration of Applet Methods
29. Demonstrates getParameterInfo() and getAppletInfo()
30. Walking Text Demo
31. Java Applets Can Run CGI's (on some browsers)
32. Demo Choice Applet
33. Demo Applet: Connect to Legacy System
34. ShowDocApplet: try out showDocument()
35. Bookmarks
36. Java Wave Applet Demo
37. Slide Puzzle
38. A class to allow use of the ncsa ISMAP format in java applets
39. File Read Applet
40. Image Loader Applet
41. Thread Race Applet
42. Applet: Print from an Applet
43. Calling methods of an Applet from JavaScript code
44. Read an applet parameters
45. Change an applet background color
46. Passing Parameters to Java Applet
47. Display message in browser status bar
48. Your own Applet runtime environment
49. This applet is a simple button that plays an audio clip when pushed
w___w___w___.__j___a__v_a2__s_._c_o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.