Chapter-19 Drawing in A Window: From: Shraddha Sheth
Chapter-19 Drawing in A Window: From: Shraddha Sheth
Drawing in a Window
From :
Shraddha Sheth
The Coordinate System
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.
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.
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
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.
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.
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.
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.
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.
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.
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
}
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
Rendering Objects
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.
Classes Defining Points
Two classes in the java.awt.geom package
define points, Point2D.Float and
Point2D.Double.
Cont..
The operations that each of the three
concrete point classes inherits are:
Accessing coordinate values
Calculating the distance between two
points
Cont..
OR
OR
You draw a line using the draw() method for a Graphics2D object
Create a Rectangle
From :
Shraddha Sheth
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.
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.
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.
Example
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.
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:”);
Cont..
String input =
JOptionPane.showInputDialog(null, “Enter
Input:”,“Dialog for Input”,
JOptionPane.WARNING_MESSAGE);
Cont..
String[] choices = {“Money”, “Health”,
“Happiness”, “This”, “That”, “The Other”};
String input =
(String)JOptionPane.showInputDialog(null,
“Choose now...”, “The Choice of a Lifetime”,
JOptionPane.QUESTION_MESSAGE,
null, // Use default icon
choices, // Array of choices
choices[1]); // Initial choice
Choosing a Custom Color
Facility of Choosing Custom Color is provided
by the javax.swing.JColorChooser class.
Example
Thank You..