home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-12-14 | 40.5 KB | 1,993 lines |
- Calc.java:
-
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
-
- /**
- * A simple calculator
- */
- public class Calc extends Applet
- {
-
- Display display = new Display();
-
- /**
- * Initialize the Calc applet
- */
- public void init () {
-
- setLayout(new BorderLayout());
- Keypad keypad = new Keypad();
-
- add ("North", display);
- add ("Center", keypad);
- }
-
- /**
- * This allows the class to be used either as an applet
- * or as a standalone application.
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Calculator");
- Calc calc = new Calc ();
-
- calc.init ();
-
- f.setSize (210, 200);
- f.add ("Center", calc);
- f.show ();
- }
-
- class Keypad extends Panel
- implements ActionListener
- {
- Button b7;
- Button b8;
- Button b9;
- Button bDiv;
- Button b4;
- Button b5;
- Button b6;
- Button bMult;
- Button b1;
- Button b2;
- Button b3;
- Button bMin;
- Button bDec;
- Button b0;
- Button bSign;
- Button bPlus;
- Button bC;
- Button bEqu;
-
- /**
- * Initialize the keypad, add buttons, set colors, etc.
- */
- Keypad (){
-
- Font font = new Font ("Times", Font.BOLD, 14);
- Color functionColor = new Color (255, 255, 0);
- Color numberColor = new Color (0, 255, 255);
- Color equalsColor = new Color (0, 255, 0);
- setFont (font);
-
- b7 = new Button ("7");
- add (b7);
- b7.setBackground (numberColor);
- b7.addActionListener(this);
-
- b8 = new Button ("8");
- add (b8);
- b8.setBackground (numberColor);
- b8.addActionListener(this);
-
- b9 = new Button ("9");
- add (b9);
- b9.setBackground (numberColor);
- b9.addActionListener(this);
-
- bDiv = new Button ("/");
- add (bDiv);
- bDiv.setBackground (numberColor);
- bDiv.addActionListener(this);
-
- b4 = new Button ("4");
- add (b4);
- b4.setBackground (numberColor);
- b4.addActionListener(this);
-
- b5 = new Button ("5");
- add (b5);
- b5.setBackground (numberColor);
- b5.addActionListener(this);
-
- b6 = new Button ("6");
- add (b6);
- b6.setBackground (numberColor);
- b6.addActionListener(this);
-
- bMult = new Button ("x");
- add (bMult);
- bMult.setBackground (numberColor);
- bMult.addActionListener(this);
-
- b1 = new Button ("1");
- add (b1);
- b1.setBackground (numberColor);
- b1.addActionListener(this);
-
- b2 = new Button ("2");
- add (b2);
- b2.setBackground (numberColor);
- b2.addActionListener(this);
-
- b3 = new Button ("3");
- add (b3);
- b3.setBackground (numberColor);
- b3.addActionListener(this);
-
- bMin = new Button ("-");
- add (bMin);
- bMin.setBackground (numberColor);
- bMin.addActionListener(this);
-
- bDec = new Button (".");
- add (bDec);
- bDec.setBackground (numberColor);
- bDec.addActionListener(this);
-
- b0 = new Button ("0");
- add (b0);
- b0.setBackground (numberColor);
- b0.addActionListener(this);
-
- bSign = new Button ("+/-");
- add (bSign);
- bSign.setBackground (numberColor);
- bSign.addActionListener(this);
-
- bPlus = new Button ("+");
- add (bPlus);
- bPlus.setBackground (numberColor);
- bPlus.addActionListener(this);
-
- bC = new Button ("C");
- add (bC);
- bC.setBackground (functionColor);
- bC.addActionListener(this);
-
- add (new Label (""));
- add (new Label (""));
-
- bEqu = new Button ("=");
- add (bEqu);
- bEqu.setBackground (equalsColor);
- bEqu.addActionListener(this);
-
- setLayout (new GridLayout (5, 4, 4, 4));
- }
-
- public void actionPerformed(ActionEvent event)
- {
- Object object = event.getSource();
-
- if(object == bC)
- {
- display.Clear ();
- }
-
- if(object == bDec)
- {
- display.Dot ();
- }
-
- if(object == bPlus)
- {
- display.Plus ();
- }
-
- if(object == bMin)
- {
- display.Minus ();
- }
-
- if(object == bMult)
- {
- display.Mul ();
- }
-
- if(object == bDiv)
- {
- display.Div ();
- }
-
- if(object == bSign)
- {
- display.Chs ();
- }
-
- if(object == bEqu)
- {
- display.Equals ();
- }
-
- if(object == b0)
- {
- display.Digit ("0");
- }
-
- if(object == b1)
- {
- display.Digit ("1");
- }
-
- if(object == b2)
- {
- display.Digit ("2");
- }
-
- if(object == b3)
- {
- display.Digit ("3");
- }
-
- if(object == b4)
- {
- display.Digit ("4");
- }
-
- if(object == b5)
- {
- display.Digit ("5");
- }
-
- if(object == b6)
- {
- display.Digit ("6");
- }
-
- if(object == b7)
- {
- display.Digit ("7");
- }
-
- if(object == b8)
- {
- display.Digit ("8");
- }
-
- if(object == b9)
- {
- display.Digit ("9");
- }
-
- }
- }
- }
-
- /* -------------------------------------------------- */
-
- /**
- * The Keypad handles the input for the calculator
- * and writes to the display.
- */
-
- /* -------------------------------------------------- */
-
- /**
- * The Display class manages displaying the calculated result
- * as well as implementing the calculator function keys.
- */
- class Display extends Panel{
-
- double last = 0;
- int op = 0;
- boolean equals = false;
- int maxlen = 10;
- String s;
- Label readout = new Label("");
-
- /**
- * Initialize the display
- */
- Display () {
-
- setLayout(new BorderLayout());
- setBackground (Color.red);
- setFont (new Font ("Courier", Font.BOLD + Font.ITALIC, 30));
- readout.setAlignment(1);
- add ("Center",readout);
- repaint();
- Clear ();
- }
-
- /**
- * Handle clicking a digit.
- */
- void Digit (String digit) {
- checkEquals ();
-
- /*
- * Strip leading zeros
- */
- if (s.length () == 1 && s.charAt (0) == '0' && digit.charAt (0) != '.')
- s = s.substring (1);
-
- if (s.length () < maxlen)
- s = s + digit;
- showacc ();
- }
-
- /**
- * Handle a decimal point.
- */
- void Dot () {
- checkEquals ();
-
- /*
- * Already have '.'
- */
- if (s.indexOf ('.') != -1)
- return;
-
- if (s.length () < maxlen)
- s = s + ".";
- showacc ();
- }
-
- /**
- If the user clicks equals without
- clicking an operator
- * key first (+,-,x,/), zero the display.
- */
- private void checkEquals () {
- if (equals == true) {
- equals = false;
- s = "0";
- }
- }
-
- /**
- * Stack the addition operator for later use.
- */
- void Plus () {
- op = 1;
- operation ();
- }
-
- /**
- * Stack the subtraction operator for later use.
- */
- void Minus () {
- op = 2;
- operation ();
- }
-
- /**
- * Stack the multiplication operator for later use.
- */
- void Mul () {
- op = 3;
- operation ();
- }
-
- /**
- * Stack the division operator for later use.
- */
- void Div () {
- op = 4;
- operation ();
- }
-
- /**
- * Interpret the display value as a double, and store it
- * for later use (by Equals).
- */
- private void operation () {
- if (s.length () == 0) return;
-
- Double xyz = Double.valueOf (s);
- last = xyz.doubleValue ();
-
- equals = false;
- s = "0";
- }
- /**
- * Negate the current value and redisplay.
- */
- void Chs () {
- if (s.length () == 0) return;
-
- if (s.charAt (0) == '-') s = s.substring (1);
- else s = "-" + s;
-
- showacc ();
- }
-
- /**
- * Finish the last calculation and display the result.
- */
- void Equals () {
- double acc;
-
- if (s.length () == 0) return;
- Double xyz = Double.valueOf (s);
- switch (op) {
- case 1:
- acc = last + xyz.doubleValue ();
- break;
-
- case 2:
- acc = last - xyz.doubleValue ();
- break;
-
- case 3:
- acc = last * xyz.doubleValue ();
- break;
-
- case 4:
- acc = last / xyz.doubleValue ();
- break;
-
- default:
- acc = 0;
- break;
- }
-
- s = new Double (acc).toString ();
- showacc ();
- equals = true;
- last = 0;
- op = 0;
- }
-
- /**
- * Clear the display and the internal last value.
- */
- void Clear () {
- last = 0;
- op = 0;
- s = "0";
- equals = false;
- showacc ();
- }
-
- /**
- * Demand that the display be repainted.
- */
- private void showacc () {
- readout.setText(s);
- repaint ();
- }
-
- }
-
-
-
-
-
-
-
-
-
- Checkbox1.java:
-
- import java.awt.*;
-
- import java.applet.*;
-
- public class Checkbox1 extends Applet
- {
-
- void displayButton_Clicked(java.awt.event.[ccc]
- ActionEvent event)
- {
-
- // clear the results area
-
- results.setText("");
-
- // display the gender
-
- Checkbox current = genderGroup.getSelectedCheckbox();
-
- results.append(current.getLabel() + "\r\n");
-
-
- // check each of the sports
-
- if (artCheckbox.getState() == true)
-
- results.append("Art\r\n");
-
- if (psychologyCheckbox.getState() == true)
-
- results.append("Psychology\r\n");
-
- if (historyCheckbox.getState() == true)
-
- results.append("History\r\n");
-
- if (musicCheckbox.getState() == true)
-
- results.append("Music\r\n");
-
- if (scienceCheckbox.getState() == true)
-
- results.append("Science\r\n");
-
- }
-
-
- public void init() {
-
- // Call parents init method.
-
- super.init();
-
-
- // This code is automatically generated by Visual Cafe
-
- // when you add components to the visual environment.
-
- // It instantiates and initializes the components. To
-
- // modify the code, use only code syntax that matches
-
- // what Visual Cafe can generate, or Visual Cafe may
-
- // be unable to back parse your Java file into its
-
- // visual environment.
-
- //{{INIT_CONTROLS
-
- setLayout(null);
-
- resize(442,354);
-
- label1 = new java.awt.Label("Gender:",Label.RIGHT);
-
- label1.setBounds(72,24,84,28);
-
- add(label1);
-
- genderGroup = new CheckboxGroup();
-
- maleButton = new java.awt.Checkbox("Male",genderGroup,
-
- true);
-
- maleButton.setBounds(168,24,58,21);
-
- add(maleButton);
-
- femaleButton = new java.awt.Checkbox("Female",
-
- genderGroup, false);
-
- femaleButton.setBounds(240,24,71,21);
-
- add(femaleButton);
-
- historyCheckbox = new java.awt.Checkbox("History");
-
- historyCheckbox.setBounds(36,96,60,21);
-
- add(historyCheckbox);
-
- psychologyCheckbox = new java.awt.Checkbox("Psychology");
-
- psychologyCheckbox.setBounds(101,96,85,21);
-
- add(psychologyCheckbox);
-
- artCheckbox = new java.awt.Checkbox("Art");
-
- artCheckbox.setBounds(190,96,40,21);
-
- add(artCheckbox);
-
- musicCheckbox = new java.awt.Checkbox("Music");
-
- musicCheckbox.setBounds(238,96,56,21);
-
- add(musicCheckbox);
-
- scienceCheckbox = new java.awt.Checkbox("Science");
-
- scienceCheckbox.setBounds(298,96,93,21);
-
- add(scienceCheckbox);
-
- displayButton = new java.awt.Button("Display");
-
- displayButton.setBounds(48,204,100,33);
-
- add(displayButton);
-
- results = new java.awt.TextArea();
-
- results.setBounds(180,144,246,198);
-
- add(results);
-
- Action lAction = new Action();
-
- displayButton.addActionListener(lAction);
-
- }
-
-
- java.awt.Label label1;
-
- java.awt.Checkbox maleButton;
-
- CheckboxGroup genderGroup;
-
- java.awt.Checkbox femaleButton;
-
- java.awt.Label label2;
-
- java.awt.Checkbox historyCheckbox;
-
- java.awt.Checkbox psychologyCheckbox;
-
- java.awt.Checkbox artCheckbox;
-
- java.awt.Checkbox musicCheckbox;
-
- java.awt.Checkbox scienceCheckbox;
-
- java.awt.Button displayButton;
-
- java.awt.TextArea results;
-
-
-
- class Action implements java.awt.event.ActionListener {
-
- public void actionPerformed(java.awt.event.ActionEvent
-
- event) {
-
- Object object = event.getSource();
-
- if (object == displayButton)
-
- displayButton_Clicked(event);
-
- }
-
- }
-
- }
-
-
-
-
-
-
-
- Doodle.java:
-
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
-
- /**
- * The ColorBar class displays a color bar for color selection.
- */
- class ColorBar {
-
- /*
- * the top, left coordinate of the color bar
- */
- int xpos, ypos;
-
- /*
- * the width and height of the color bar
- */
- int width, height;
-
- /*
- * the current color selection index into the colors array
- */
- int selectedColor = 3;
-
- /*
- * the array of colors available for selection
- */
- static Color colors[] = {
- Color.white, Color.gray, Color.red, Color.pink,
- Color.orange, Color.yellow, Color.green, Color.magenta,
- Color.cyan, Color.blue
- };
-
- /**
- * Create the color bar
- */
- public ColorBar (int x, int y, int w, int h) {
-
- xpos = x;
- ypos = y;
- width = w;
- height = h;
- }
-
- /**
- * Paint the color bar
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- int x, y, i;
- int w, h;
-
- for (i=0; i<colors.length; i+=1) {
- w = width;
- h = height/colors.length;
- x = xpos;
- y = ypos + (i * h);
- g.setColor (Color.black);
- g.fillRect (x, y, w, h);
- if (i == selectedColor) {
- x += 5;
- y += 5;
- w -= 10;
- h -= 10;
- } else {
- x += 1;
- y += 1;
- w -= 2;
- h -= 2;
- }
- g.setColor (colors[i]);
- g.fillRect (x, y, w, h);
- }
- }
-
- /**
- * Check to see whether the mouse is inside a palette box.
- * If it is, set selectedColor and return true;
- * otherwise, return false.
- * @param x, y - x and y position of mouse
- */
- boolean inside (int x, int y) {
-
- int i, h;
-
- if (x < xpos || x > xpos+width) return false;
- if (y < ypos || y > ypos+height) return false;
-
- h = height/colors.length;
- for (i=0; i<colors.length; i+=1) {
- if (y < ((i+1)*h+ypos)) {
- selectedColor = i;
- return true;
- }
- }
- return false;
- }
- }
-
- /**
- * The Doodle applet implements a drawable surface
- * with a limited choice of colors to draw with.
- */
- public class Doodle extends Frame {
-
- /*
- * the maximum number of points that can be
- * saved in the xpoints, ypoints, and color arrays
- */
- static final int MaxPoints = 1000;
-
- /*
- * arrays to hold the points where the user draws
- */
- int xpoints[] = new int[MaxPoints];
- int ypoints[] = new int[MaxPoints];
-
- /*
- * the color of each point
- */
- int color[] = new int[MaxPoints];
-
- /*
- * used to keep track of the previous mouse
- * click to avoid filling arrays with the
- * same point
- */
- int lastx;
- int lasty;
-
- /*
- * the number of points in the arrays
- */
- int npoints = 0;
- ColorBar colorBar;
- boolean inColorBar;
-
- /**
- * Set the window title and create the menus
- * Create an instance of ColorBar
- */
-
- public void New_Action(ActionEvent event)
- {
- Doodle doodle = new Doodle();
- }
-
- public void Quit_Action(ActionEvent event)
- {
- System.exit(0);
- }
-
- class ActionListener1 implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- String str = event.getActionCommand();
- if (str.equals("New"))
- New_Action(event);
- else if (str.equals("Quit"))
- Quit_Action(event);
- }
- }
-
- public Doodle () {
-
- setTitle ("Doodle");
-
- setBackground(Color.white);
-
- MenuBar menuBar = new MenuBar();
- Menu fileMenu = new Menu("File");
- fileMenu.add(new MenuItem("Open"));
- fileMenu.add(new MenuItem("New"));
- fileMenu.add(new MenuItem("Close"));
- fileMenu.add(new MenuItem("-"));
- fileMenu.add(new MenuItem("Quit"));
- menuBar.add (fileMenu);
-
- Menu editMenu = new Menu("Edit");
- editMenu.add(new MenuItem("Undo"));
- editMenu.add(new MenuItem("Cut"));
- editMenu.add(new MenuItem("Copy"));
- editMenu.add(new MenuItem("Paste"));
- editMenu.add(new MenuItem("-"));
- editMenu.add(new MenuItem("Clear"));
- menuBar.add (editMenu);
- setMenuBar (menuBar);
-
- colorBar = new ColorBar (10, 75, 30, 200);
-
- WindowListener1 lWindow = new WindowListener1();
- addWindowListener(lWindow);
-
- MouseListener1 lMouse = new MouseListener1();
- addMouseListener(lMouse);
-
- MouseMotionListener1 lMouseMotion = new MouseMotionListener1();
- addMouseMotionListener(lMouseMotion);
-
- ActionListener1 lAction = new ActionListener1();
- fileMenu.addActionListener(lAction);
-
- setSize (410, 430);
- show ();
- }
-
- class WindowListener1 extends WindowAdapter
- {
- public void windowClosing(WindowEvent event)
- {
- Window win = event.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }
- }
-
- class MouseMotionListener1 extends MouseMotionAdapter
- {
- public void mouseDragged(MouseEvent e)
- {
- int x = e.getX();
- int y = e.getY();
- if (inColorBar) return;
- if ((x != lastx || y != lasty) && npoints < MaxPoints) {
- lastx = x;
- lasty = y;
- xpoints[npoints] = x;
- ypoints[npoints] = y;
- color[npoints] = colorBar.selectedColor;
- npoints += 1;
- repaint ();
- }
- }
- }
-
- class MouseListener1 extends MouseAdapter
- {
- public void mousePressed(MouseEvent e)
- {
- int x = e.getX();
- int y = e.getY();
- if (colorBar.inside (x, y)) {
- inColorBar = true;
- repaint ();
- return;
- }
- inColorBar = false;
- if (npoints < MaxPoints) {
- lastx = x;
- lasty = y;
- xpoints[npoints] = x;
- ypoints[npoints] = y;
- color[npoints] = colorBar.selectedColor;
- npoints += 1;
- }
- npoints = 0;
- }
- }
-
- /**
- * Redisplay the drawing space
- * @param g - destination graphics object
- */
- public void update (Graphics g) {
-
- int i;
-
- for (i=0; i<npoints; i+=1) {
- g.setColor (colorBar.colors[color[i]]);
- g.fillOval (xpoints[i]-5, ypoints[i]-5, 10, 10);
- }
- colorBar.paint (g);
- }
-
- /**
- * Repaint the drawing space when required
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- update (g);
- }
-
- /**
- * The main method allows this class to be run as an application
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- Doodle doodle = new Doodle ();
- }
-
- }
-
-
-
-
-
-
-
-
-
- Converter.java:
-
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
-
- /*
- * the applet class
- */
- public class Converter extends Applet {
-
- /*
- * the "from" unit index
- */
- int fromindex = 0;
-
- /*
- * the "to" unit index
- */
- int toindex = 0;
-
- /*
- * a place to print the conversion factor
- */
- TextField textfield = new TextField(12);
-
- /*
- * where the choice lists are displayed
- */
- Panel listpanel = new Panel();
-
- /*
- * where the text field is displayed
- */
- Panel textpanel = new Panel();
- Choice unit1 = new Choice();
- Choice unit2 = new Choice();
-
- /*
- * an array of conversion factors
- */
- String values[][] = {
- {"1.000", "1.000 E-2", "1.000 E-5", "3.397 E-1", "3.937 E-2", "6.214 E-6"},
- {"1.000 E+2", "1.000", "1.000 E-3", "39.37","3.28", "6.214 E-4"},
- {"1.000 E+5", "1.000 E+3", "1.000", "3.937 E+4","3.281 E+3", "6.214 E-1"},
- {"2.54", "0.0254", "2.54 E-5", "1.000", "12.0","1.578 E-5"},
- {"30.48", "0.3048", "3.048 E-4", "12.0", "1.000","1.894 E-4"},
- {"1.609 E+5", "1.609 E+3", "1609", "6.336 E+4","5280", "1.000"}
- };
-
- /*
- * called when the applet is loaded
- * create the user interface
- */
- public void init() {
-
- textfield.setText(values[fromindex][toindex]);
- textfield.setEditable (false);
-
- this.setLayout(new BorderLayout());
- listpanel.setLayout(new FlowLayout());
- add("North", listpanel);
- add("South", textpanel);
-
- Label fromlabel = new Label ("To Convert From ",1);
- listpanel.add(fromlabel);
- unit1.addItem("Centimeters");
- unit1.addItem("Meters");
- unit1.addItem("Kilometers");
- unit1.addItem("Inches");
- unit1.addItem("Feet");
- unit1.addItem("Miles");
- listpanel.add(unit1);
-
- Label tolabel = new Label (" to ",1);
- listpanel.add(tolabel);
- unit2.addItem("Centimeters");
- unit2.addItem("Meters");
- unit2.addItem("Kilometers");
- unit2.addItem("Inches");
- unit2.addItem("Feet");
- unit2.addItem("Miles");
- listpanel.add(unit2);
-
- Label multlabel = new Label ("Multiply by ",1);
- textpanel.add(multlabel);
- textpanel.add(textfield);
-
- ItemListener1 lItem = new ItemListener1();
- unit1.addItemListener(lItem);
- unit2.addItemListener(lItem);
-
- }
-
- /**
- * called when an action event occurs
- * @param evt - the event object
- * @param arg - the target object
- */
-
- class ItemListener1 implements ItemListener
- {
- public void itemStateChanged( ItemEvent e)
- {
- fromindex = unit1.getSelectedIndex();
- toindex = unit2.getSelectedIndex();
- textfield.setText(values[fromindex][toindex]);
- repaint();
- }
- }
-
- /**
- * application entry point
- * @param args - commandline arguments
- */
-
- public static void main(String args[]) {
-
- Frame f = new Frame("Converter ");
- Converter converter = new Converter();
- converter.init();
- converter.start();
- f.addWindowListener(new WindowCloser());
-
- f.add("Center", converter);
- f.setSize(500, 100);
- f.show();
- }
- }
-
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }
- }
-
-
-
-
-
-
-
-
- SpreadSheet.java:
-
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
- import javax.swing.*;
- import javax.swing.text.*;
-
- /*
- * the applet class
- */
- public class SpreadSheet extends Applet implements KeyListener{
-
- /*
- * the text entry field
- */
- TextField textField;
-
- /*
- * instance of the sheet panel
- */
- Sheet sheet;
-
- /*
- * initialize the applet
- */
- public void init () {
-
- setBackground(Color.lightGray);
- setLayout(new BorderLayout());
-
- textField = new TextField ("", 80);
- sheet = new Sheet (10, 5, 400, 200, textField);
- add ("North", textField);
- add ("Center", sheet);
- textField.addKeyListener(this);
- }
-
- /*
- * check for return keypresses
- */
- public void keyTyped(KeyEvent e){}
- public void keyReleased(KeyEvent e){}
- public void keyPressed(KeyEvent e)
- {
- char character;
- int x;
-
-
- character = e.getKeyChar();
- x=e.getKeyCode();
-
- //Check whether the user pressed Return
- if (x == 10)
- {
- sheet.enter (textField.getText ());
- }
- }
- /*
- * application entry point
- * not used when run as an applet
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("SpreadSheet");
- SpreadSheet spreadSheet = new SpreadSheet ();
-
- spreadSheet.init ();
-
- f.addWindowListener(new WindowCloser());
- f.setSize (440, 330);
- f.add ("Center", spreadSheet);
- f.show ();
- }
- }
-
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }
- }
-
-
-
-
-
-
-
-
- Sheet.java:
-
- import java.awt.*;
- import java.awt.event.*;
-
- /*
- * the panel that holds the cells
- */
- public class Sheet extends Panel implements MouseListener
- {
-
- /*
- * the number of rows and columns in the spreadsheet
- */
- int rows;
- int cols;
-
- /*
- * width and height of the panel
- */
- int width;
- int height;
-
- /*
- * the array of cells
- */
- Cell cells[][];
-
- /*
- * offsets into the panel where the cells are displayed
- */
- int xoffset;
- int yoffset;
-
- /*
- * the row and column selected by a mouse click
- */
- int selectedRow = 0;
- int selectedCol = 0;
-
- /*
- * the text entry field
- */
- TextField textField;
-
- /*
- * constructor
- * create all the cells and init them
- * @param r - number of rows
- * @param c - number of columns
- * @param w - width of the panel
- * @param h - height of the panel
- * @param t - instance of text entry field
- */
- public Sheet (int r, int c, int w, int h, TextField t) {
-
- int i, j;
-
- rows = r;
- cols = c;
- width = w;
- height = h;
- xoffset = 30;
- yoffset = 30;
- textField = t;
-
- cells = new Cell[rows][cols];
- for (i=0; i<rows; i+=1) {
- for (j=0; j<cols; j+=1) {
- cells[i][j] = new Cell (cells, rows, cols);
- }
- }
- addMouseListener(this);
-
- }
-
- /*
- * a mapping array for converting column indexes to characters
- */
- static String charMap[] = {
- "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
- "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
- "u", "v", "w", "x", "y", "z"
- };
-
- /*
- * paint each cell
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int i, j;
- int x, y;
- int w, h;
- double val;
- String s;
-
- w = width / cols;
- h = height / rows;
-
- x = 0;
- g.setColor (Color.black);
- for (i=0; i<rows; i+=1) {
- y = (i * height / rows) + yoffset;
- g.drawString (String.valueOf (i+1), x, y+h);
- }
- y = yoffset-2;
- for (j=0; j<cols; j+=1) {
- x = (j * width / cols) + xoffset+(w/2);
- g.drawString (charMap[j], x, y);
- }
- for (i=0; i<rows; i+=1) {
- for (j=0; j<cols; j+=1) {
- s = cells[i][j].evalToString ();
- x = (j * width / cols) + xoffset + 2;
- y = (i * height / rows) + yoffset - 2;
- if (i == selectedRow && j == selectedCol) {
- g.setColor (Color.yellow);
- g.fillRect (x, y, w-1, h-1);
- } else {
- g.setColor (Color.white);
- g.fillRect (x, y, w-1, h-1);
- }
- g.setColor (Color.black);
- g.drawString (s, x, y+h);
- }
- }
- }
-
- /*
- * called to recalculate the entire spreadsheet
- */
- void recalculate () {
-
- int i, j;
-
- for (i=0; i<rows; i+=1) {
- for (j=0; j<cols; j+=1) {
- cells[i][j].evaluate ();
- }
- }
- }
-
- /*
- * handle mouse down events
- * @param evt - event object
- * @param x - mouse x position
- * @param y - mouse y position
- */
-
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mouseReleased(MouseEvent e){}
- public void mousePressed(MouseEvent e)
- {
- int x = e.getX();
- int y = e.getY();
-
- int w = width / cols;
- int h = height / rows;
-
- int sr = (y-yoffset)/h;
- int sc = (x-xoffset)/w;
-
- if (sr < 0 || sr >= rows || sc < 0 || sc > cols)
- return;
-
-
- selectedRow = sr;
- selectedCol = sc;
- repaint ();
- textField.setText (cells[selectedRow][selectedCol].text);
- }
-
-
- /*
- * called to enter a text into a selected cell
- * @param s - the string to enter
- */
- void enter (String s) {
-
- cells[selectedRow][selectedCol].enter (s);
- recalculate ();
- repaint ();
- }
- }
-
-
-
-
-
-
- Cell.java:
-
- import java.awt.*;
-
- /*
- * Defines an individual cell
- */
- public class Cell {
-
- /*
- * Token types returned by Lex()
- */
- static final int NUMBER = 1;
- static final int EQUALS = 2;
- static final int PLUS = 3;
- static final int MINUS = 4;
- static final int STAR = 5;
- static final int SLASH = 6;
- static final int TEXT = 7;
- static final int EOT = 8;
- static final int LEFT = 9;
- static final int RIGHT = 10;
- static final int UNKN = 11;
- static final int FORMULA = 12;
- static final int REFERENCE = 13;
-
- /*
- * What is in this cell
- */
- int type;
-
- /*
- * The numeric value, if this cell is numeric
- */
- double value;
-
- /*
- * Numeric value for this token
- */
- double lexValue;
-
- /*
- * Index into input string (used for parsing)
- */
- int lexIndex;
-
- /*
- * Token value returned from Lex()
- */
- int token;
-
- /*
- * The text contents of this cell
- */
- String text;
- int textLength;
- int textIndex;
-
- /*
- * Reference to all cells in spreadsheet
- */
- Cell cells[][];
-
- /*
- * Error flag, set if parse error detected
- */
- boolean error;
-
- /*
- * Used to force rereading of tokens
- */
- boolean lexFlag;
-
- /*
- * Number of rows and columns in the spreadsheet
- * Used for bounds checking
- */
- int cols;
- int rows;
-
- public Cell (Cell cs[][], int r, int c) {
-
- cells = cs;
- rows = r;
- cols = c;
- text = "";
- lexFlag = true;
- type = UNKN;
- }
-
- /*
- * Called to get the numeric value of this cell
- */
- double evaluate () {
-
- resetLex ();
- error = false;
- switch (type) {
- case FORMULA:
- Lex ();
- value = Level1 ();
- if (error) return 0;
- return value;
-
- case NUMBER:
- value = lexValue;
- return value;
-
- case UNKN:
- return 0;
- }
- error = true;
- return 0;
- }
-
- /*
- * Returns the string representation of the value
- */
- String evalToString () {
-
- String s;
-
- if (type == TEXT || type == UNKN) return text;
- s = String.valueOf (value);
- if (error) return "Error";
- else return s;
- }
-
- /*
- * Called to enter a string into this cell
- */
- void enter (String s) {
-
- text = s;
- textLength = text.length ();
- resetLex ();
- error = false;
- switch (Lex ()) {
- case EQUALS:
- type = FORMULA;
- break;
-
- case NUMBER:
- type = NUMBER;
- value = lexValue;
- break;
-
- default:
- type = TEXT;
- break;
- }
- }
-
- /*
- * Top level of the recursive descent parser
- * Handle plus and minus.
- */
- double Level1 () {
-
- boolean ok;
- double x1, x2;
-
- x1 = Level2 ();
- if (error) return 0;
-
- ok = true;
- while (ok) switch (Lex ()) {
- case PLUS:
- x2 = Level2 ();
- if (error) return 0;
- x1 += x2;
- break;
-
- case MINUS:
- x2 = Level2 ();
- if (error) return 0;
- x1 -= x2;
- break;
-
- default:
- Unlex ();
- ok = false;
- break;
- }
- return x1;
- }
-
- /*
- * Handle multiply and divide.
- */
- double Level2 () {
-
- boolean ok;
- double x1, x2;
-
- x1 = Level3 ();
- if (error) return 0;
-
- ok = true;
- while (ok) switch (Lex ()) {
- case STAR:
- x2 = Level3 ();
- if (error) return 0;
- x1 *= x2;
- break;
-
- case SLASH:
- x2 = Level3 ();
- if (error) return 0;
- x1 /= x2;
- break;
-
- default:
- Unlex ();
- ok = false;
- break;
- }
- return x1;
- }
-
- /*
- Handle unary minus, parentheses, constants, and cell references
- */
- double Level3 () {
-
- double x1, x2;
-
- switch (Lex ()) {
- case MINUS:
- x2 = Level1 ();
- if (error) return 0;
- return -x2;
-
- case LEFT:
- x2 = Level1 ();
- if (error) return 0;
- if (Lex () != RIGHT) {
- error = true;
- return 0;
- }
- return x2;
-
- case NUMBER:
- case REFERENCE:
- return lexValue;
- }
- error = true;
- return 0;
- }
-
- /*
- * Reset the lexical analyzer.
- */
- void resetLex () {
-
- lexIndex = 0;
- lexFlag = true;
- }
-
- /*
- * Push a token back for rereading.
- */
- void Unlex () {
-
- lexFlag = false;
- }
-
- /*
- * Returns the next token
- */
- int Lex () {
-
- if (lexFlag) {
- token = lowlevelLex ();
- }
- lexFlag = true;
- return token;
- }
-
- /*
- * Returns the next token in the text string
- */
- int lowlevelLex () {
-
- char c;
- String s;
-
- do {
- if (lexIndex >= textLength) return EOT;
- c = text.charAt (lexIndex++);
- } while (c == ' ');
- switch (c) {
- case '=':
- return EQUALS;
-
- case '+':
- return PLUS;
-
- case '-':
- return MINUS;
-
- case '*':
- return STAR;
-
- case '/':
- return SLASH;
-
- case '(':
- return LEFT;
-
- case ')':
- return RIGHT;
- }
-
- if (c >= '0' && c <= '9') {
- s = "";
- while ((c >= '0' && c <= '9') || c == '.' ||
- c == '-' || c == 'e' || c == 'E') {
- s += c;
- if (lexIndex >= textLength) break;
- c = text.charAt (lexIndex++);
- }
- lexIndex -= 1;
- try {
- lexValue = Double.valueOf (s).doubleValue();
- } catch (NumberFormatException e) {
- System.out.println (e);
- error = true;
- return UNKN;
- }
- return NUMBER;
- }
- if (c >= 'a' && c <= 'z') {
- int col = c - 'a';
- int row;
- s = "";
- if (lexIndex >= textLength) {
- error = true;
- return UNKN;
- }
- c = text.charAt (lexIndex++);
- while (c >= '0' && c <= '9') {
- s += c;
- if (lexIndex >= textLength) break;
- c = text.charAt (lexIndex++);
- }
- lexIndex -= 1;
- try {
- row = Integer.valueOf (s).intValue() - 1;
- } catch (NumberFormatException e) {
- error = true;
- return UNKN;
- }
- if (row >= rows || col >= cols) {
- error = true;
- return REFERENCE;
- }
- lexValue = cells[row][col].evaluate();
- if (cells[row][col].error) error = true;
- return REFERENCE;
- }
- return TEXT;
- }
- }
-
-
-
-
-
-
-
-
- ColorPicker.java:
-
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
-
-
- /*
- * the Applet class
- */
- public class ColorPicker extends Applet implements AdjustmentListener
-
- {
-
- /*
- * the panel that holds the sliders that
- * control the RGB values
- */
- Panel controls = new Panel();
-
- /*
- * the panel that holds the sample color
- */
- Panel sample = new Panel();
-
- public void adjustmentValueChanged(AdjustmentEvent evt)
- {
- Object object1 = evt.getSource();
-
- if (object1 == (sbRed))
- {
- tfRed.setText(String.valueOf(sbRed.getValue()));
- changecolor();
- }
-
- if (object1 == (sbGreen))
- {
- tfGreen.setText(String.valueOf(sbGreen.getValue()));
- changecolor();
- }
-
- if (object1 == (sbBlue))
- {
- tfBlue.setText(String.valueOf(sbBlue.getValue()));
- changecolor();
- }
- }
-
- /*
- *the red scrollbar
- */
- Scrollbar sbRed;
-
- /*
- * the green scrollbar
- */
- Scrollbar sbGreen;
-
-
- /*
- * the blue scrollbar
- */
- Scrollbar sbBlue;
-
- /*
- * the red component TextField
- */
- TextField tfRed;
-
- /*
- * the green component TextField
- */
- TextField tfGreen;
-
- /*
- * the blue component TextField
- */
- TextField tfBlue;
- int min = 0;
- int max = 255;
-
- /*
- * initilaizes the applet
- */
- public void init () {
-
- this.setLayout(new BorderLayout());
- controls.setLayout (new GridLayout(3,3,5,5));
- this.add ("South",controls);
- this.add ("Center",sample);
-
- tfRed = new TextField (5);
- tfGreen = new TextField (5);
- tfBlue = new TextField (5);
- controls.add (new Label ("Red",1));
- controls.add (new Label ("Green",1));
- controls.add (new Label ("Blue",1));
- controls.add (tfRed);
- controls.add (tfGreen);
- controls.add (tfBlue);
-
- sbRed = new Scrollbar (Scrollbar.HORIZONTAL,0, 1, min, max);
- //set the values for the scrollbar
- // value, page size, min, max
- controls.add (sbRed);
- sbRed.addAdjustmentListener(this);
-
- sbGreen = new Scrollbar (Scrollbar.HORIZONTAL,0, 1, min, max);
- //set the values for the scrollbar
- // value, page size, min, max
- controls.add (sbGreen);
- sbGreen.addAdjustmentListener(this);
-
- sbBlue = new Scrollbar (Scrollbar.HORIZONTAL,0, 1, min, max);
- //set the values for the scrollbar
- // value, page size, min, max
- controls.add (sbBlue);
- sbBlue.addAdjustmentListener(this);
-
- // sets the text fields to the slider value
- tfRed.setText(String.valueOf(sbRed.getValue()));
- tfGreen.setText(String.valueOf(sbGreen.getValue()));
- tfBlue.setText(String.valueOf(sbBlue.getValue()));
-
- changecolor();
- }
-
- /** Gets the current value in the text field.
- * That's guaranteed to be the same as the value
- * in the scroller (subject to rounding, of course).
- * @param textField - the textField
- */
- double getValue(TextField textField) {
-
- double f;
- try {
- f = Double.valueOf(textField.getText())
- σdoubleValue();
- } catch (java.lang.NumberFormatException e) {
- f = 0.0;
- }
- return f;
- }
-
-
- /*
- * changes the color of the sample to the
- * color defined by the RGB values set by
- * the user
- */
- public void changecolor () {
-
- int i;
-
- sample.setBackground(new Color((int)getValue(tfRed),
- (int)getValue(tfGreen), (int)getValue(tfBlue)));
- repaint();
- }
-
- public void update (Graphics g) {
-
- paintAll(g);
- }
-
- /*
- * application entry point
- * not used when run as an applet
- * @param args - command line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("ColorPicker");
- ColorPicker colorPicker = new ColorPicker ();
-
- colorPicker.init ();
- f.addWindowListener(new WindowCloser());
-
- f.setSize (300, 200);
- f.add ("Center", colorPicker);
- f.show ();
- }
- }
-
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- Window win = e.getWindow();
- win.setVisible(false);
- win.dispose();
- System.exit(0);
- }
- }
-
-
-
-
-
-
-
-
-
-