L1_Graphics_programming___Inner_classes
L1_Graphics_programming___Inner_classes
Java has extensive support for graphics programming. In the next few lectures
we will write Java applications which manipulate windows or frames, text, fonts,
colours, shapes etc.
We will start with creating a closable frame, i.e. atop-level window that is not
inside another window:
import javax.swing.*;
class myFrame1 extends JFrame{
public myFrame1(){
setTitle(“My First Frame");
setSize(300,200);
}
public static void main(String[] a){
JFrame frame=new myFrame1();
frame.show();
}
}
Java includes a set of classes for dealing with graphics; these classes are
stored in a package called Swing. The first line of this program imports
this package.
We then create class myFrame1 which extends the Java JFrame class
which is a Swing class. We use the methods setTitle and setSize
from JFrame class to give the frame a title and specify the size. The output
of this simple program is the following window:
A frame is a container object which can hold other user interface elements
such as buttons, menus, text fields, etc. To show a frame you need to
follow the three steps:
There is one problem with this little program; you will find it hard to end the
program. To properly close this graphics program, add the following event
handler to the class:
To change how your frames look like, you can use inherited methods from
the JFrame and its parent classes. The most common layout methods are
the following:
- setIconImage method to set an icon for your frames
- setTitle method to set some text in the title bar
- setResizable method determine if a frame will be resizable by the user
- setLocation method to set the position for your frame
This next example will create a frame with an area one-forth that of the
whole screen and is positioned in the middle of the screen. For this
example, we need to obtain the screen resolution:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class myFrame2 extends JFrame{
public myFrame2(){
setTitle("Centered Frame");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(sWidth/2,sHeight/2);
setLocation(sWidth/4,sHeight/4);
Image img=tk.getImage("icon.gif");//window icon
setIconImage(img);
}
public static void main(String[] a){
JFrame frame=new myFrame2();
frame.setVisible(true);
}
}
Java uses the Toolkit class to access system-dependent information.
We also need to interact with the operating system to obtain an image stored in
the file-system using the Toolkit class method getImage:
Image img=tk.getImage(“icon.gif”);
You could specify long paths, e.g. “D:/Pictures/icon.gif”
In this example, we declare three font objects f, f1 and f2. Before drawing the
string, the fonts are applied to the graphics object. You can use any font
available on your machine. But you should be aware that not all machines
have the same fonts installed.
Or you can create a Color object and then specify its color by specifying its
red, green and blue components: Color(byte redness, byte
greenness, byte blueness)
To draw an arc:
drawArc(int x, int y, int width, int height, int
startAngle, int arcAngle);
This will draw an arc bounded by rectangle [x,y,width, height] and with start
angle and span angle. For a circle the start angle would be 0 and the span
angle would be 360.
The last method draws and fills a round rectangle at the same time. You can
also use fillRect, fill3DRect, fillOval ,fillPolygon,
fillArc to draw other filled shapes.
An inner class is a class that is defined inside another class. One of the
main reasons of having inner classes is because they are very convenient
when you are writing event-driven programs in Java. Since we will study
GUI and event-driven programming later on, we need to understand inner
classes in detail.
Inner classes are defined inside other classes. The following example
demonstrates this feature:
Public class Parcel{
class Contents{
private int i=1;
public int value(){return i;}
}
class Destination{
private String label;
Destination(String whereTo){ label=whereTo;}
String readLabel() {return label;}
}
public void ship(String dest){
Contents c=new Contents();
Destination d=new Destination(dest);
System.out.println(d.readLabel());
}
public static void main(String[] a){
Parcel p=new Parcel();
p.ship(“London”);
}
}
This example shows how you can create and use inner classes. In this
example, two inner classes are defined inside the outer class Parcel. In
the method ship(), objects are created of type Contents and Destination.
When you compile this program, 3 class files are created, one for each
class. Inner classes are given the name of OuterClass$InnerClass format.