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.
- import java.awt.event.*;
- import java.awt.*;
- import java.util.MissingResourceException;
- import java.util.ResourceBundle;
- public class SquaresAndCircles {
- private static final ResourceBundle STRINGS = ResourceBundle.getBundle("squaresandcircles");
- public static String getString(String key) {
- try {
- return STRINGS.getString(key);
- }
- catch (MissingResourceException e) {
- return '!' + key + '!';
- }
- }
- public static void main(String[] args) {
- SquaresAndCirclesWindow appwin = new SquaresAndCirclesWindow(getString("Title"));
- appwin.setVisible(true);
- }
- }
- class SquaresAndCirclesWindow extends Frame implements ActionListener {
- private static final String QUIT = "quit";
- private static final String REDRAW = "redraw";
- private static final String SQUARES = "squares";
- private static final String CIRCLES = "circles";
- private static final int SHAPES = 16;
- private DrawingArea canvas;
- MenuItem squares, circles;
- SquaresAndCirclesWindow(String name) {
- super(name);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- Menu filemenu = new Menu(SquaresAndCircles.getString("File"));
- MenuItem redraw = new MenuItem(SquaresAndCircles.getString("Redraw"));
- redraw.setActionCommand(REDRAW);
- redraw.addActionListener(this);
- redraw.setShortcut(new MenuShortcut(redraw.getLabel().charAt(0)));
- filemenu.add(redraw);
- filemenu.addSeparator();
- MenuItem quit = new MenuItem(SquaresAndCircles.getString("Quit"));
- quit.setActionCommand(QUIT);
- quit.addActionListener(this);
- quit.setShortcut(new MenuShortcut(quit.getLabel().charAt(0)));
- filemenu.add(quit);
- Menu modemenu = new Menu(SquaresAndCircles.getString("Mode"), true);
- circles = new MenuItem(SquaresAndCircles.getString("Circles"));
- circles.setActionCommand(CIRCLES);
- circles.addActionListener(this);
- circles.setShortcut(new MenuShortcut(circles.getLabel().charAt(0)));
- modemenu.add(circles);
- squares = new MenuItem(SquaresAndCircles.getString("Squares"));
- squares.setActionCommand(SQUARES);
- squares.addActionListener(this);
- squares.setShortcut(new MenuShortcut(squares.getLabel().charAt(0)));
- modemenu.add(squares);
- MenuBar menubar = new MenuBar();
- menubar.add(filemenu);
- menubar.add(modemenu);
- setMenuBar(menubar);
- setLayout(new BorderLayout());
- canvas = new DrawingArea(SHAPES);
- add(canvas, BorderLayout.CENTER);
- setModeMenu();
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- int width = screenSize.width / 3;
- int height = screenSize.height / 4;
- int x = (screenSize.width - width) / 2;
- int y = (screenSize.height - height) / 3;
- setBounds(x, y, width, height);
- setResizable(true);
- }
- private void setModeMenu() {
- int mode = canvas.getMode();
- circles.setEnabled(mode != DrawingArea.CIRCLES);
- squares.setEnabled(mode != DrawingArea.SQUARES);
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getActionCommand() == REDRAW) {
- canvas.repaint();
- }
- if (e.getActionCommand() == SQUARES) {
- canvas.setMode(DrawingArea.SQUARES);
- setModeMenu();
- canvas.repaint();
- }
- if (e.getActionCommand() == CIRCLES) {
- canvas.setMode(DrawingArea.CIRCLES);
- setModeMenu();
- canvas.repaint();
- }
- if (e.getActionCommand() == QUIT) {
- System.exit(0);
- }
- }
- }
- class DrawingArea extends Canvas {
- static final int SQUARES = 0;
- static final int CIRCLES = 1;
- protected int shapes = 10;
- private int mode = SQUARES;
- DrawingArea(int n) {
- this.shapes = n;
- }
- public void setMode(int mode) {
- this.mode = mode;
- }
- public int getMode() {
- return mode;
- }
- public void paint(Graphics g) {
- int width = getSize().width, height = getSize().height;
- int maxsize = Math.min(width, height);
- g.setClip(null);
- Image offscreen = createImage(width, height);
- Graphics2D g2d = (Graphics2D) offscreen.getGraphics();
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2d.setColor(Color.white);
- g2d.fillRect(0, 0, width, height);
- for (int n = 0; n < shapes; n++) {
- Color c = new Color(rint(0, 256), rint(0, 256), rint(0, 256));
- int l = rint(10, maxsize / 2);
- int x = rint(0, width - l);
- int y = rint(0, height - l);
- g2d.setColor(c);
- if (mode == SQUARES)
- g2d.fillRect(x, y, l, l);
- else if (mode == CIRCLES)
- g2d.fillOval(x, y, l, l);
- }
- g.drawImage(offscreen, 0, 0, this);
- }
- public void update(Graphics g) {
- paint(g);
- }
- private int rint(int min, int max) {
- return min + ((int) (Math.floor(Math.random() * (max - min))));
- }
- }
$ javac SquaresAndCircles.java
$ java SquaresAndCircles
Test the program in another language:
$ java -Duser -language=fr -Duser.country=FR SquaresAndCircles
Comments