5

SquaresAndCircles

Create a file called squaresandcircles_en_US.properties for the English version:

Title=Squares & Circles
File=File
Redraw=Redraw
Quit=Quit
Mode=Mode
Circles=Circles
Squares=Squares

Create a file called squaresandcircles_fr_FR.properties for the French version:

Title=Carrés & Ronds
File=Fichier
Redraw=Dessiner
Quit=Quitter
Mode=Mode
Circles=Ronds
Squares=Carrés

Remember to save these files in ISO-8859-1.

  1. import java.awt.event.*;
  2. import java.awt.*;
  3.  
  4. import java.util.MissingResourceException;
  5. import java.util.ResourceBundle;
  6.  
  7. public class SquaresAndCircles {
  8.     private static final ResourceBundle STRINGS = ResourceBundle.getBundle("squaresandcircles");
  9.  
  10.     public static String getString(String key) {
  11.         try {
  12.             return STRINGS.getString(key);
  13.         }
  14.         catch (MissingResourceException e) {
  15.             return '!' + key + '!';
  16.         }
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         SquaresAndCirclesWindow appwin = new SquaresAndCirclesWindow(getString("Title"));
  21.         appwin.setVisible(true);
  22.     }
  23. }
  24.  
  25. class SquaresAndCirclesWindow extends Frame implements ActionListener {
  26.     private static final String QUIT = "quit";
  27.  
  28.     private static final String REDRAW = "redraw";
  29.  
  30.     private static final String SQUARES = "squares";
  31.  
  32.     private static final String CIRCLES = "circles";
  33.  
  34.     private static final int SHAPES = 16;
  35.  
  36.     private DrawingArea canvas;
  37.  
  38.     MenuItem squares, circles;
  39.  
  40.     SquaresAndCirclesWindow(String name) {
  41.         super(name);
  42.  
  43.         addWindowListener(new WindowAdapter() {
  44.             public void windowClosing(WindowEvent e) {
  45.                 System.exit(0);
  46.             }
  47.         });
  48.  
  49.         Menu filemenu = new Menu(SquaresAndCircles.getString("File"));
  50.  
  51.         MenuItem redraw = new MenuItem(SquaresAndCircles.getString("Redraw"));
  52.         redraw.setActionCommand(REDRAW);
  53.         redraw.addActionListener(this);
  54.         redraw.setShortcut(new MenuShortcut(redraw.getLabel().charAt(0)));
  55.         filemenu.add(redraw);
  56.  
  57.         filemenu.addSeparator();
  58.  
  59.         MenuItem quit = new MenuItem(SquaresAndCircles.getString("Quit"));
  60.         quit.setActionCommand(QUIT);
  61.         quit.addActionListener(this);
  62.         quit.setShortcut(new MenuShortcut(quit.getLabel().charAt(0)));
  63.         filemenu.add(quit);
  64.  
  65.         Menu modemenu = new Menu(SquaresAndCircles.getString("Mode"), true);
  66.  
  67.         circles = new MenuItem(SquaresAndCircles.getString("Circles"));
  68.         circles.setActionCommand(CIRCLES);
  69.         circles.addActionListener(this);
  70.         circles.setShortcut(new MenuShortcut(circles.getLabel().charAt(0)));
  71.         modemenu.add(circles);
  72.  
  73.         squares = new MenuItem(SquaresAndCircles.getString("Squares"));
  74.         squares.setActionCommand(SQUARES);
  75.         squares.addActionListener(this);
  76.         squares.setShortcut(new MenuShortcut(squares.getLabel().charAt(0)));
  77.         modemenu.add(squares);
  78.  
  79.         MenuBar menubar = new MenuBar();
  80.         menubar.add(filemenu);
  81.         menubar.add(modemenu);
  82.  
  83.         setMenuBar(menubar);
  84.  
  85.         setLayout(new BorderLayout());
  86.  
  87.         canvas = new DrawingArea(SHAPES);
  88.         add(canvas, BorderLayout.CENTER);
  89.  
  90.         setModeMenu();
  91.  
  92.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  93.  
  94.         int width = screenSize.width / 3;
  95.         int height = screenSize.height / 4;
  96.         int x = (screenSize.width - width) / 2;
  97.         int y = (screenSize.height - height) / 3;
  98.  
  99.         setBounds(x, y, width, height);
  100.         setResizable(true);
  101.     }
  102.  
  103.     private void setModeMenu() {
  104.         int mode = canvas.getMode();
  105.  
  106.         circles.setEnabled(mode != DrawingArea.CIRCLES);
  107.         squares.setEnabled(mode != DrawingArea.SQUARES);
  108.     }
  109.  
  110.     public void actionPerformed(ActionEvent e) {
  111.         if (e.getActionCommand() == REDRAW) {
  112.             canvas.repaint();
  113.         }
  114.         if (e.getActionCommand() == SQUARES) {
  115.             canvas.setMode(DrawingArea.SQUARES);
  116.             setModeMenu();
  117.             canvas.repaint();
  118.         }
  119.         if (e.getActionCommand() == CIRCLES) {
  120.             canvas.setMode(DrawingArea.CIRCLES);
  121.             setModeMenu();
  122.             canvas.repaint();
  123.         }
  124.         if (e.getActionCommand() == QUIT) {
  125.             System.exit(0);
  126.         }
  127.     }
  128. }
  129.  
  130. class DrawingArea extends Canvas {
  131.     static final int SQUARES = 0;
  132.  
  133.     static final int CIRCLES = 1;
  134.  
  135.     protected int shapes = 10;
  136.  
  137.     private int mode = SQUARES;
  138.  
  139.     DrawingArea(int n) {
  140.         this.shapes = n;
  141.     }
  142.  
  143.     public void setMode(int mode) {
  144.         this.mode = mode;
  145.     }
  146.  
  147.     public int getMode() {
  148.         return mode;
  149.     }
  150.  
  151.     public void paint(Graphics g) {
  152.         int width = getSize().width, height = getSize().height;
  153.         int maxsize = Math.min(width, height);
  154.  
  155.         g.setClip(null);
  156.  
  157.         Image offscreen = createImage(width, height);
  158.  
  159.         Graphics2D g2d = (Graphics2D) offscreen.getGraphics();
  160.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  161.  
  162.         g2d.setColor(Color.white);
  163.         g2d.fillRect(0, 0, width, height);
  164.  
  165.         for (int n = 0; n < shapes; n++) {
  166.             Color c = new Color(rint(0, 256), rint(0, 256), rint(0, 256));
  167.  
  168.             int l = rint(10, maxsize / 2);
  169.             int x = rint(0, width - l);
  170.             int y = rint(0, height - l);
  171.  
  172.             g2d.setColor(c);
  173.             if (mode == SQUARES)
  174.                 g2d.fillRect(x, y, l, l);
  175.             else if (mode == CIRCLES)
  176.                 g2d.fillOval(x, y, l, l);
  177.         }
  178.         g.drawImage(offscreen, 0, 0, this);
  179.  
  180.     }
  181.  
  182.     public void update(Graphics g) {
  183.         paint(g);
  184.     }
  185.  
  186.     private int rint(int min, int max) {
  187.         return min + ((int) (Math.floor(Math.random() * (max - min))));
  188.     }
  189. }
$ javac SquaresAndCircles.java
$ java SquaresAndCircles

Test the program in another language:

$ java -Duser -language=fr -Duser.country=FR SquaresAndCircles

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].