This page last changed on Apr 28, 2006 by iiley.

Canvas, a MovieClip in a JPanel

This tutorial will help you get a way to do things with MovieClips you'v familar with before, now in AsWing .

the code

/*
 Copyright aswing.org, see the LICENCE.txt.
*/
import org.aswing.JPanel;
import org.aswing.JWindow;

/**
 * @author iiley
 */
class test.CanvasTest {
	
	private var canvasPanel:JPanel;
	private var canvasMC:MovieClip;
	
	public function CanvasTest(){
		canvasPanel = new JPanel();
		canvasPanel.addEventListener(JPanel.ON_CREATED, __canvasCreated, this);
		
		//create a window to show the canvas panel
		var window:JWindow = new JWindow();
		window.setContentPane(canvasPanel);
		window.setSize(Stage.width, Stage.height);
		window.show();
	}
	
	private function __canvasCreated():Void{
		//you must create the mc after component created, like the way 
		//here in ON_CREATED handler method
		canvasMC = canvasPanel.createMovieClip("canvas");
		//Then you have a movie clip, you can do anything a movie clip can do here
		//here we draw some thing on it, and create a text filed at it
		canvasMC.lineStyle(4, 0xff0000);
		canvasMC.moveTo(100, 100);
		canvasMC.lineTo(200, 200);
		canvasMC.createTextField("aText", 0, 150, 150, 150, 20);
		canvasMC["aText"].text = "a text on canvas mc";
	}
	
	public static function main():Void{
		Stage.scaleMode = "noScale";
		var t:CanvasTest = new CanvasTest();
	}
}

Run this app, you'll see a red and a text on the stage, all the visible thing are on the canvasMC, so from this way, you can do things you'v ever did with flash, you can attach symbols, create sub movieclips, textfields, play movies etc.

CanvasTest.swf


CanvasTest.swf (application/x-shockwave-flash)

Hi! 

Now, I would like to insert a JScrollPane to the JPanel and DragAndDrop the MovieClip.
When I drag the MovieClip to a square, the vertical scroll or the horizontal scroll appears.
Can I do it? How?

 Thanks

Posted by at Jul 13, 2006 11:03

1 : Locate a JPanel to the JScrollPane as the canvas.
2 : Set a default preffered size to the canvas.
3 : When user drag the MovieClip to a square, check the moviclip bounds, if the canvas need spread, set a bigger prffered size to it, and call revalidate. (When the canvas spreaded bigger than the scrollpane, the scroll bars will appear)

Posted by iiley at Jul 15, 2006 03:33
Document generated by Confluence on Dec 19, 2006 14:17