[go: up one dir, main page]

0% found this document useful (0 votes)
28 views12 pages

AWT Graphics Swing

Uploaded by

Aryan yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

AWT Graphics Swing

Uploaded by

Aryan yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java Graphics

java.awt.Graphics class provides many methods for graphics programming.

Commonly used methods of Graphics class:

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 (Abstract Window Toolkit) is an API to develop GUI or window-based


applications in java.

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:

i. By Instantiating Frame class

ii. By extending Frame class

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 Handling in Java

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.

Delegation Event Model


We know about Source, Listener, and Event. Now let’s look at the model which joins these 3
entities and make them work in sync. The delegation event model is used to accomplish the
task. It consists of 2 components Source and listener. As soon as the source generates an
event it is noticed by the listener and it handles the event at hand. For this action to happen
the component or the source should be registered with the listener so that it can be notified
when an event occurs.
//*************************************************************************

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.

Pros of using Adapter classes:


o It assists the unrelated classes to work combined.

o It provides ways to use classes in different ways.

o It increases the transparency of classes.

o It provides a way to include related patterns in the class.

o It provides a pluggable kit for developing an application.

o It increases the reusability of the class.

The adapter classes are found in java.awt.event,


java.awt.dnd and javax.swing.event packages. The Adapter classes with their corresponding
listener interfaces are given below.
java.awt.event Adapter classes

Do yourself-What is jar file? Advantages of jar file?

JAR File - Write Steps

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

Note-After class name press 2 or 3 enters.

2) Now create GUI file (a.java)


import javax.swing.*;

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

You might also like