Graphics2D Stroke Applet
The default rendering of a line is continuous and thin. You can change the properties of the line using a BasicStroke. The arguments passed to the BasicStroke constructor are:
- the line thickness;
- the ends of lines (BasicStroke.CAP_BUTT, BasicStroke.CAP_ROUND or BasicStroke.CAP_SQUARE);
- the line joins (BasicStroke.JOIN_MITER, BasicStroke.JOIN_BEVEL or BasicStroke.JOIN_ROUND);
- trimming of mitre joins for small angles (the default value of 10.0 meaning that angles less than 10 degrees would be converted to bevel joins);
- dash attributes (e.g. [4.0, 2.0] to alternate 4 point dashes with 2 point spaces);
- the offset to start the dashing pattern.
namespace stroke_demo; //Based on an applet by Michael McGuffin which was converted to Oxygene for Java by Dharmesh Tailor interface uses java.util, java.applet.*, java.awt.*; type StrokeApplet = public class(Applet, MouseListener, MouseMotionListener) private const dash1 : array[0 .. 1] of Single = [4.0, 2.0]; var dashed : BasicStroke := new BasicStroke(3.0, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0, dash1, 0.0); var width, height : Integer; var x, y : Integer; //Coordinates of upper-left corner of box var mx, my : Integer; //Mouse coordinates var isMouseDraggingBox : Boolean := false; public method init; override; method mouseEntered(e : MouseEvent); method mouseExited(e : MouseEvent); method mouseClicked(e : MouseEvent); method mousePressed(e : MouseEvent); method mouseReleased(e : MouseEvent); method mouseMoved(e : MouseEvent); method mouseDragged(e : MouseEvent); method paint(g : Graphics); override; end; implementation method StrokeApplet.init; begin width := getSize.width; height := getSize.height; setBackground(Color.black); x := width/2 - 20; y := height/2 - 20; addMouseListener(self); addMouseMotionListener(self); end; method StrokeApplet.mouseEntered(e : MouseEvent); begin end; method StrokeApplet.mouseExited(e : MouseEvent); begin end; method StrokeApplet.mouseClicked(e : MouseEvent); begin end; method StrokeApplet.mousePressed(e : MouseEvent); begin mx := e.X; my := e.Y; if (x < mx) and (mx < x + 40) and (y < my) and (my < y + 40) then begin isMouseDraggingBox := true; end; e.consume; end; method StrokeApplet.mouseReleased(e : MouseEvent); begin isMouseDraggingBox := false; e.consume; repaint; end; method StrokeApplet.mouseMoved(e : MouseEvent); begin end; method StrokeApplet.mouseDragged(e : MouseEvent); var new_mx, new_my : Integer; begin if isMouseDraggingBox then begin //Get the latest mouse position. new_mx := e.X; new_my := e.Y; //Displace the box by the distance moved since the last event. x := x + new_mx - mx; y := y + new_my - my; //Update the coordinates. mx := new_mx; my := new_my; repaint; e.consume; end; end; method StrokeApplet.paint(g : Graphics); var g2 : Graphics2D; begin g2 := Graphics2D(g); g2.setStroke(dashed); g2.setColor(Color.green); if isMouseDraggingBox then g2.draw(new Rectangle2d.Float(x, y, 40, 40)) else g2.fill(new Rectangle2d.Float(x, y, 40, 40)); end; end.