java graphics
TRANSCRIPT
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
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 ComponentDevice-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 systemThis 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 ContextsThe 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 redg2D.draw3DRect(50, 50, 150, 100, true);
// Draw a raised 3D rectangleg2D.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. Paint2. Stroke3. Font4. Transform5. Clip6. Composite
ShapesClasses 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 PointsTwo 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 valuesCalculating the distance between two points
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.
Cont..Setting a new location for a point : setLocation()
method is used to set the location of the point.
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.
Filling ObjectsOnce 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.
Ex., Filling Starpublic void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g;Star star = new Star(0,0); // Create a starfloat delta = 60; // Increment between starsfloat starty = 0; // Starting y position// Draw 3 rows of 4 starsfor(int yCount = 0 ; yCount<3; yCount++) {
starty += delta; // Increment row positionfloat startx = 0; // Start x position in a row// Draw a row of 4 starsfor(int xCount = 0 ; xCount<4; xCount++) {
g2D.setPaint(Color.BLUE); // Drawing color blueg2D.draw(star.atLocation(startx += delta, starty));g2D.setPaint(Color.GREEN); // Color for fill is greeng2D.fill(star.getShape()); // Fill the star}
}}
Using DialogsA 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 DialogsThere 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.
Instant DialogsThe 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 iconchoices, // Array of choiceschoices[1]); // Initial choice
Choosing a Custom ColorFacility of Choosing Custom Color is provided by the
javax.swing.JColorChooser class.
Example