PDA

View Full Version : Drawing class - comments ? test pls


ferdymania
10-31-2003, 07:02 PM
I am writing a class that will make using the drawing api easier to use...
it mirrors the java Graphics class
usage is :

var gr:Graphics = new Graphics(depth);
gr.setColor("0xff0000");
gr.setThick(3);
gr.setFill("0x00ff00");
gr.drawLine(x,y,x1,y1);
gr.drawRect(startx,starty,width, height);
gr.fillRect(same);
gr.drawOval(startx,starty,width,height);
gr.fillOval( ...same..);


It doesnt draw a circle real well but if any genius bezier mathematicians can tell me where to put the control points for a circle then it would work properly for a circle..
any additions, refactoring, abuse welcome...

class Graphics {
var mc;
var color:String;
var fill:String;
var thick:Number;
// constructor
public function Graphics(depth:Number) {
_root.createEmptyMovieClip("test1", depth);
mc = _root.test1;
// set defaults
color = "0x000000";
thick = 1;
fill = "0x666666";
}
// setters
function setColor(_color:String) {
color = _color;
}
// set thickness
function setThick(_thick:Number) {
thick = _thick;
}
// set fill
function setFill(_fill:String) {
fill = _fill;
}
//
//drawing functions
function drawLine(x1:Number, y1:Number, x2:Number, y2:Number) {
mc.lineStyle(thick, color);
mc.moveTo(x1, y1);
mc.lineTo(x2, y2);
}
function drawRect(x1:Number, y1:Number, width:Number, height:Number) {
mc.lineStyle(thick, color);
mc.moveTo(x1, y1);
mc.lineTo(x1+width, y1);
mc.lineTo(x1+width, y1+height);
mc.lineTo(x1, y1+height);
mc.lineTo(x1, y1);
}
// fillRect
function fillRect(x1:Number, y1:Number, width:Number, height:Number) {
mc.lineStyle(thick, color);
mc.moveTo(x1, y1);
mc.beginFill(fill);
mc.lineTo(x1+width, y1);
mc.lineTo(x1+width, y1+height);
mc.lineTo(x1, y1+height);
mc.lineTo(x1, y1);
mc.endFill();
}
// draw Oval
function drawOval(x:Number,y:Number, width:Number,height:Number){
mc.lineStyle(thick, color);
mc.moveTo(x,y+height/2);
mc.curveTo(x,y,x+width/2, y);
mc.curveTo(x+width,y,x+width, y+height/2);
mc.curveTo(x+width,y+height, x+width/2, y+height);
mc.curveTo(x,y+height, x, y+height/2);
}
// fill oval
function fillOval(x:Number,y:Number, width:Number,height:Number){
mc.lineStyle(thick, color);
mc.moveTo(x,y+height/2);
mc.beginFill(fill);
mc.curveTo(x,y,x+width/2, y);
mc.curveTo(x+width,y,x+width, y+height/2);
mc.curveTo(x+width,y+height, x+width/2, y+height);
mc.curveTo(x,y+height, x, y+height/2);
mc.endFill();
}
}

farafiro
11-02-2003, 05:17 AM
i hope this can help

http://astronomy.swin.edu.au/~pbourke/curves/bezier/

ferdymania
11-02-2003, 08:12 AM
thanks faffy,
that is really complex maths...
i will have a good hard look at it...
my good friend deamothul worked out the code for the circle
and some stuff for gradient fills which i incorporated..
You could do some amazingly spirally animations
with all that maths but...
Here is my class as i have done up to now..
steve