Welcome!
Long time no see! These weeks we have continued to learn about JFrame class in Java Program. We know how to put buttons, selective buttons, and even pictures in a program. We also learnt how to change color and how to draw in Java Program. These add spices to our classes. It were a memorable weeks. We have learnt a lot during these weeks. Here are the things we have learnt during these two weeks.
Intro
First, there is an important concept in Java which is inheritance. The class at the top of hierarchies, superclasses, defines instance variables and methods common to all classes in the hierarchy. So subclasses inherits behavior and fields from superclasses.
Later, we learnt about GUI (graphical user interface). It makes the program more gorgeous. It enables user to enter data, display information, set program preferences (like color, fonts and so on)…
java.awt is the original components of java components. It hands off some of the display and behavior of the native windowing system. However, it has some disadvantages. One of them is that it is inconvenient in the look-and-feel of the various windowing systems. It is referred as heavyweight components. On the other hand, java.swing is the second generation of GUI components. It is referred to as lightweight components. Applications could run consistently.
Graphics Class and Paint Method
Graphics class enables users to draw pictures on the computer. It enables user to draw pictures with basic shapes. People could change the color of background and letters easily. The constructor of graphics class is different from any other programs we have written before. It is written like “public class ShellGraphicApp extends JFrame”, as shown below. It extends from JFrame, superclass. It uses the inheritance characteristic.
With the extension from JFrame class, users could draw, make the picture visible, change size, and even change colors. To be mentioned, the JFrame picture is originally invisible. So users need to remember to make the picture visible when utilizing JFrame. “draw…” method is to draw the outlined shape. “fill…” is to draw a solid shape which means that there is color in the shape. Please don’t forget that writing graphics code in paint method. There should be client class. If graphics application without client class, it is blank in the box.
Another thing needs to be mentioned is that graphics application is closely associated with coordinate system. The place where all shapes are drawn based coordinate system. Examples
Draw a line: g.drawLine(xStart, yStart, xEnd, yEnd);
Draw a rectangle: g.drawRect(x, y, width, height);
Next, it comes to colors. Before using color, it should be imported from the Color class from java.awt package. All drawings are done in current color which means that whenever users want to change color of a shape, line ,or filling color, users should change the color first. The default color is black. If users want to set the color in basic colors, it could be written as “g.setColor(Color.BLUE);”. If it is more complicated color, it should be written as “Color name = new Color (100, 211, 38);”. In order to draw a colored rectangle, programmer could write “g.fillRect(x, y, width, height);”. Then the rectangle will be filled with the current color.

Besides, we also learnt to draw ovals.
Draw an oval: g.drawOval(x, y, width, height);

Here is an easier method to move or resize an oval.

It is the same method to draw a circle.

We did a small activity to practice our skills.

Bar Chart
This is a program that we have done for assignments to review things we have learnt. It is related with GUI application. I hope you will like it.






GUI Application
GUI application helps computer become user-friendly and manipulative. As I have mentioned before, GUI extends from JFrame class. JFrame class is a Frame; it is a Window; it is a Container; it is a Component; it is an Object. It means that JFrame inherits all those characteristics. There is an enormous library of methods inherited. Below is the steps used to create GUI application.
The first step is to call the constructor of JFrame superclass: “super(“GUI Intro”);”. Then, get an object reference to the content pane container. “contents = getContentPane( );”. We need add all components into the content pane. By this way, those things would be shown. Next step is to set the layout manager. “contents.setLayout(new FlowLayout( ) ); ”. It is the rule. Layout managers could help arrange the GUI components in the window. During the class, Mr. Pete showed us the result without setting the layout manager. Some texts, or shapes disappeared. Instantiating each component is the next step. For example, “labelName = new JLabel(“I am Eva. ”); ”. Then the user need to add each component to the content pane. “contents.add(labelName);”. The user could set the size of the window, which is “setSize(300, 500); ”. The last step is to make the window visible, which is written as “setVisible(true);”.
In order to practice GUI application, we did a small activity. It is shown below.



We can also add event and event handler. By writing if-else statement, event handler could identify which component fired the event. When we write the actions, we need to use the word “implements”. We played with JButton during the class. The event handler uses the “getSource” method to determine which button was pressed. JTextField provides opportunities for user to type in the box, which increases the reactions between programs and users. JLabel is used to label the textfields. To be more complicated, we learnt radioButton. This method makes the buttons become selective.

private class SCHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Write the code of actions
}
}
To be more familiar with these functions, we wrote a program, simple calculator. It has the same function as calculators which could do the simple calculations.






Overall, we have learnt a lot of stuff during these classes. I am really looking forward to the next week. I hope that I could learn more things related with JFrame. Simple calculator is the first program that I feel I am a real programmer. I believe that practice makes perfect. I could do better in the future class. I believe I will enjoy the classes!