Refactored out screen centering code to utilities class.

This commit is contained in:
jendave
2011-08-06 15:19:00 +00:00
parent 52f9065e5d
commit f0fac32807
4 changed files with 61 additions and 84 deletions

View File

@@ -1,70 +1,35 @@
package forge; package forge;
import arcane.ui.util.ManaSymbols;
import arcane.util.MultiplexOutputStream;
import com.esotericsoftware.minlog.Log;
import forge.error.ErrorViewer;
import forge.error.ExceptionHandler;
import forge.gui.GuiUtils;
import forge.gui.ListChooser;
import forge.properties.ForgePreferences;
import forge.properties.ForgePreferences.CardSizeType;
import forge.properties.ForgePreferences.StackOffsetType;
import forge.properties.ForgeProps;
import forge.properties.NewConstants;
import forge.properties.NewConstants.LANG.Gui_NewGame.MENU_BAR.MENU;
import forge.properties.NewConstants.LANG.Gui_NewGame.MENU_BAR.OPTIONS;
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.Rectangle; import java.awt.Graphics;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.io.BufferedOutputStream; import java.io.*;
import java.io.File; import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import com.esotericsoftware.minlog.Log;
import arcane.ui.util.ManaSymbols;
import arcane.util.MultiplexOutputStream;
import net.miginfocom.swing.MigLayout;
import forge.error.ErrorViewer;
import forge.error.ExceptionHandler;
import forge.gui.ListChooser;
import forge.properties.ForgePreferences;
import forge.properties.ForgeProps;
import forge.properties.NewConstants;
import forge.properties.ForgePreferences.CardSizeType;
import forge.properties.ForgePreferences.StackOffsetType;
import forge.properties.NewConstants.LANG.Gui_NewGame.MENU_BAR.MENU;
import forge.properties.NewConstants.LANG.Gui_NewGame.MENU_BAR.OPTIONS;
/*CHOPPIC*/
import java.awt.Graphics;
import javax.swing.ImageIcon;
/*CHOPPIC*/ /*CHOPPIC*/
@@ -226,13 +191,8 @@ public class Gui_NewGame extends JFrame implements NewConstants, NewConstants.LA
addListeners(); addListeners();
Dimension screen = getToolkit().getScreenSize(); this.setSize(550, 553);
Rectangle bounds = getBounds(); GuiUtils.centerFrame(this);
bounds.width = 550;
bounds.height = 553;
bounds.x = (screen.width - bounds.width) / 2;
bounds.y = (screen.height - bounds.height) / 2;
setBounds(bounds);
setTitle(ForgeProps.getLocalized(LANG.PROGRAM_NAME)); setTitle(ForgeProps.getLocalized(LANG.PROGRAM_NAME));
setupMenu(); setupMenu();

View File

@@ -1,12 +1,12 @@
package forge; package forge;
import forge.error.ErrorViewer; import forge.error.ErrorViewer;
import forge.gui.GuiUtils;
import forge.quest.QuestFrame; import forge.quest.QuestFrame;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.Border; import javax.swing.border.Border;
import javax.swing.border.TitledBorder; import javax.swing.border.TitledBorder;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@@ -60,6 +60,8 @@ public class Gui_QuestOptions extends JFrame {
setup(); setup();
setupRadioButtonText(); setupRadioButtonText();
this.setSize(420,570);
GuiUtils.centerFrame(this);
setVisible(true); setVisible(true);
} }
@@ -67,10 +69,6 @@ public class Gui_QuestOptions extends JFrame {
//make the text look correct on the screen //make the text look correct on the screen
jTextArea1.setBackground(getBackground()); jTextArea1.setBackground(getBackground());
//center window on the screen
Dimension screen = this.getToolkit().getScreenSize();
setBounds(screen.width / 4, 50, //position
420, 570); //size
//if user closes this window, go back to "New Game" screen //if user closes this window, go back to "New Game" screen
this.addWindowListener(new WindowAdapter() { this.addWindowListener(new WindowAdapter() {

View File

@@ -4,10 +4,7 @@ import forge.properties.ForgeProps;
import forge.properties.NewConstants; import forge.properties.NewConstants;
import javax.swing.*; import javax.swing.*;
import java.awt.Component; import java.awt.*;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
@@ -41,6 +38,7 @@ public class GuiUtils {
public static void addExpandingHorizontalSpace(JPanel panel) { public static void addExpandingHorizontalSpace(JPanel panel) {
panel.add(Box.createHorizontalGlue()); panel.add(Box.createHorizontalGlue());
} }
/** /**
* Adds a Vertical Glue to panel * Adds a Vertical Glue to panel
*/ */
@@ -73,11 +71,13 @@ public class GuiUtils {
public static ImageIcon getIconFromFile(String iconName) { public static ImageIcon getIconFromFile(String iconName) {
File base = ForgeProps.getFile(NewConstants.IMAGE_ICON); File base = ForgeProps.getFile(NewConstants.IMAGE_ICON);
File file = new File(base, iconName); File file = new File(base, iconName);
if (file.exists()) if (file.exists()) {
return new ImageIcon(file.toString()); return new ImageIcon(file.toString());
else }
else {
return null; return null;
} }
}
public static ImageIcon getResizedIcon(ImageIcon icon, int width, int height) { public static ImageIcon getResizedIcon(ImageIcon icon, int width, int height) {
return new ImageIcon(icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH)); return new ImageIcon(icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
@@ -86,4 +86,18 @@ public class GuiUtils {
public static ImageIcon getEmptyIcon(int width, int height) { public static ImageIcon getEmptyIcon(int width, int height) {
return new ImageIcon(new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)); return new ImageIcon(new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB));
} }
/**
* Centers a frame on the screen based on its current size
* @param frame a fully laid-out frame
*/
public static void centerFrame(Frame frame) {
Dimension screen = frame.getToolkit().getScreenSize();
Rectangle bounds = frame.getBounds();
bounds.width = frame.getWidth();
bounds.height = frame.getHeight();
bounds.x = (screen.width - bounds.width) / 2;
bounds.y = (screen.height - bounds.height) / 2;
frame.setBounds(bounds);
}
} }

View File

@@ -3,6 +3,7 @@ package forge.quest;
import forge.AllZone; import forge.AllZone;
import forge.Gui_NewGame; import forge.Gui_NewGame;
import forge.QuestData; import forge.QuestData;
import forge.gui.GuiUtils;
import forge.quest.bazaar.QuestBazaarPanel; import forge.quest.bazaar.QuestBazaarPanel;
import javax.swing.*; import javax.swing.*;
@@ -53,8 +54,12 @@ public class QuestFrame extends JFrame {
this.pack(); this.pack();
this.setVisible(true); this.setVisible(true);
GuiUtils.centerFrame(this);
} }
public void showPane(String paneName){ public void showPane(String paneName){
subPanelMap.get(paneName).refreshState(); subPanelMap.get(paneName).refreshState();
questLayout.show(visiblePanel, paneName); questLayout.show(visiblePanel, paneName);