2. The coordinate system used by a container to
position components within it is analogous to the
screen coordinate system.
The origin is at the top-left corner of the container,
with the positive x-axis running horizontally from
left to right, and the positive y-axis running from
top to bottom.
The positions of buttons in a JWindow or a JFrame
object are specified as a pair of (x, y) pixel
coordinates, relative to the origin at the top-left
corner of the container object on the screen.
The Coordinate System
3.
4. Cont..
The content pane will have its own coordinate
system, too, which will be used to position the
components that it contains.
Every component has its own coordinate system,
which will be used to position the component that it
contains.
5. Cont..
You also need a coordinate system to draw on a
component—to draw a line, for example, you need to
be able to specify where it begins and ends in relation
to the component
6. Drawing on a Component
Device-independent logical coordinate system is
called the user coordinate system or user space.
By default, this coordinate system has the same
orientation as the system for positioning components
in containers.
The origin is at the top-left corner; the positive x-axis
runs
from left to right, and the positive y-axis from top to
bottom.
Coordinates are usually specified as floating- point
values, although you can also use integers.
7. Cont..
A particular graphical output device will have its own
device coordinate system
This coordinate system has the same orientation as
the default user coordinate system, but the coordinate
units depend on the characteristics of the device.
8.
9. Cont..
With the default mapping from user coordinates to
device coordinates, the units for user coordinates are
assumed to be 1/72 of an inch. Since for most screen
devices the pixels are approximately 1/72 inch apart,
the conversion amounts to an identity
transformation.
10. Graphics Contexts
The user coordinate system for drawing on a
component using Java 2D is encapsulated in an object
of type Graphics2D, which is usually referred to as a
graphics context. It provides all the tools you need
to draw whatever you want on the surface of the
component. A graphics context enables you to draw
lines, curves, shapes, filled shapes, as well as images,
and gives you a great deal of control over the drawing
process.
11. Cont..
The information required for converting user
coordinates to device coordinates is encapsulated in
three different kinds of objects:
❑ A GraphicsEnvironment object encapsulates all
the graphics devices (as GraphicsDevice objects) and
fonts (as Font objects) that are available on your
computer.
❑ A GraphicsDevice object encapsulates information
about a particular device, such as a screen or a
printer, and stores it in one or more
GraphicsConfiguration objects.
❑ A GraphicsConfiguration object defines the
characteristics of a particular device, such as a screen
or a printer.
12. Cont..
We can draw on a component by implementing the
paint() method that is called whenever the
component needs to be reconstructed.
The another way of drawing on a component is by
obtaining a graphics context for a component at any
time just by calling its getGraphics() method and then
using methods for the Graphics object to specify the
drawing operations.
If you don’t want to call paint() method directly in
some occasion , you have to use repaint() method.
13. An Example,
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g;
// Get a Java 2D device context
g2D.setPaint(Color.RED); // Draw in red
g2D.draw3DRect(50, 50, 150, 100, true);
// Draw a raised 3D rectangle
g2D.drawString(“A nice 3D rectangle”, 60,
100); // Draw some text
}
14.
15. The Drawing Process
A Graphics2D object maintains a whole heap of
information that determines how things are drawn.
Most of this information is contained in six
attributes within a Graphics2D object:
1. Paint
2. Stroke
3. Font
4. Transform
5. Clip
6. Composite
17. Shapes
Classes that define geometric shapes are contained in
the java.awt.geom package, but the Shape interface
that these classes implement is defined in java.awt.
To draw a shape on a component, you just need to
pass the object defining the shape to the draw()
method for the Graphics2D object for the component.
18. Classes Defining Points
Two classes in the java.awt.geom package define
points, Point2D.Float and Point2D.Double.
19. Cont..
The operations that each of the three concrete point
classes inherits are:
Accessing coordinate values
Calculating the distance between two points
20. Cont..
OR
Comparing points—The equals() method compares
the current point with the point object referenced by
the argument and returns true if they are equal and
false otherwise.
21. Cont..
Setting a new location for a point : setLocation()
method is used to set the location of the point.
25. Create a Rectangle
getx() and gety() are used to retrieve the coordinates
of rectangle.
Getheight() and getwidth() returns the height and
width.
You can set the position, width, and height of a
rectangle by calling its setRect() method.
33. Filling Objects
Once you know how to create and draw a shape,
filling it is easy. You just call the fill() method for the
Graphics2D object and pass a reference of type Shape
to it.
This works for any shape but for sensible results the
boundary should be closed.
The way the enclosed region will be filled is
determined by the window rule in effect for the
shape.
34. Ex., Filling Star
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g;
Star star = new Star(0,0); // Create a star
float delta = 60; // Increment between stars
float starty = 0; // Starting y position
// Draw 3 rows of 4 stars
for(int yCount = 0 ; yCount<3; yCount++) {
starty += delta; // Increment row position
float startx = 0; // Start x position in a row
// Draw a row of 4 stars
for(int xCount = 0 ; xCount<4; xCount++) {
g2D.setPaint(Color.BLUE); // Drawing color blue
g2D.draw(star.atLocation(startx += delta, starty));
g2D.setPaint(Color.GREEN); // Color for fill is green
g2D.fill(star.getShape()); // Fill the star
}
}
}
41. Using Dialogs
A dialog is a window that is displayed within the
context of another window—its parent.
The JDialog class in the javax.swing package defines
dialogs, and a JDialog object is a specialized sort of
Window.
A JDialog object will typically contain one or more
components for displaying information or allowing
data to be entered, plus buttons for selection of dialog
options (including closing the dialog) together.
42. Modal and Non-Modal Dialogs
There are two different kinds of dialog that you can
create, and they have distinct operating
characteristics.
You have a choice of creating either a modal dialog
or a non-modal dialog.
When you display a modal dialog—typically by
selecting a menu item or clicking a button—it inhibits
the operation of any other windows in the application
until you close the dialog.
43. Cont..
A non-modal dialog can be left on the screen for as long
as you want, since it doesn’t block interaction with
other windows in the application.
You can also switch the focus back and forth between
using a non-modal dialog and using any other
application windows that are on the screen.
46. Instant Dialogs
The JOptionPane class in the javax.swing package
defines a number of static methods that will create
and display standard modal dialogs for you.
The following static methods in the JOptionPane class
produce message dialogs:
You can refer to various methos of this from our
textbook on page no-1009.
47. Input Dialogs
JOptionPane also has four static methods that you can use to
create standard modal input dialogs:
showInputDialog(Object message)
eg,
String input = JOptionPane.showInputDialog(“Enter
Input:”);