/Users/lyon/j4p/src/ip/gui/dialog/DoubleArrayLog.java
|
1 //--------------------------------
2 //File: DoubleArrayLog.java
3 //Date: 03/29/99
4 //Student id: 0424811
5 //Student name: Jitendra Shiralkar
6 //Compiled on: JDK 1.2
7 //--------------------------------
8 //
9 //Class implemented: DoubleArrayLog
10 //Derived from: ArrayDialog
11 //Uses: MessageLog
12 //
13 //Description:
14 // The DoubleArrayLog class accepts only double values for the array elements.
15 //A message Dialog is shown to the user if he/she has made error in input.
16 //
17 package ip.gui.dialog;
18
19 import ip.gui.frames.ConvolutionFrame;
20
21 import java.awt.*;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24
25 public class DoubleArrayLog
26 extends ArrayDialog
27 implements ActionListener {
28
29 public DoubleArrayLog(ConvolutionFrame frame, String title,
30 String defaults[][], int row, int col, int fieldSize) {
31 super(frame, title, defaults, row, col, fieldSize);
32 cf = frame;
33 }
34
35 ConvolutionFrame cf;
36
37 public void printUserInput() {
38 double userInput[][] = getUserInputAsDouble();
39 System.out.println("The values set to the array elements are:");
40 System.out.println("*****************************************");
41 for (int i = 0; i < userInput.length; i++) {
42 for (int j = 0; j < userInput[0].length; j++) {
43 System.out.print("" + userInput[i][j] + " ");
44 }
45 System.out.println();
46 }
47 }
48
49 public void convolve() {
50 float userInput[][] = getUserInputAsFloat();
51 cf.convolve(userInput);
52 }
53
54 public float[][] getUserInputAsFloat() {
55 double ud[][] = getUserInputAsDouble();
56 int w = ud.length;
57 int h = ud[0].length;
58 float uf[][] = new float[w][h];
59 for (int x = 0; x < w; x++)
60 for (int y = 0; y < h; y++)
61 uf[x][y] = (float) ud[x][y];
62 return uf;
63
64 }
65
66 public double[][] getUserInputAsDouble() {
67 String userInput[][] = super.getUserInput(); //calling the super class method getUserInput()
68 double dui[][] = new double[userInput.length][userInput[0].length];
69 int i = 0, j = 0;
70
71 try {
72 for (i = 0; i < userInput.length; i++) {
73 for (j = 0; j < userInput[0].length; j++) {
74 Double d = Double.valueOf(userInput[i][j]);
75 dui[i][j] = d.doubleValue();
76 }
77 }
78 } catch (NumberFormatException e) {
79 StringBuffer message = new StringBuffer();
80 message.append("Could not convert to double\n");
81 message.append("Row: " + (i + 1) + "Column: " + (j + 1) + "\n");
82 message.append("Value: " + e.getMessage());
83 ;
84
85 MessageLog m1 = new MessageLog("Input Error: Could not convert to Double", message.toString());
86 }
87
88 return dui;
89 }
90
91 public void actionPerformed(ActionEvent e) {
92 Button b = (Button) e.getSource();
93 if (b == super.cancelButton) { //if cancel button was pressed, exit
94 setVisible(false);
95 System.exit(0);
96 } else
97 //printUserInput();
98 convolve();
99 }
100
101 }
102
103 class MessageLog extends Dialog implements ActionListener {
104 Button okButton = new Button("OK");
105 Panel buttonPanel = new Panel();
106 Label messageLabel = new Label();
107
108 public MessageLog(String title, String message) {
109 super(new Frame(), title, true); //sets the MessageLog modal
110 okButton.addActionListener(this);
111 messageLabel.setText(message);
112
113 setLayout(new GridLayout(0, 1));
114 add(messageLabel);
115 add(okButton);
116 pack();
117 show();
118 }
119
120 public void actionPerformed(ActionEvent e) {
121 if (e.getSource() == okButton)
122 setVisible(false);
123 }
124 }
125