P. 1
Java Graphics

Java Graphics

|Views: 25|Likes:
Published by Shraddha Sheth

More info:

Published by: Shraddha Sheth on Nov 28, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, TXT or read online from Scribd
See more
See less

04/08/2013

text

original

Chapter-19 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

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.

Lines and Rectangles

Drawing a Line
OR

You draw a line using the draw() method for a Graphics2D object

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.

Round Rectangle

Combining Rectangles

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.

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 } } }

Gradient Fill

Cont..

Chapter-20 Extending the GUI
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..

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->