View RSS Feed

All Blog Entries

  1. Drawing an Image

    by , 03-07-2012 at 05: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 ...
  2. Reading/Loading an Image

    by , 03-07-2012 at 04: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, ...
  3. Working with Images

    by , 03-07-2012 at 04: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
  4. Drawing Multiple Lines of Text

    by , 03-07-2012 at 04: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 ...
  5. Using Text Attributes to Style Text

    by , 03-07-2012 at 04: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. ...
  6. Displaying Antialiased Text by Using Rendering Hints

    by , 03-07-2012 at 04: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 ...
  7. Measuring Text

    by , 03-07-2012 at 04: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
  8. Advantages and Disadvantages of Using Physical and Logical Fonts

    by , 03-07-2012 at 04: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 ...
  9. Logical Fonts

    by , 03-07-2012 at 04: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 ...
  10. Physical Fonts

    by , 03-06-2012 at 08: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();
  11. Fill Patterns

    by , 03-06-2012 at 08: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
  12. Line Styles

    by , 03-06-2012 at 08: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
  13. Stroking and Filling Graphics Primitives

    by , 03-06-2012 at 08: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 ...
  14. Drawing Arbitrary Shapes

    by , 03-06-2012 at 07: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. ...
  15. Drawing an Arc

    by , 03-06-2012 at 07: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
  16. Drawing an Ellipse

    by , 03-06-2012 at 07: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,
  17. Drawing a Rectangle

    by , 03-06-2012 at 07: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 ...
  18. Cubic Curve Segment

    by , 03-06-2012 at 07: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();
  19. Quadratic Curve Segment

    by , 03-06-2012 at 07: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 ...
  20. Drawing a Line

    by , 03-06-2012 at 07:46 PM
    Line2D class is used to present the line segment(x,y) in coordinate space. To specify the lines in double precision & float, use these lines.

    • Line2D.Double
    • Line2D.Float

    Code below explains the drawing of a line.
    Java Code:
    // draw Line2D.Double
    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    Different setline() methods are used by the Line class for explaining end points of line. End point of line will be specified by this given ...
  21. Drawing a Point

    by , 03-06-2012 at 07:45 PM
    Point class creates location presenting x,y coordinate space. Point class consist of these sub classes.

    • Point2D.Float
    • Point2D.Double

    Double precision and float are provided by these given classes to store the point’s coordinates, respectively.
    Java Code:
    //Create Point2D.Double
    Point2D.Double point = new Point2D.Double(x, y);
    For creation of coordinate along with 0,0, default constructor Point 2D.Double can be used.
    Point’s position ...
  22. Drawing Geometric Primitives

    by , 03-06-2012 at 07:43 PM
    Several classes are provided in the Java 2D API that defines common geometric object like:
    • Points
    • Lines
    • Curves
    • Rectangles

    These geometry classes are part of the java.awt.geom package.

    To retrieve methods from the path, the PathIterator interface defines and provides methods. For describing and inspecting geometric path objects, the Shape interface provides a set of methods. The GeneralPath class implements this interface and other geometry classes. ...
  23. Supporting User Interaction

    by , 03-06-2012 at 07:41 PM
    User interaction is supported with the graphics you want to display. You need to be able to determine when the user clicks on one of them. To determine whether a mouse click occurred over a particular Shape object, The Graphics2D class provides a hit method. You can also get the location of the mouse click. Call the contains method on the Shape class to determine whether the click was within the bounds of the Shape.

    You can perform simple hit testing, if you are using primitive text ...
  24. Constructing Complex Shapes from Geometry Primitives

    by , 03-06-2012 at 07:40 PM
    New geometric shapes are created by operations of Boolean at the existing ones. This is what is known as CAG i.e. constructive area geometry. In Java 2D API, shape interface is implemented by the area class. Following Boolean operations are supported by it.

    Name:  1.JPG
Views: 2397
Size:  12.4 KB
    Constructing Complex Shapes from Geometry Primitives

    An intersection at 2 overlapping circles is performed to create the leaves

    Java Code:
    leaf = new Ellipse2D.Double();
  25. Factory Pattern Example

    by , 03-04-2012 at 08:10 PM
    Java Code:
    package factoryPattern2;
    public abstract class Product {
    	private String productName;
    	public void setName(String name) {
    		this.productName = name;
    	public String getName() {
    		return productName;
    	public void putInABox() {
    		System.out.println("Putting the product in a box..");
    package factoryPattern2;
  26. Controlling Rendering Quality

    by , 03-04-2012 at 04:51 PM
    The Graphics2D class rendering are used for specifications like:

    • One wishes rendering objects as soon as possible.
    • Rendering of the higher quality graphics is required.

    Create a RenderingHint object. Pass to setRenderingHints of class Graphics 2D so that to alter or set rendering hint attribute, in context of Graphics2D.

    Graphics 2D’s setRenderingHint method is called so that to set 1 hint and pass it on a value pair key, for that hint which you ...
  27. Clipping the Drawing Region

    by , 03-04-2012 at 04:49 PM
    As a clipping path, shape object is used so that to restrict that drawing areas which will render. Clipping path is considered to be the portion of Graphic2D context. Call the Graphics2D.setClip & pass ahpe so that to set clip’s attributes. Clipping path gets shrink by clip method calling & then passing to other shape. A clip intersects to current shape and specified shape.

    To set clip method pass the ellipse & call clip so that for setting the clip path for intersection ...
  28. Transforming Shapes, Text, and Images

    by , 03-04-2012 at 04:48 PM
    One can do the modification of the graphic attributes so that for performing these graphic primitives in graphic 2D context.
    • Move
    • Scale
    • Rotate
    • Shear

    Transform attribute is defined by the instance of class AffineTransform. Parallel lines are found to be parallel no matter if the transformation has been done in affine transformation.

    Graphics 2D class provides number of methods to change the transform attribute. Graphic2Dtransform can be changed by calling ...
    Tags: images, shapes, text Add / Edit Tags
  29. Getting Started with Graphics

    by , 03-04-2012 at 04:46 PM
    The Java 2D API is considered to be a complex and powerful API which provides support to graphics. Just smaller feature sets are used by the Java 2D graphics, provided in class

    Graphic class is divided into 2 groups, as following.

    • Draw & fill method: This makes you able to render the basic text shapes & images.
    • Attributes setting method: Affects at filling appearance & drawing.

    The setColor & setFont method ...
    Tags: graphics Add / Edit Tags
  30. Java Printing

    by , 03-04-2012 at 04:44 PM
    Graphics rendered to print are given as following, by making use of Java 2D Printing API.
    • Swing
    • Composite graphics
    • Java 2D graphics
    • Images

    Document composition features are provided by the Java 2D printing API. Operations are facilitated to be performed by it. For example, ordering in which you need pages.

    Pages are rendered by the Rendering to screen and rendering to printer are same things. Printing system controls render the pages. At screen, there ...
Page 17 of 48 FirstFirst ... 7151617181927 ... LastLast