View RSS Feed

Java AWT

Java Abstract Window Toolkit (AWT)

  1. Printing Support in Swing Components

    by , 03-07-2012 at 06:53 PM
    Contents out of view or which are invisible because of scrolling in a scrollable window would not be present in a printout. At printer, they will be placing a dumb effect. This specified problem is usually present with larger components printing like a swing table or text components.

    Multiple lines of text are present in many components that are entirely not visible at the screen. Contents shall be printed in a way that is consistent along with the screen display.

    For ...
  2. Print Service Discovery

    by , 03-07-2012 at 06:51 PM
    Abstract class PrintServiceLookup static methods are invoked by the application so that to locate the print services that possess certain abilities to makes the print requests of an application satisfied. E.g, to print out 2 copies of double sided document first thing that shall be searched by the application is the printers that hold the ability of double sided capability.
    Java Code:
    DocFlavor doc_flavor = DocFlavor.INPUT_STREAM.PDF;
    PrintRequestAttributeSet attr_set =
  3. Attribute Definitions

    by , 03-07-2012 at 06:50 PM
    The javax.print.attribute.standard & javax.print.attribute packages details those attributes of prints that show the print service’s capabilities, requirement of print job or the progress of print job.

    E.g, for using A4 paper format to print 3 copies, you shall construct following attributes set implementing
    the PrintRequestAttributeSet interface:
    Java Code:
    PrintRequestAttributeSet attr_set =
        new HashPrintRequestAttributeSet();
  4. Document Type Specification

    by , 03-07-2012 at 06:41 PM
    Format of the print data is presented by DocFlavor, like PostScript or JPEG. There are 2 main parts of the DocFlavor: A representation class name or a MIME type. MIME shows that what is the format and a document representation class name show the way documents are delivered to output stream/ printer. An attribute set and DocFlavor is used by an application to find printers with certain capabilities that have been specified by attribute set. Such sample code shows the way an array of StreamPrintServiceFactory ...
  5. Working with Print Services and Attributes

    by , 03-07-2012 at 06:39 PM
    The Java™ Print Service (JPS) API makes the extension of the current features of Java 2D printing so that to offer these functionalities:
    • Printers which cater to the needs of application by querying the capabilities of printers are discovered.
    • Attributes are extended by the application included with the JPS API.
    • With service provider Interface, 3rd parties might put their print services which print varying formats, including PDF, SVG and Postscript.

    The Java Print Service ...
  6. Printing a Multiple Page Document

    by , 03-07-2012 at 06:11 PM
    To print number of graphic images, page index is used so that to iterate via pages & print just 1 at every page. E.g, many images are presented in this array:
    Java Code:
    BufferedImage[] images = new BufferedImage[10];
    Then, use the print() method as in the following code fragment:
    public int print(Graphics graphics,
               PageFormat pageFormat, int pageIndex)
               throws PrinterException {
        if (pageIndex < images.length) {
  7. Using Print Setup Dialogs

    by , 03-07-2012 at 06:09 PM
    Typically a user wishes to see the print dialog box and page setup. One can select a printer from the print dialog, pages to be printed may be specified and also number of copies could be set.

    Name:  1.JPG
Views: 728
Size:  26.7 KB
    Print Setup Dialog

    A Print dialog is displayed by an application when a button of print command is pressed by user or he selects an item from print menu. In order to display this dialog

    Call the printdialog method of the PrinterJob ...
  8. Basic Printing Program

    by , 03-07-2012 at 06:07 PM
    Printing consists of following two parts:

    Job Control: Following are part of job control.
    • Creating a print job
    • Associate it with printer
    • Specify no of copies
    • User print dialog interaction

    Page Imaging: It includes drawing content to a page.
    The java.awt.print package includes the classes to create a printer job and most other related classes are part of this package.
    Java Code:
    import java.awt.print.*;
    PrinterJob job = PrinterJob.getPrinterJob();
  9. Writing/Saving an Image

    by , 03-07-2012 at 06:04 PM
    It has been explained to use the javax.imageio package so that to load image to Java 2D™'s internalBufferedImage format from an external image format. Also, the usage of Graphics.drawImage() has been explained to draw images with an optional filtering.

    To an external image format, the BufferedImage object is saved. This image might be the one that was loaded originally by Image I/O class, from external image format. It might also be the one which was constructed by Java 2D or was ...
  10. Creating and Drawing to an Image

    by , 03-07-2012 at 06:03 PM
    The process of loading an image is already known, which is created as well as stored on any network or your system. However, you would also want to make a new pixel data buffered image.

    BufferedImage object can be created manually by making use of 3 constructors of class:
    • new BufferedImage(width, height, type) - creates a BufferedImage for one of the predefined type of image.
    • new BufferedImage(width, type,height, colorModel) - creates a BufferedImage of the predefined
  11. Filtering Images

    by , 03-07-2012 at 06:02 PM
    In addition to copying and scaling images, the Java 2D API also filter an image. Filtering is drawing or producing a new image by applying an algorithm to the pixels of the source image. Image filters can be applied by using the following method:

    Java 2D API filters images along with copy or scaling the images. Filtering means producing a new images by applying algorithm to the source image pixels.
    Java Code:
    void Graphics2D.drawImage(BufferedImage img,
  12. Drawing an Image

    by , 03-07-2012 at 06:00 PM
    Graphics.drawImage method is used to draw the images at a specific location:
    Java Code:
    boolean Graphics.drawImage(Image img,
                               int x, int y,
                               ImageObserver observer);
    Top left position of an image is specified by x,y location. Observer parameters are usually null as they are not used frequently or directly & is also not required for BufferedImage.

    This method is used just when there is a need ...
  13. Reading/Loading an Image

    by , 03-07-2012 at 05:59 PM
    Sampled images formats come into mind when we think about digital images. Like GIF images thare used at web pages or JPEG images that are commonly used in digital photography. Program using them shall 1st convert them into an internal format from an external format.

    Java 2D™ provides support to load such external image format to BufferedImage format. This is done by using Image I/O API that is present in javax.imageio package. Built in support is present in Image I/O for GIF,JPEG,WBMP, ...
  14. Working with Images

    by , 03-07-2012 at 05:57 PM
    Common tasks present while working with images are:
    • To Load an external PNG, GIF & JPEG image format file, to Java 2D™'s internal image representation.
    • Creating Java 2D image directly & also rendering to it.
    • Java 2D image contents drawn on a drawing surface.
    • Java 2D image contents saved to an external PNG, GIF or JPEG image file.

    Two classes shall be learnt so that to work with images:
    • Java.awt.Image class being a superclass present the graphical
  15. Drawing Multiple Lines of Text

    by , 03-07-2012 at 05:56 PM
    LineBreakMeasurer shall be used in case you want your styled text paragraph to get fit in a specific width. Styled text is broken down into lines by this class so that to make them fit in a visual advance. As a TextLayout object every line is returned, that presents the styled and unchangeable character data. This class accesses the layout information.

    The getDescent and getAscent methods of Textlayout make the information to be returned regarding font which positions the component ...
  16. Using Text Attributes to Style Text

    by , 03-07-2012 at 05:54 PM
    Following text attributes needs to be applied by an application:
    • Underline –Underneath text, a line is drawn.
    • Strikethrough –Through the text, a horizontal line gets drawn.
    • Superscript or Subscript –A Letter of a text which slightly appears below or above a line.
    • Kerning – The space adjustments between characters

    Such text attributes might get applied, by using the Java 2D™ TextAttribute class.

    Text attributes are applied by adding them to a Font object. ...
  17. Displaying Antialiased Text by Using Rendering Hints

    by , 03-07-2012 at 05:48 PM
    Rendering hints may affect the Java 2D™ text.

    Text drawing method that is the most important of all is given as following:
    Java Code:
    Graphics.drawString(String s, int x, int y);
    Text antialiasing is a method which is used so that to smooth the text edges on screen. Applications are enabled by the Java 2D API to specify that such method shall be used or not and which algorithm shall be used by using text rendering hint, to the graphics.

    At the edge ...
  18. Measuring Text

    by , 03-07-2012 at 05:46 PM
    For measuring text properly you shall be learning certain important methods and also few mistakes shall be avoided. Font metrics are considered as the measure of the text which has been rendered by the font object like, height of line in font. By using FontMetrics instance, you can measure text easily and this is one of the most common way to do so. E.g,
    Java Code:
    // get metrics from the graphics
    FontMetrics metrics = graphics.getFontMetrics(font);
    // get the height of a line
  19. Advantages and Disadvantages of Using Physical and Logical Fonts

    by , 03-07-2012 at 05:45 PM
    An application is enable by the physical fonts to get great advantage of the all present fonts. This is actually done to accomplish maximum language coverage as well as different text appearances. However, this is a tough task to create those applications which yse these physical fonts.

    To bundle the physical fonts along with application makes you able to create those applications which possess same look and also the great control at those applications. However these bundled fonts ...
  20. Logical Fonts

    by , 03-07-2012 at 05:44 PM
    The following five logical font families are defined in the Java SE.
    • Dialog
    • DialogInput
    • Monospaced
    • Serif
    • SansSerif

    These fonts are available on any Java platform and can be thought of as aliases for some underlying font that has the properties implied by its name.
    • A Serif font is a font similar to Times New Roman, which is commonly used in print.
    • A Sans Serif font is more typical for onscreen use.

    These fonts can be customized for the ...
  21. Physical Fonts

    by , 03-06-2012 at 09:03 PM
    Actual font libraries are the physical fonts which contain glyph data as well as tables so that character sequence and glyph sequences are mapped. Font technology like PostScript Type 1 and True Type are used. Following shall be called to get the available installed font families in your system:
    Java Code:
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String []fontFamilies = ge.getAvailableFontFamilyNames();
  22. Fill Patterns

    by , 03-06-2012 at 09:02 PM
    Paint attributes define fill pattern in Graphic2D rendering context. An object’s instance is created for setting the paint attributes which makes possible the implementation of the paint interface. setPaint method is called along with Graphic2D, as a parameter.

    Paint interface is implemented by using these 3 classes.

    • Color
    • TexturePaint
    • GradientPaint

    Create a GradientPaint by specifying.

    • Beginning position/color
    • Ending position/color
  23. Line Styles

    by , 03-06-2012 at 09:01 PM
    Stroke attributes define the line style in Graphic2D rendering context. BasicStroke object shall be created and passed to Graphic2D setStroke method so that to set the stroke attribute.

    Information in a BasicStroke object is:

    • Line width
    • Join style
    • End cap style
    • Dash style

    Use this information when shape is rendered with draw method.

    The line width is line thickness which is measured perpendicular, to its trajectory. The line width ...
    Tags: line styles Add / Edit Tags
  24. Stroking and Filling Graphics Primitives

    by , 03-06-2012 at 09:00 PM
    In this post, discussion will be done regarding the outlines and color which are applicable to the graphics. Stroking as well as filling will also be discussed.

    • lling – To paint with color gradient or solid color.
    • Stroking – Outline of shape is drawn by applying color attribute, stroke width and line style.

    Change paint attributes & stroke that are given in Graphics2D context. This is done before line styles are rendered or patterns are filled to the geometric ...
  25. Drawing Arbitrary Shapes

    by , 03-06-2012 at 08:56 PM
    This class is used to implement shape interface. It also presents geometric path that is constructed from:
    • Lines
    • Cubic Curves
    • Quadratic

    This class supports 3 constructors which creates GeneralPath. These constructors are supported by class along with default winding rule i.e. WIND_NON_ZERO, specified initial coordinate capacity and winding rule i.e. WIND_NON_ZERO or WIND_EVEN_ODD

    How to determine interior of a path is specified by using the winding rule. ...
  26. Drawing an Arc

    by , 03-06-2012 at 08:54 PM
    A piece of ellipse is drawn by Arc2D. Following present an arc class.
    • A bounding rectangle
    • An angular extent
    • A start angle
    • A closure type

    An ellipse in float & double precision are specified.
    • Arc2D.Double
    • Arc2D.Float

    Arc2D class defines 3 types of arcs:
    • OPEN
    • CHORD
    • PIE

    The parameters and size of the arc are set by applying these different methods:
    • By supplied Dimension2D and Point2D.
    • Directly
  27. Drawing an Ellipse

    by , 03-06-2012 at 08:52 PM
    Ellipse2D class presents the ellipse which is defined by bounded rectangles. An ellipse in float & precision are specified by these subclasses.

    • Ellipse2D.Double
    • Ellipse2D.Float

    Following attributes define ellipse:

    • Location
    • Height
    • Width

    Java Code:
    Creation of an ellipse is explained by this example.
    // draw Ellipse2D.Double
    g2.draw(new Ellipse2D.Double(x, y,
  28. Drawing a Rectangle

    by , 03-06-2012 at 08:51 PM
    RectangularShape class implements the shape interface, in which few more methods are added of its own.
    Such methods are used to find out certain information regarding the size and location of shape so that to find out the rectangle’s center point and to set the shape’s bounds.

    Dimension w,x,h and location x,y defines a rectangle. These given methods specify rectangle in float or double precision.

    1. Rectangle2D.Double
    2. Rectangle2D.Float

    Creation ...
  29. Cubic Curve Segment

    by , 03-06-2012 at 08:49 PM
    CubicCurve2D class used to implement the shape interface. In x,y coordinates space, cubic parametric curve segment is presented by this class. In float & double precision, a cubic curve is specified by using these methods.

    • CubicCurve2D.Double
    • CubicCurve2D.Float

    Curve setting methods are same for the CubicCurve2D and QuadraticCurve class, except 2nd control point.E.g.
    Java Code:
    // create new CubicCurve2D.Double
    CubicCurve2D c = new CubicCurve2D.Double();
  30. Quadratic Curve Segment

    by , 03-06-2012 at 08:48 PM
    QuadCurve2D class is used for the implementation of the Shape interface. In x,y coordinate segment, a quadratic parametric curve segment is represent by this given class. A quadratic class is specified by using these given subclasses, in float & double precision.

    • QuadCurve2D.Double
    • QuadCureve2D.Float

    Curve’s control point & 2D endpoints are specified by using many setCurve methods. To directly define the coordinates, use coordinates of other points and ...
Page 1 of 3 123 LastLast