Set the system appearance
The following Setsystemlookandfeel () method calls the Setlookandfeel () method of the Javax.swing.UIManager class:
It requires swing to convert from the default metal appearance to a local skin:
private void setSystemLookAndFeel() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException x) {
log(x);
} catch (ClassNotFoundException x) {
log(x);
} catch (IllegalAccessException x) {
log(x);
} catch (InstantiationException x) {
log(x);
}
}
Typically, an exception is not thrown because the Setlookandfeel () parameter has an available value. However, any exception with the standard log API can be logged as a critical error message:
private static void log(Exception x) {
Logger.global.severe(x.getMessage());
}
It is possible to use a global log for a prototype, but a product should be using its own log to save the error message in the file.
Create and display a primary window
The Createframe () method creates a mainframe instance and loads the picture:
private void createFrame() {
mainFrame = new MainFrame();
mainPanel = mainFrame.getMainPanel();
mainPanel.updateSize();
mainFrame.pack();
loadImage();
}
Updatesize () sets the reasonable size of the main panel obtained by Getmainpanel (). The pack () method makes the main frame resize so that the main panel and the application toolbar are resized to fit the size. Note that the Getmainpanel () and Updatesize () methods are the application methods implemented by the mainframe and Mainpanel classes. The pack () method is inherited from the Java.awt.Window.
The Showframe () method displays the main frame of the application and invokes the Requestfocus () method of the main panel. Without calling Requestfocus (), the focus will be obtained by the Indent drop-down box in the toolbar, which is not the main component of the framework. When the application starts, its main component should get the focus, even if the main panel does not handle any keyboard events.
Call Setdefaultcloseoperation () when the window is closed, disable the default action for this method, and pass Do_nothing_on_close as an argument. The Showframe () method registers its own window listener to handle window shutdown events. When the user closes the main frame, the listener saves an annotated picture, releases the resources occupied by the frame, and ends the application execution with System.exit (0).
private void showFrame() {
mainFrame.setDefaultCloseOperation(
MainFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
saveImage();
mainFrame.dispose();
System.exit(0);
}
});
mainFrame.show();
mainPanel.requestFocus();
}