Before starting this exercise, be sure that Java, Eclipse and all its plugins are properly installed, including having set all the preferences in Eclipse and its plug-ins!
Here, we will build a simple graphical user interface ("GUI") program using Eclipse and its WindowBuilder plugin.
Make sure that before you start, that you have all the necessary Eclipse plug-ins installed and properly configured, i.e. the preferences are properly set for Eclipse and all its plug-ins!
High level development view:
- Make an Eclipse workspace -- typically this only needs to be done once as all one's course work would be in the same workspace.
- Make an Eclipse project in the workspace -- Do this for every assignment and lab
- Make sure that the "src" and "bin" folders are separate! ==> Don't mix source code and byte-code!
- Commit the project to source control.
- Set up the overall MVC (Model-View-Controller) architecture
- Typically, this means creating "model", "view" and "controller" packages for simple systems.
- Create a simple JFrame for the main view component.
- Create a simple Controller class with a main() method to instantiate the system, e.g. the JFrame initially.
- Build the model part of the system and attach it to the view component(s).
- Commit the code often and early!
Note that some of the formal steps to create the MVC architecture outlined above are abbreviated below for simplicity. See the references section below for detail discussions.
Detailed Development Steps:
- Make a Comp310 Labs workspace folder
- In your Comp310 folder (make one if you don't already have one), make a folder called "Labs". We will put all of our lab code into sub-folders in this Labs folder.
- It is highly suggested that you also create another, "Assignments" folder in your Comp310 folder to put your homework code.
- Create an Eclipse workspace -- a workspace is a collection of projects, which is the collection of files needed to make a a specific application (e.g. homework or lab assignment)
- Start Eclipse -- the "Workspace Launcher" dialog should show up. If you have gotten past this point already for some reason, you can get back to it by slecting "File/Switch Workspace/Other..." from the main Eclipse menu.
- Click the "Browse" button and browse to and highlight the Labs folder you made.
- Click the "Ok" button twice and Eclipse will restart in your new workspace.
- If this is the first time you are using the workspace, you will need to click the "Workbench: Go to the workbench" icon in the middle right of the Eclipse screen.
- Adjust the WindowBuilder settings -- this only needs to be done once ever.
- Follow the instructions for setting the WindowBuilder preferences in the Using WindowBuilder resource page.
- Create a new Java Project -- Sets up a specific Java application to be developed.
- Be sure that the upper right corner of the Eclipse window has a little button labeled "Java", indicating that Eclipse is using its default "Java perspective". If not, click the "Open Perspective" icon in the upper right corner and select the Java perspective.
- Click "File/New/Java Project" from the main Eclipse menu.
- Project Name = "Lab00"
- Be sure that the following default values are indeed set:
- "Use default location" selected
- Use execution environment JRE = JavaSE-1.8 (or whatever the latest version is)
- "Create separate folder sfor sources and class files" selected
- "Add project to working sets" NOT selected.
- Click "Finish". A new folder in Labs called "Lab01" with subfolders, "src" and "bin" should be created. The .java source files will go in "src" and the .class files will go in "bin".
- Create a WindowBuilder application - adds a JFrame-based application to the project based on a pre-defined template for a WindowBuilder graphical user interface type (GUI) Java application
- Add a new package under the "src" folder:
- In the "Package Explorer" tab, right-click the "src" folder and select "New/Package"
- Set the "Name" of the new package to "view" and click "Finish".
- Note: Normally one would also create the "model" and "controller" packages for one's MVC architecture at this point but we will forego that for now for simplicity's sake. In the end, we will need to migrate the code to a normal MVC architecture.
- Be sure that the new "view" package is highlighted and follow the instructions in the Using WindowBuilder resource page to "Create a Simple GUI Application"
- Run your working, albeit not too exciting application!
- Highlight the JFrame file in the package explorer that you created (probably "MainAppFrame.java") and click the green "Run" button on the main Eclipse toolbar.
- If you haven't already saved all your files a "Save and Launch" dialog will pop up asking you to save any unsaved files. Click "Select All" and check the box that says "Always save resources before launching" and never be bothered by this again.
- A small blank window will appear. Hooray!
- Add some GUI components to make your application more interesting
- Open the WindowBuilder "What-You-See-Is-What-You-Get" (WSIWYG) GUI editor:
- Double-click the JFrame file (probably "MainAppFrame.java") to open it in the editor if it is not already open.
- Click on the "Design" tab at the bottom of the editor pane. This will open WindowBuilder's
- Add a panel to the frame:
- In the WindowBuilder component palette, click on the "JPanel" component.
- Holding the mouse (without clicking!) over various locations in the frame will highlight each of the 5 possible locations that the panel can be placed onto the frame.
- Click on the top location (the "North" location) to drop the panel into the frame.
- Change the name of the variable referencing the new panel by selecting the panel in the frame and going to the WindowBuilder Properties tab. Under the entry called "Variable", change the value to "pnlControl" (we are making a "control panel").
- Note the naming convention being used here. By prefixing the name with "pnl", one can tell at a glance exactly what sort of component the variable references. We will use prefixes like "pnl", "lbl", "btn", etc. to indicate panels, labels, button and other components. This is incredibly useful. Good naming practices are paramount!
- Be sure that the panel is selected and change the background color of panel by clicking on the ellipses button ("...") next to the "background" entry in the WindowBuilder Properties tab.
- Run your application to see your colored panel on the frame.
- Add a label to the panel:
- In the WindowBuilder component palette, click on the "JLabel" component.
- Select a JLabel from the WindowBuilder component palette and drop it onto the panel you added earlier.
- Change the name of the label to something more useful, e.g. "lblInfo".
- Enter the text for the label in the "text" entry in the label's Properties.
- Run your application to test it.
- Add a button to the panel:
- Select a JButton from the component palette and add it to the panel.
- Change the name of the button to something more useful, e.g. "btnRun".
- Set the button's text property to whatever you like (recommendation: something related to the name of the button).
- Run your application. You should be able to click the button but it won't do anything...yet.
- Add behavior to the button:
- In the WindowBuilder designer, double-click the button.
- WindowBuilder will auto-generate the following code (see note on using lambda functions for ActionListeners when hand-coding):
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
- Add the following line to the actionPerformed() method:
lblInfo.setText("Comp310 rocks!");
- Run your application to test it. Clicking the button should now change the text of the label. Woohoo!
- Go nuts and have fun!
- Experiment around with adding more components and changing their properties.
- Challenge: Add a JTextField to your GUI and have the button change the label's text to whatever you type into the text field.
- If your project is connected to source control, e.g. you're working in a homework project, don't forget to commit (and push, if using Git) your code!
- See the course-specific directions on how to commit your to the remote repository.
Addtional program modifications:
- Moving towards MVC
- Moving the
setVisible()
method
- Move the line "
setVisible(true)
" from main()
to a new method of the frame called "void start()
"
- Questions:
- "What makes this change so significant?"
- Moving the
main()
method to its proper location
- Create a "MainApp" (or otherwise appropriately named) class in the "controller" package to represent your application as a whole. We'll refer to this as the "controller" class.
- In the constructor of the controller class, instantiate the view (frame) class.
- Add a
start()
method to the controller class.
- Call the view's
start()
method in the controller's start() method.
- Questions:
- What is the meaning ("semantic") of the controller's
start()
method?
- Why not have the controller call
view.setVisible(true)
?
- Move the
main()
method to this class.
- Change
main()
to instantiate the controller class instead of the view class.
- See Easy MVC Creation using Anonymous Inner Classes
- Drawing and Animating Shapes
- First, read about The mystery of "repaint" in the Basic Animations in Java page
- Drawing a static shape:
- Follow the directions on the "Painting a Shape" section of the Basic Animations page above to draw a simple oval or other shape on the screen.
- Questions:
- Even though the Java libraries all reference shapes by the location of their upper left-hand corner, from a design perspective, why should you NEVER do this?
- What point (spot) in a shape should define its "location" and why? Relate your answer to the notions of modeling the visual representation an entity.
- Why do you think Java, like many graphics systems, uses the upper left-hand corner? The answer should disturb you!
- How does "encapsulation" protect you from this issue?
- Animating a shape:
- Try this first, just to see how animation works:
- In your
paintComponent()
method, after the shape has been drawn, increment the x
and/or y
component of the shape's location, i.e. "location.x
" and/or "location.y
".
- Make sure your shape initially starts off somewhere away from the edge of the screen to ensure that you can see it initially.
- Only increment the location value by a few pixels or the shape will jump off the screen!
- Run your program and while it is running, force the frame to repaint by simply resizing it with your mouse.
- Next, try to automate the frame repainting using a timer by following the directions in the "Setting up the timer" section of the Basic Animations page above.
- Note that the directions assume that you have your MVC architecture set up already.
- Questions:
- In what part of the system should the timer exist: the view, the controller or the model? Why?
- Likewise, what part of the system should hold the sprites?
References:
- Software Installation:
- See Canvas for the latest official installation instructions!
- General installation instructions:
- Software Usage Instructions:
- See Canvas for the latest official usage instructions!
- General instructions:
- Graphical User Interfaces (GUIs):
- Model-View-Controller ("MVC") Design Pattern
- Inner Classes:
© 2020 by Stephen Wong