// MatrixDialog.java import java.awt.*; import java.awt.event.*; public class MatrixDialog extends Dialog implements ActionListener, KeyListener { private Button close; public MatrixDialog(Frame parent, Matrix matrix) { super(parent, "Product", false); MatrixCanvas matrixCanvas = new MatrixCanvas(matrix); ScrollPane matrixPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED); matrixPane.setBackground(Color.white); matrixPane.add(matrixCanvas); matrixPane.setSize(300, 250); close = new Button("Close"); close.addActionListener(this); close.addKeyListener(this); Panel buttonPanel = new Panel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(close); setLayout(new BorderLayout()); add("Center", matrixCanvas); add("South", buttonPanel); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { close(); } }); validate(); pack(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int screenWidth = d.getSize().width, screenHeight = d.getSize().height - 27, parentWidth = parent.getSize().width, parentHeight = parent.getSize().height, w = this.getSize().width, h = this.getSize().height; Point p = parent.getLocation(), setp = parent.getLocation(); if (p.x < 0) setp.x = (parentWidth + p.x - w) / 2; else if (p.x + parentWidth > screenWidth) setp.x = p.x + (screenWidth - p.x - w) / 2; else setp.x = p.x + (parentWidth - w) / 2; if (p.y < 0) setp.y = (parentHeight + p.y - h) / 2; else if (p.y + parentHeight > screenHeight) setp.y = p.y + (screenHeight - p.y - h) / 2; else setp.y = p.y + (parentHeight - h) / 2; if (setp.x < 0) setp.x = 0; else if (setp.x + w > screenWidth) setp.x = screenWidth - w; if (setp.y < 0) setp.y = 0; else if (p.y + h > screenHeight) setp.y = screenHeight - h; setLocation(setp); setVisible(true); } public void close() { setVisible(false); dispose(); } public void keyPressed(KeyEvent ke) { if ((ke.getKeyText(ke.getKeyCode()).equals("Enter")) || (ke.getKeyText(ke.getKeyCode()).equals("Escape"))) { close(); } } public void keyReleased(KeyEvent ke) { } public void keyTyped(KeyEvent ke) { } public void actionPerformed(ActionEvent ae) { close(); } }