In this lab, we are going to inherit some classes that we didn't create, this should allow us to understand the concept of OOP and how it's very useful in creating many abstraction levels, we will use AWT & Swing libraries as an example.
Note: We won't use AWT or Swing in the next labs, and we won't cover it in the course material.
Swing is a GUI library for Java, so it will allow you to create GUI applications in Java, note that it's built on top of AWT (Abstract Window Toolkit), notice that later in this course, we will learn to use JavaFX which is a new GUI library for Java.
To create a frame (window), we will use javax.swing.JFrame
class, then we will configure this window and show it.
Configurations:
6
, we changed the default operation, so when we close the window the whole application will exit.7
, we changed the size of this window.8
, we set the visibility to true.
Figure 1: JFrame
You could add a component to the frame using the method frame.add
, which will take any element of type java.awt.Component
, for example, we could add javax.swing.JTextField
.
Notice that we changed the layout of the frame to prevent it from stretching the elements.
Figure 2: JTextField
There is a lot of components that you could use, take a look at this docs.
You could extend the functionality of any component by simply inheriting them, for example, we could create a JTextField that only accept numbers!
This could be achieved easily by overriding processKeyEvent
method, and checking if this event is a character typing, and if it's a digit:
So this text field, will only accept numbers, notice that we've created our new component that could be added to the frame without understanding the whole library!
In this section, we are going to talk about the AWT Graphics API find the docs here, the graphics API will allow you to draw stuff, and it's used by components to draw their shape.
The JPanel component is just a lightweight container that has a double buffer, we could extend this component and override its paintComponent
method.
This will give us access to the graphics API, which we could use to draw anything!
Figure 3: Extending JPanel
Please take a look a the docs to see graphics class methods.
We could override the `` method to move the rect when there is an event!
Congratulations you have a moving box!
We could now create a class name GameElement
, which has a draw
method, and interact
method, these two methods could be overridden, so if we want to create our moving box, we could extend the GameElement
class.
So you could extend this class in many classes, have all your elements as an array, and paint them all together!
In this task, you should create a simple painting app, to enable mouse events, you could call enableEvents
method in your constructor:
then override processMouseEvent
method, finally, you could store the graphics and draw over it, or store every mouse click position to draw it later!
Bouns: Make the right click change the color randomly!
Note: Please read the Java Docs!
Programming
Java
IUG