Graphical Components in Java

snottybugbearSoftware and s/w Development

Nov 3, 2013 (3 years and 5 months ago)

60 views

Graphical Components in Java
How graphics programs are structured
JFrames
!Components
Frames and components
One way to open a window which displays an image in Java is to
1.dene a so-called JFrame in the le containing main,
2.dene a component in a separate le that might be called
MyComponent.java and then
3.(back in main) add the component to the JFrame,and make
the JFrame visible.
We will rst study the construction of component classes and then
talk about JFrames.
Component classes
Your component classes must extend existing graphics
classes
In the javax.swing package (one of the most popular packages
for graphics in Java) there is a class of objects called JComponents
which (when added to a frame) display an empty picture.Thus,by
themselves JComponents are not very useful.
We use the JComponent class in the following way:we extend it
to a class of our own making which does contain visible pictorial
elements.Put another way,we make a subclass (with a name we
invent) of the JComponent class containing the features we like.
What's a subclass?The full answer to this will not be given until
later in the course,a few weeks from now.But I'll use the jargon
and see if you can't make some kind of sense of it as we go along.
Extending a class
An existing class A may be extended or subclassed with a new
class (call it B) of your own making by dening it as follows
public class B extends A{
//...all the stuff you want to add
//...to this class definition goes here
}
B is now a subclass of A which means that in addition to its own
elds and methods it inherits those of A.If there is a particular
method of A we don't like then we redene it or overwrite it in B.
JComponent and paintComponent()
When you dene a graphical component class,call it
MyComponent,you must extend the JComponent class.So your
rst lines will look like this
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class MyComponent extends JComponent{
}
The problem now is that your MyComponent class will inherit the
paintComponent method from JComponent,which draws a blank
picture.So you must overwrite this method,i.e.redene it,by
declaring it as a method in your new class with exactly the same
name and input and output types as the original method in
JComponent.
Redening paintComponent()
Here is an example of how that is done,so that instead of nothing
the component will paint a rectangle.
public class MyComponent extends JComponent
{
public void paintComponent(Graphics g){//redefine paintComponent
Graphics2D g2 = (Graphics2D) g;//explained later!
Rectangle r = new Rectangle (10,20,100,200);
g2.fill(r);//this colors the rectangle a solid black
}
}
Now all one needs to do is add this component in the le where a
JFrame has been created.
JFrames
(a kind of Window)
The JFrame le
Here's an example of a main method which opens a JFrame but
adds no picture.
import javax.swing.JFrame;
public class RectangleViewer{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(500,600);
frame.setTitle("My first frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
If you compile and execute this le it will open an empty frame.
Adding your component
Now let's add the black rectangle image to the frame.
public class RectangleViewer{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(500,600);
frame.setTitle("My first frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyComponent mc = new MyComponent();
frame.add(mc);//add it to the JFrame
frame.setVisible(true);
}
}
Why do we need to use JComponent at all?
Since adding a JComponent object to a JFrame would create a
blank picture,why don't we just dene our own picture class and
add it to the JFrame?
Ans.:
1.We can't just add any object we please to a JFrame.The add
method of the JFrame class will only accept an object from
JComponent or one of its subclasses.
2.When a JFrame (with an added JComponent) is opened it
automatically executes the method called paintComponent,
thus automatically drawing the picture we dened.
Component Files II:advanced uses
(read after learning about constructors in ch.3)
More on component classes
Do component classes inherit anything useful from the
JComponent class?
Ans:They inherit dozens (over a hundred) useful methods which
will now work with your own MyComponent objects.To see what
they are look at the list of methods in
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html
This le lists not only the methods of the JComponent class,but
also the methods that it inherits from its own superclasses.
Component classes
(cont.)
Do component classes just redene the paintComponent method or do
they carry out other tasks?This amounts to the question:
Can classes extending the JComponent classes have all the features of
regular classes:elds,constructors,new methods?
Yes.We will look at examples later in this course where such a class has
all these features.
It is especially important to add constructors to a class extending
JComponent if you want to be able to pass data (say a bunch of
rectangles) to the component class at the time you are creating a
component to add to a JFrame.
JFrame frame =new JFrame();
...
MyComponent mc = new MyComponent(r1,r2,r3,7)//*
frame.add(mc);
...
Notice how,in the preceding code fragment (all happening inside a
main method) the starred line
MyComponent mc = new MyComponent(r1,r2,r3,7)//*
makes reference to a constructor of MyComponents that takes four
inputs (let's imagine that they are three rectangles and an int).
That necessarily means that in your MyComponent class you dened (at
least) four elds and a constructor with four arguments in addition to
overwriting the paintComponent method.
public class MyComponent extends JComponent{
private Rectangle r1;
private Rectangle r2;
private Rectangle r3;
private int n;
//constructor
public MyComponent(Rectangle x1,Rectangle x2,
Rectangle x3,int y){
r1=x1;r2=x2;r3=x3;n=y;
}
...
//paintComponent definition here
}
A question
In the problem that asks you to dene two rectangles,print their
intersection and then draw it you are faced with a quandary.Do I
dene the rectangles in the class containing main so I can print
them (with println) and then dene the same rectangles again in
the component class so I can draw them and paint their
intersection?
Ans:
No.You want to dene the rectangles once,in the rst le,
compute their intersection once,also in the rst le,and then pass
the three rectangles to the new component object you add to your
Jframe.That means doing what was outlined above:add elds
and a constructor to your IntersectionComponent class.