AWT Graphics Swing
AWT Graphics Swing
1. public abstract void drawString(“hello”, int x, int y): is used to draw the specified
string.
2. public void drawRect(int x, int y, int width, int height): draws a rectangle with the
specified width and height.
3. public abstract void fillRect(int x, int y, int width, int height): is used to fill
rectangle with the default color and specified width and height.
4. public abstract void drawOval(int x, int y, int width, int height): is used to draw
oval with the specified width and height.
5. public abstract void fillOval(int x, int y, int width, int height): is used to fill oval
with the default color and specified width and height.
6. public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line
between the points(x1, y1) and (x2, y2).
7. public abstract boolean drawImage(Image img, int x, int y, ImageObserver
observer): is used draw the specified image.
8. public abstract void drawArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used draw a circular or elliptical arc .
9. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used to fill a circular or elliptical arc.
10. public abstract void setColor(Color c): is used to set the graphics current color to
the specified color.
11. public abstract void setFont(Font font): is used to set the graphics current font to
the specified font.
//program
**************************************************************************
Java AWT
Java AWT components are platform-dependent i.e. components are displayed according to
the view of operating system. AWT is heavyweight i.e. its components are using the
resources of OS.
The java.awt.*; package provides classes for AWT API such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
Java AWT Hierarchy
Container
The Container is a component in AWT that can contain other components like buttons, text
fields, labels etc. The classes that extend Container class are known as container such as
window, Frame, Dialog and Panel.
AWT Components
1. Containers
Container in Java AWT is a component that is used to hold other components such as text fields,
buttons, etc. It is a subclass of java.awt.Component and is responsible for keeping a track of
components being added. There are four types of containers provided by AWT in Java.
Types of Containers
1. Window: It is an instance of the Window class having neither border nor title. It is used for
creating a top-level window.
2. Frame: Frame is a subclass of Window and contains title, border and menu bars. It comes
with a resizing canvas and is the most widely used container for developing AWT
applications. It is capable of holding various components such as buttons, text fields,
scrollbars, etc. You can create a Java AWT Frame in two ways:
3. Dialog: Dialog class is also a subclass of Window and comes with the border as well as the
title. Dialog class’s instance always needs an associated Frame class instance to exist.
4. Panel: Panel is the concrete subclass of container and doesn’t contain any title bar, menu
bar or border. Panel class is a generic container for holding the GUI components. You need
the instance of the Panel class in order to add the components.
Swing in Java
Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building
optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is
built on top of the AWT API and entirely written in java. It is platform independent unlike
AWT and has lightweight components.
Example
import javax.swing.*;
public class swingsButton
{
public static void main(String args[])
{
JFrame f = new JFrame("Swings Application");
JButton b = new JButton("click me");
b.setBounds(40,90,85,20);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}}
Event
When you press a button in your program or Android application the state of the button
changes from ‘Unclicked’ to ‘Clicked’. This change in the state of our button is called an
Event. Events are generated based on how you interact with the GUI. For example- entering
some text through the keyboard, moving your cursor, scrolling, etc. generates events.
Source
In Java, nearly everything is an object. The button you press is an object too. Source is the
object which generates an event. In other words, a source is an object which undergoes state
change. It also provides information about the event to the listener. We will talk about the
listener in the other half of this post.
Listeners
Listeners are also called as event handlers as they are the ones responsible to handle events
occurring at the source. Listeners are interfaces and different types of listeners are used
according to the event.
Adapter class
Java adapter classes provide the default implementation of listener interfaces. If you inherit
the adapter class, you will not be forced to provide the implementation of all the methods of
listener interfaces. So it saves code.
The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An
executable jar file calls the main method of the class if you double click it.
To create the executable jar file, you need to create .mf file, also known as manifest file.
1) First create manifest File
class a
{
public a()
{
JOptionPane.showMessageDialog(null,"hello how r you?");
}
public static void main(String args[])
{
a o=new a();
}
}
3) Compile and run the java file
4) C:/> jar cfm a.jar mymanifest.txt a.class
5) Now you can run jar file directly