Copyright 2011-09-18 Werner Randelshofer

ch.randelshofer.awt.graphics
Class ConcurrentTranslucentPipe

java.lang.Object
  extended by java.awt.Graphics
      extended by java.awt.Graphics2D
          extended by ch.randelshofer.awt.graphics.Pipe
              extended by ch.randelshofer.awt.graphics.AbstractPipe
                  extended by ch.randelshofer.awt.graphics.ConcurrentTranslucentPipe

public class ConcurrentTranslucentPipe
extends AbstractPipe

Renders non-antialiased, translucent drawing primitives with pixel accuracy. Supports translation on X- and Y-axis by full pixels.

This Graphics2D object only works with a BufferedImage which has the following characteristics:

Usage:

 // Create a raster and a buffered image using the supported characteristics.
 WritableRaster ras = Raster.createWritableRaster(
      new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, gw, gh,
      new int[] {0xff0000, 0xff00, 0xff}),
      new Point(0,0)
      );
 
 BufferedImage bufferedImage = new BufferedImage(
      new DirectColorModel(24, 0xff0000, 0xff00, 0xff),
      ras, false, new Hashtable());
 
 // Create the graphics object
 ConcurrentTranslucentPipe g = new ConcurrentTranslucentPipe(bufferedImage);
 
 // Perform the grapics operations
 g.drawLine(0,0,100,100);
 

Version:
1.9 2006-03-26 Tweaked scanFill methods.
1.8 2006-03-21 Tweaked scanFill methods.
1.7 2006-03-18 Fixed fillPolygon and fill in order to properly fill closed polygons and shapes. Fixed a bug in scanFillHorizontal and scanFillVertical which caused shapes with horizontal edges not being filled properly. Simplified line drawing code.
1.6 2006-03-11 Manually unrolled inner loops of line drawing methods. This does not appear to have a measureable effect though 1.2 2006-03-07 Added little optimizations here and there.
1.1 2006-03-02 Fixed rounding errors in clipping algorithm.
1.0 March 1, 2006 Created.
Author:
Werner Randelshofer

Field Summary
protected  int color
          Current drawing color.
protected  Dispatcher dispatcher
          Dispatcher for Runnable objects.
protected  ch.randelshofer.awt.graphics.Edge[] edges
          Is filled with a list of edges for each scan line.
protected  int oneMinusAlpha
          Precomputed values to speed up alpha compositing.
protected  int premultipliedB
           
protected  int premultipliedG
           
protected  int premultipliedR
           
protected  int previousIn
           
protected  int previousOut
           
 
Fields inherited from class ch.randelshofer.awt.graphics.AbstractPipe
background, flatness, mis, pixels, previousClip, previousTransform, ras, tx, ty, u1, u2
 
Fields inherited from class ch.randelshofer.awt.graphics.Pipe
cx1, cx2, cy1, cy2, gh, gw
 
Constructor Summary
ConcurrentTranslucentPipe(java.awt.image.BufferedImage image, Dispatcher dispatcher)
          Creates a new instance.
 
Method Summary
protected  void buildActiveList(int scan, ch.randelshofer.awt.graphics.Edge active)
           
protected  void buildEdgeListHorizontal(int[] x, int[] y, int cnt)
           
protected  void buildEdgeListVertical(int[] x, int[] y, int cnt)
           
protected  void deleteAfter(ch.randelshofer.awt.graphics.Edge q)
           
 void draw(java.awt.Shape s)
           
 void drawLine(int x1, int y1, int x2, int y2)
           
 void drawOval(int ox, int oy, int owidth, int oheight)
           
 void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
          FIXME - Must use drawLineSegment for all line segments except for the last one in order to prevent pixels at line joints from overlapping!
 void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
          FIXME - Must use drawLineSegment for all line segments except for the last one in order to prevent pixels at line joints from overlapping!
 void drawRect(int x, int y, int width, int height)
           
 void fill(java.awt.Shape s)
           
 void fillOval(int ox, int oy, int owidth, int oheight)
           
 void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
           
 void fillRect(int x, int y, int width, int height)
           
 void flush()
          Flushes all pending graphics operations of the rendering pipe into its underlying image.
 java.awt.Color getColor()
           
protected  void insertEdge(ch.randelshofer.awt.graphics.Edge list, ch.randelshofer.awt.graphics.Edge edge)
          Inserts edge into list in order of increasing edge.intersect
protected  void makeEdgeRecHorizontal(int lower, int upper, int yComp, ch.randelshofer.awt.graphics.Edge edge, int[] x, int[] y)
           
protected  void makeEdgeRecVertical(int lower, int upper, int xComp, ch.randelshofer.awt.graphics.Edge edge, int[] x, int[] y)
           
protected  void plotHClipped(int scan, ch.randelshofer.awt.graphics.Edge active)
           
protected  void plotVClipped(int scan, ch.randelshofer.awt.graphics.Edge active)
           
protected  void prepareEdgeList(int scan)
           
protected  void resortActiveList(ch.randelshofer.awt.graphics.Edge active)
           
protected  void scanFillHorizontal(int[] x, int[] y, int cnt, int miny, int maxy)
          ScanFill algorithm for shapes which are wider than tall using a concurrent algorithm.
protected  void scanFillVertical(int[] x, int[] y, int cnt, int minx, int maxx)
          ScanFill algorithm for shapes which are taller than wide.
 void setColor(java.awt.Color c)
           
protected  void updateActiveList(int scan, ch.randelshofer.awt.graphics.Edge active)
           
protected  int xNext(int k, int[] x, int[] y, int cnt)
          given an index, returns x coordinate of next nonvertical line
protected  int yNext(int k, int[] x, int[] y, int cnt)
          given an index, returns y coordinate of next nonhorizontal line
 
Methods inherited from class ch.randelshofer.awt.graphics.AbstractPipe
arrayfill, clearRect, clipRect, clipTest, clipTest, getBackground, setBackground, setClip, setClip, setComposite, setTransform, transform, translate, translate
 
Methods inherited from class ch.randelshofer.awt.graphics.Pipe
addRenderingHints, clip, copyArea, create, dispose, drawArc, drawGlyphVector, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawRenderableImage, drawRenderedImage, drawRoundRect, drawString, drawString, drawString, drawString, fillArc, fillRoundRect, getClip, getClipBounds, getComposite, getDeviceConfiguration, getFont, getFontMetrics, getFontRenderContext, getPaint, getRenderingHint, getRenderingHints, getStroke, getTransform, hit, rotate, rotate, scale, setFont, setPaint, setPaintMode, setRenderingHint, setRenderingHints, setStroke, setXORMode, shear, toString
 
Methods inherited from class java.awt.Graphics2D
draw3DRect, fill3DRect
 
Methods inherited from class java.awt.Graphics
create, drawBytes, drawChars, drawPolygon, fillPolygon, finalize, getClipBounds, getClipRect, getFontMetrics, hitClip
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

oneMinusAlpha

protected int oneMinusAlpha
Precomputed values to speed up alpha compositing.


premultipliedR

protected int premultipliedR

premultipliedG

protected int premultipliedG

premultipliedB

protected int premultipliedB

previousIn

protected int previousIn

previousOut

protected int previousOut

color

protected int color
Current drawing color.


dispatcher

protected Dispatcher dispatcher
Dispatcher for Runnable objects.


edges

protected ch.randelshofer.awt.graphics.Edge[] edges
Is filled with a list of edges for each scan line. The list of edges on an indivdiual scan line is sorted from left to right.

Constructor Detail

ConcurrentTranslucentPipe

public ConcurrentTranslucentPipe(java.awt.image.BufferedImage image,
                                 Dispatcher dispatcher)
Creates a new instance.

Method Detail

flush

public void flush()
Description copied from class: Pipe
Flushes all pending graphics operations of the rendering pipe into its underlying image.

Overrides:
flush in class AbstractPipe

setColor

public void setColor(java.awt.Color c)
Overrides:
setColor in class Pipe

getColor

public java.awt.Color getColor()
Overrides:
getColor in class Pipe

drawRect

public void drawRect(int x,
                     int y,
                     int width,
                     int height)
Overrides:
drawRect in class java.awt.Graphics

fillRect

public void fillRect(int x,
                     int y,
                     int width,
                     int height)
Overrides:
fillRect in class Pipe

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Specified by:
drawLine in class java.awt.Graphics

drawOval

public void drawOval(int ox,
                     int oy,
                     int owidth,
                     int oheight)
Overrides:
drawOval in class Pipe

fillOval

public void fillOval(int ox,
                     int oy,
                     int owidth,
                     int oheight)
Overrides:
fillOval in class Pipe

drawPolygon

public void drawPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)
FIXME - Must use drawLineSegment for all line segments except for the last one in order to prevent pixels at line joints from overlapping!

Overrides:
drawPolygon in class Pipe

drawPolyline

public void drawPolyline(int[] xPoints,
                         int[] yPoints,
                         int nPoints)
FIXME - Must use drawLineSegment for all line segments except for the last one in order to prevent pixels at line joints from overlapping!

Overrides:
drawPolyline in class Pipe

fillPolygon

public void fillPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)
Overrides:
fillPolygon in class Pipe

draw

public void draw(java.awt.Shape s)
Overrides:
draw in class Pipe

fill

public void fill(java.awt.Shape s)
Overrides:
fill in class Pipe

scanFillHorizontal

protected void scanFillHorizontal(int[] x,
                                  int[] y,
                                  int cnt,
                                  int miny,
                                  int maxy)
ScanFill algorithm for shapes which are wider than tall using a concurrent algorithm.

Parameters:
miny - Must be = smallest y and >= cy1.
maxy - Must be = biggest y and <= cy2.

scanFillVertical

protected void scanFillVertical(int[] x,
                                int[] y,
                                int cnt,
                                int minx,
                                int maxx)
ScanFill algorithm for shapes which are taller than wide.

Parameters:
minx - Must be = smallest x and >= cx1.
maxx - Must be = biggest x and <= cx2.

insertEdge

protected void insertEdge(ch.randelshofer.awt.graphics.Edge list,
                          ch.randelshofer.awt.graphics.Edge edge)
Inserts edge into list in order of increasing edge.intersect


buildEdgeListHorizontal

protected void buildEdgeListHorizontal(int[] x,
                                       int[] y,
                                       int cnt)

buildEdgeListVertical

protected void buildEdgeListVertical(int[] x,
                                     int[] y,
                                     int cnt)

yNext

protected int yNext(int k,
                    int[] x,
                    int[] y,
                    int cnt)
given an index, returns y coordinate of next nonhorizontal line


xNext

protected int xNext(int k,
                    int[] x,
                    int[] y,
                    int cnt)
given an index, returns x coordinate of next nonvertical line


makeEdgeRecHorizontal

protected void makeEdgeRecHorizontal(int lower,
                                     int upper,
                                     int yComp,
                                     ch.randelshofer.awt.graphics.Edge edge,
                                     int[] x,
                                     int[] y)

makeEdgeRecVertical

protected void makeEdgeRecVertical(int lower,
                                   int upper,
                                   int xComp,
                                   ch.randelshofer.awt.graphics.Edge edge,
                                   int[] x,
                                   int[] y)

buildActiveList

protected void buildActiveList(int scan,
                               ch.randelshofer.awt.graphics.Edge active)

plotHClipped

protected final void plotHClipped(int scan,
                                  ch.randelshofer.awt.graphics.Edge active)

plotVClipped

protected void plotVClipped(int scan,
                            ch.randelshofer.awt.graphics.Edge active)

updateActiveList

protected void updateActiveList(int scan,
                                ch.randelshofer.awt.graphics.Edge active)

prepareEdgeList

protected void prepareEdgeList(int scan)

deleteAfter

protected void deleteAfter(ch.randelshofer.awt.graphics.Edge q)

resortActiveList

protected void resortActiveList(ch.randelshofer.awt.graphics.Edge active)

Copyright 2011-09-18 Werner Randelshofer