Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
AboutDialog.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 package org.bitstorm.util;
4 
5 import java.awt.AWTEvent;
6 import java.awt.Button;
7 import java.awt.Dialog;
8 import java.awt.Event;
9 import java.awt.FlowLayout;
10 import java.awt.Frame;
11 import java.awt.GridBagConstraints;
12 import java.awt.GridBagLayout;
13 import java.awt.GridLayout;
14 import java.awt.Image;
15 import java.awt.Label;
16 import java.awt.Panel;
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 
21 
22 /**
23  * Shows a constructed About dialog box.
24  * The dialog box can contain an image and several lines of text.
25  *
26  * @author Edwin Martin
27  */
28 public class AboutDialog extends Dialog {
29  private Button okButton;
30 
31  /**
32  * Construct a AboutDialog.
33  * @param parent parent Frame
34  * @param title title of dialog
35  * @param imageName image to show
36  * @param posX x-coordinate
37  * @param posY y-coordinate
38  */
39  public AboutDialog( Frame parent, String title, String[] lines, String imageName, int posX, int posY ) {
40  super( parent, title, false );
41 
42  Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(imageName));
43  ImageComponent ic = new ImageComponent( image );
44  okButton = new Button(" OK ");
45  okButton.addActionListener(
46  new ActionListener() {
47  public void actionPerformed(ActionEvent e) {
48  close();
49  }
50  }
51  );
52 
53  GridBagLayout gridbag = new GridBagLayout();
54  GridBagConstraints contraints = new GridBagConstraints();
55  setLayout(gridbag);
56 
57  Panel buttonPanel = new Panel();
58  buttonPanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
59  buttonPanel.add( okButton );
60 
61  Panel textPanel = new Panel();
62  textPanel.setLayout( new GridLayout( lines.length, 1 ) );
63  for (int i=0; i<lines.length; i++) {
64  textPanel.add(new Label(lines[i]));
65  }
66  contraints.fill = GridBagConstraints.BOTH;
67  contraints.weightx = 1;
68  contraints.weighty = 1;
69  contraints.gridx = GridBagConstraints.REMAINDER;
70  contraints.gridy = 0;
71  contraints.anchor = GridBagConstraints.CENTER;
72  gridbag.setConstraints(ic, contraints);
73  add( ic );
74  contraints.fill = GridBagConstraints.NONE;
75  contraints.weightx = 0;
76  contraints.weighty = 0;
77  contraints.gridy = 1;
78  gridbag.setConstraints(textPanel, contraints);
79  add( textPanel );
80  contraints.gridy = 2;
81  gridbag.setConstraints(buttonPanel, contraints);
82  add( buttonPanel );
83 
84  enableEvents(Event.WINDOW_DESTROY);
85  setResizable( false );
86  setModal( true );
87  pack();
88  setLocation( posX, posY );
89  show();
90  }
91 
92  /**
93  * Close dialog box.
94  */
95  private void close() {
96  this.hide();
97  this.dispose();
98  }
99  /**
100  * Process close window button.
101  * @see java.awt.Component#processEvent(java.awt.AWTEvent)
102  */
103  public void processEvent( AWTEvent e ) {
104  if ( e.getID() == Event.WINDOW_DESTROY )
105  close();
106  }
107 }
AboutDialog(Frame parent, String title, String[] lines, String imageName, int posX, int posY)