1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.Dimension;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.Graphics2D;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.awt.RenderingHints;
|
---|
8 | import java.awt.Toolkit;
|
---|
9 | import java.awt.event.ComponentEvent;
|
---|
10 | import java.awt.event.ComponentListener;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.awt.event.WindowListener;
|
---|
13 | import java.awt.event.WindowStateListener;
|
---|
14 |
|
---|
15 | import javax.swing.JFrame;
|
---|
16 |
|
---|
17 | import org.expeditee.actions.Actions;
|
---|
18 | import org.expeditee.actions.Misc;
|
---|
19 | import org.expeditee.io.Logger;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * The Main GUI class, comprises what people will see on the screen.<br>
|
---|
23 | * Note: Each Object (Item) is responsible for drawing itself on the screen.<br>
|
---|
24 | * Note2: The Frame is registered as a MouseListener and KeyListener, and
|
---|
25 | * processes any Events.
|
---|
26 | *
|
---|
27 | * @author jdm18
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class Browser extends JFrame implements ComponentListener,
|
---|
31 | WindowListener, WindowStateListener {
|
---|
32 | /**
|
---|
33 | * Default version - just to stop eclipse from complaining about it.
|
---|
34 | */
|
---|
35 | private static final long serialVersionUID = 1L;
|
---|
36 |
|
---|
37 | // private static final JScrollPane scrollPane = new JScrollPane();
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a new Browser object, then launches it
|
---|
41 | *
|
---|
42 | * @param args
|
---|
43 | */
|
---|
44 | public static void main(String[] args) {
|
---|
45 | Browser b = new Browser();
|
---|
46 | // Why do we want to ignore repaint?
|
---|
47 | // b.setIgnoreRepaint(true);
|
---|
48 | b.requestFocus();
|
---|
49 | // FrameGraphics.ForceRepaint();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void setSizes(Dimension size) {
|
---|
53 | setSize(size);
|
---|
54 | setPreferredSize(size);
|
---|
55 | addWindowListener(this);
|
---|
56 | addWindowStateListener(this);
|
---|
57 |
|
---|
58 | // center the frame on the screen
|
---|
59 | Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
60 | double xpos = screen.getWidth() / 2;
|
---|
61 | double ypos = screen.getHeight() / 2;
|
---|
62 | setLocation((int) (xpos - (size.getWidth() / 2)), (int) (ypos - (size
|
---|
63 | .getHeight() / 2)));
|
---|
64 |
|
---|
65 | FrameGraphics.setMaxSize(size);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public Browser() {
|
---|
69 | // Use the default values initially so we can load the profile frame
|
---|
70 | setSizes(new Dimension(UserSettings.InitialWidth,
|
---|
71 | UserSettings.InitialHeight));
|
---|
72 |
|
---|
73 | UserSettings.Init();
|
---|
74 | UserSettings.Username = System.getProperty("user.name");
|
---|
75 | Frame profile = FrameIO.LoadProfile(UserSettings.Username);
|
---|
76 | if (profile == null) {
|
---|
77 | profile = FrameIO.CreateNewProfile(UserSettings.Username);
|
---|
78 | }
|
---|
79 | FrameUtils.ParseProfile(profile);
|
---|
80 |
|
---|
81 | // Now reset the size of the windows to the size specified in the users
|
---|
82 | // profile
|
---|
83 | setSizes(new Dimension(UserSettings.InitialWidth,
|
---|
84 | UserSettings.InitialHeight));
|
---|
85 |
|
---|
86 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
87 |
|
---|
88 | addComponentListener(this);
|
---|
89 | pack();
|
---|
90 | try {
|
---|
91 | go();
|
---|
92 | } catch (Exception e) {
|
---|
93 | e.printStackTrace();
|
---|
94 | Logger.Log(e);
|
---|
95 | }
|
---|
96 | // FrameGraphics.ForceRepaint();
|
---|
97 | }
|
---|
98 |
|
---|
99 | Graphics2D g;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Loads in the Menus and the first Frame.
|
---|
103 | */
|
---|
104 | public void go() {
|
---|
105 |
|
---|
106 | Actions.Init();
|
---|
107 | DisplayIO.Init(this);
|
---|
108 | // Set visible must be just after DisplayIO.Init for the message box to
|
---|
109 | // be the right size
|
---|
110 | setVisible(true);
|
---|
111 |
|
---|
112 | g = (Graphics2D) getGraphics();
|
---|
113 | assert g != null;
|
---|
114 | g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
|
---|
115 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
---|
116 | g.setFont(g.getFont().deriveFont(40f));
|
---|
117 |
|
---|
118 | FrameGraphics.setDisplayGraphics((Graphics2D) getGraphics());
|
---|
119 |
|
---|
120 | // required to accept TAB key
|
---|
121 | setFocusTraversalKeysEnabled(false);
|
---|
122 |
|
---|
123 | assert (UserSettings.FirstFrame != null);
|
---|
124 | Frame firstFrame = FrameIO.LoadFrame(UserSettings.FirstFrame);
|
---|
125 |
|
---|
126 | DisplayIO.setCurrentFrame(firstFrame);
|
---|
127 |
|
---|
128 | // Create the action handler for the
|
---|
129 | FrameMouseActions mouse = new FrameMouseActions();
|
---|
130 |
|
---|
131 | this.addMouseListener(mouse);
|
---|
132 | this.addMouseMotionListener(mouse);
|
---|
133 | this.addKeyListener(new FrameKeyboardActions());
|
---|
134 | this.addMouseWheelListener(mouse);
|
---|
135 |
|
---|
136 | // Rob: Why is all this being done????
|
---|
137 | g.dispose();
|
---|
138 | g = null;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * public void loadedMenu(String menu) { if (g == null) return;
|
---|
143 | *
|
---|
144 | * g.setColor(this.getBackground()); g.fillRect(0, 0, this.getWidth(),
|
---|
145 | * this.getHeight()); g.setColor(Color.BLACK); g.drawString("Loading Menu... " +
|
---|
146 | * menu, this.getWidth() / 2 - 200, this .getHeight() / 2); }
|
---|
147 | */
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void paint(Graphics g) {
|
---|
151 | super.paint(g);
|
---|
152 | FrameGraphics.Repaint();
|
---|
153 | //System.out.print('p');
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * @inheritDoc
|
---|
158 | */
|
---|
159 | public void componentResized(ComponentEvent e) {
|
---|
160 | FrameGraphics.setMaxSize(this.getSize());
|
---|
161 | // FrameGraphics.Repaint();
|
---|
162 | repaint();
|
---|
163 | //System.out.println("Resize");
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * @inheritDoc
|
---|
168 | */
|
---|
169 | public void componentMoved(ComponentEvent e) {
|
---|
170 | // FrameGraphics.setMaxSize(this.getSize());
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * @inheritDoc
|
---|
175 | */
|
---|
176 | public void componentShown(ComponentEvent e) {
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * @inheritDoc
|
---|
181 | */
|
---|
182 | public void componentHidden(ComponentEvent e) {
|
---|
183 | }
|
---|
184 |
|
---|
185 | public int getTitleBarHeight() {
|
---|
186 | Rectangle contentbounds = getContentPane().getBounds();
|
---|
187 | contentbounds = this.getGlassPane().getBounds();
|
---|
188 | // contentbounds = this.getRootPane().getBounds();
|
---|
189 | int titleheight = getSize().height - contentbounds.height;
|
---|
190 |
|
---|
191 | return titleheight;
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void windowClosing(WindowEvent e) {
|
---|
195 | Misc.Exit();
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void windowClosed(WindowEvent e) {
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void windowOpened(WindowEvent e) {
|
---|
202 | }
|
---|
203 |
|
---|
204 | public void windowIconified(WindowEvent e) {
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void windowDeiconified(WindowEvent e) {
|
---|
208 | }
|
---|
209 |
|
---|
210 | public void windowActivated(WindowEvent e) {
|
---|
211 | //System.out.println("Activated");
|
---|
212 | }
|
---|
213 |
|
---|
214 | public void windowDeactivated(WindowEvent e) {
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * public void windowGainedFocus(WindowEvent e) { FrameGraphics.Repaint(); }
|
---|
219 | *
|
---|
220 | * public void windowLostFocus(WindowEvent e) { //FrameGraphics.Repaint(); }
|
---|
221 | */
|
---|
222 | public void windowStateChanged(WindowEvent e) {
|
---|
223 | //FrameGraphics.Repaint();
|
---|
224 | //System.out.println('C');
|
---|
225 | }
|
---|
226 |
|
---|
227 | public int getDrawingAreaX() {
|
---|
228 | // return scrollPane.getLocationOnScreen().x;
|
---|
229 | return this.getLocationOnScreen().x;
|
---|
230 | }
|
---|
231 |
|
---|
232 | public int getDrawingAreaY() {
|
---|
233 | // return scrollPane.getLocationOnScreen().y;
|
---|
234 | return this.getLocationOnScreen().y;
|
---|
235 | }
|
---|
236 | }
|
---|