In this lab, we will learn how to draw graphics and make graphical user interfaces in Java. The tutorials in the lab will be presented by the TA step-by-step. However, you are free to work at your own pace on the lab. The lab assignment questions will not be presented by the TA.
This tutorial will demonstrate how to draw shapes on a panel.
Create a new project called Lab9 and a new class inside the project called GUITutorial with a main method.
A window in a graphical user interface is created by extending the JFrame class. Create a new class called TutorialFrame that extends JFrame. The code should look like this:
In the main method in GUITutorial add the code below to create a frame and display it. Run the code and you should see an empty frame with a title.
Components such as text boxes and buttons are added to a frame using panels. A JPanel is a container that holds components and other panels. A panel is added to a frame by putting it on its ContentPane in one of five areas: north (top), west (left), center, east (right), and south (bottom). We will create a new class called TutorialPanel that has a label (JLabel), a text field (JTextField), two radio buttons (JRadioButton), a check box (JCheckBox), and a combo box (JComboBox). Radio buttons are round and have the property that you can only select one at a time. Check boxes can either be checked or not. Multiple check boxes can be checked at the same time. The code to create this panel is below along with the code in TutorialFrame to add the new panel to the frame. Note that these components will not do anything until we handle events in the next lab.
The complete code files are: TutorialPanel.java, GUITutorial.java, and TutorialFrame.java.
This tutorial demonstrates applets, drawing shapes on the screen, and how to create menus. To start, download the code for DrawShapesApplet.java and put it into the project Lab9. Run the code. This program allows the user to draw shapes by selecting particular colors and shapes from the menu. There are a few pieces of the code to note. Here is what the output looks like:
A menu is organized as a menu bar (JMenuBar) which contains menus (JMenu) each of which has one or more items (JMenuItem). The code in the constructor creates a menu bar and adds two menus to it, each with three items. Note that all menu items share an ActionListener class called MenuListener. We will learn more about how event listeners work in Lab 10.
Shapes are drawn on a panel. There are many draw and fill methods. Each draw method takes a Graphics or Graphics2D object which represents how to draw on the screen. The sample code draws a rectangle using fillRect, a circle using fillOval, and a triangle using fillPolygon. Note that if drawRect was used instead, the rectangle would not be filled.
The asteroids game requires us to draw the ship and some asteroids on a background. We will also add a File menu that consists of three options: High Scores, Reset, and Exit. The main screen will be a filled black panel. It will also have a text bottom on the bottom with two buttons. At this point, we just have the interface, until we learn events the components will not do anything useful. You can either copy over your files from Lab 8 and modify them or use the starter files below. We will add draw routines to our Ship, Bullet, and Asteroid classes that can draw them on the screen. The asteroids are circles where the color and size depends on the type (type 3: Size: (60,60) Color: blue, type 2: Size: (40,40) Color: yellow, type 1: Size: (20,20) Color: green). Start from the sample code or your code as below.
File Name | Description | Differences from Lab 8 |
---|---|---|
Asteroids.java | Main Game Class | Major changes to main method and constructors. Recommend you download sample code instead of using your own from Labs 7/8. |
AsteroidsFrame.java | Game Frame | New file. Download it. |
GameObject.java | GameObject Superclass | No changes from Lab 7/8. Can use your own or download new version. |
Asteroid.java | Asteroid class | No changes from Lab 7/8 except new draw method. Can use your own and copy over draw method or download new version. |
Bullet.java | Bullet class | No changes from Lab 8 except new draw method. Can use your own and copy over draw method or download new version. |
Ship.java | Ship class | No changes from Lab 7/8 except new draw method. Can use your own and copy over draw method or download new version. |
For this lab assignment, each group should submit using Connect the Java files for the questions.