Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Smart Java to ActionScript 3 translator in progress  (Read 5395 times)
0 Members and 1 Guest are viewing this topic.
Offline krasse
« Posted 2010-08-23 14:34:20 »

I thought: Now I have programmed Java for many years, how about trying out this Flash and ActionScript thing? Expand my horizon, try new things is refreshing etc.

Full of hope I downloaded the newest Flash Builder. They use Eclipse! I guess they have implemented all that amazing refactoring and quick fix that I use everyday!?

Well, some of you know the answer: Flash Builder is just a weak, distorted shadow of what the Eclipse tools for Java is.

Long story short: Want to program in java and translate to Actionscript 3 (similar to the Google app engine).
Existing tool: j2as (broken link to source)
My approach: use japa Java parser. Add a lot of stubs for Actionscript classes. Create Actionscript classes to mimic ArrayList, HashMap, HashSet etc.

Here is a link to the current version (which also contains an applet):

http://www.springworldgames.com/javatoas3/


Offline krasse
« Reply #1 - Posted 2010-08-23 20:00:11 »

Here is the link to the current version of the source code:

http://www.springworldgames.com/javatoas3/

It is just a very simple quick and dirty translator at the moment without any ActionScript classes and Java stubs.
I guess that most of the translation will be easy since ActionScript is very similar to a subset of Java. Some decisions has to be made about the function references that are used all the time in ActionScript for handlers. I guess that a special Java-interface will do the trick but we'll see...

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 81
Projects: 15


★★★★★


« Reply #2 - Posted 2010-08-23 22:38:45 »

There has been few heroic attempts at this in the past but most have failed. Does look as if you still have tons of work to do before its usable but do keep us updated.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline krasse
« Reply #3 - Posted 2010-08-24 09:30:18 »

I have coded a bit more and just want to show some of the progress.

The following is what you write in Java (and it compiles with the help from stub classes):


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
package com.springworldgames.javatoas3.testfiles;

import com.springworldgames.javatoas3.as3replacers.EventFunction;

import flash.display.BlendMode;
import flash.display.CapsStyle;
import flash.display.Graphics;
import flash.display.LineScaleMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.ui.Mouse;

public class ch7ex1 extends Sprite {

   protected Sprite compass;
   protected Sprite crosshairs;

   public ch7ex1() {
      drawCompass();
      drawCrosshairs();
      addEventListener(Event.ENTER_FRAME, tick);
   }

   EventFunction tick = new EventFunction() {
      @Override
      public void eventFunction(Event event) {
         Mouse.hide();
         crosshairs.x = stage.mouseX;
         crosshairs.y = stage.mouseY;
         compass.rotation = Math.atan2(stage.mouseY - compass.y,
               stage.mouseX - compass.x)
               * 180 / Math.PI;
      }
   };
   
   protected void drawCompass() {
      double RADIUS = 30;
      compass = new Sprite();
      Graphics g = compass.graphics;
      g.lineStyle(4, 0xe03030);
      g.beginFill(0xd0d0d0);
      g.drawCircle(0, 0, RADIUS);
      g.endFill();
      g.moveTo(0, 0);
      g.lineTo(RADIUS, 0);
      addChild(compass);
      compass.x = stage.stageWidth / 2;
      compass.y = stage.stageHeight / 2;
   }

   protected void drawCrosshairs() {
      double SIZE = 10;
      crosshairs = new Sprite();
      Graphics g = crosshairs.graphics;
      g
            .lineStyle(8, 0x4040f0, 1, false, LineScaleMode.NONE,
                  CapsStyle.SQUARE);
      g.moveTo(0, SIZE);
      g.lineTo(0, -SIZE);
      g.moveTo(SIZE, 0);
      g.lineTo(-SIZE, 0);
      crosshairs.blendMode = BlendMode.MULTIPLY;
      addChild(crosshairs);
   }
}


The following is the ActionScript 3 result:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
package com.springworldgames.javatoas3.testfiles {
    import flash.display.BlendMode;
    import flash.display.CapsStyle;
    import flash.display.Graphics;
    import flash.display.LineScaleMode;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.ui.Mouse;

    public class ch7ex1 extends Sprite {

        protected var compass:Sprite;

        protected var crosshairs:Sprite;

        public function ch7ex1() {
            drawCompass();
            drawCrosshairs();
            addEventListener(Event.ENTER_FRAME, tick);
        }

        public function tick(event:Event):void {
            Mouse.hide();
            crosshairs.x = stage.mouseX;
            crosshairs.y = stage.mouseY;
            compass.rotation = Math.atan2(stage.mouseY - compass.y, stage.mouseX - compass.x) * 180 / Math.PI;
        }

        protected function drawCompass():void {
            var RADIUS:Number = 30;
            compass = new Sprite();
            var g:Graphics = compass.graphics;
            g.lineStyle(4, 0xe03030);
            g.beginFill(0xd0d0d0);
            g.drawCircle(0, 0, RADIUS);
            g.endFill();
            g.moveTo(0, 0);
            g.lineTo(RADIUS, 0);
            addChild(compass);
            compass.x = stage.stageWidth / 2;
            compass.y = stage.stageHeight / 2;
        }

        protected function drawCrosshairs():void {
            var SIZE:Number = 10;
            crosshairs = new Sprite();
            var g:Graphics = crosshairs.graphics;
            g.lineStyle(8, 0x4040f0, 1, false, LineScaleMode.NONE, CapsStyle.SQUARE);
            g.moveTo(0, SIZE);
            g.lineTo(0, -SIZE);
            g.moveTo(SIZE, 0);
            g.lineTo(-SIZE, 0);
            crosshairs.blendMode = BlendMode.MULTIPLY;
            addChild(crosshairs);
        }
    }
}



The example is from the ActionScript 3.0 Bible second edition. I think I will go through all the examples in that book and try to translate Java code into them.

I am aware that it will not become a general tool for all possible ways to work with ActionScript and Java, but hopefully it will transform into a fairly useful compromise.

Offline krasse
« Reply #4 - Posted 2010-08-26 07:17:45 »

There is now an applet available for the translator.

The translation is purely syntax-based at the moment so don't expect to much Smiley

The real power (if any) of this approach is when you use it together with Eclipse/Netbeans/Idea with all the stubs (see the source code).

The number of stubs has increased a lot since the last post.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-08-26 15:27:26 »

You should do a static import of TopLevel, assuming that JavaParser supports that.

Also, setWithIndex(key, val) feels way too verbose, why not use put(key, val) ?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline krasse
« Reply #6 - Posted 2010-08-26 16:51:47 »

Static import is supported by the parser, so that is a good idea Smiley

I use a similar trick for functions that aren't part of any class (just a package) but then I also add an extra import.

I would love to use put() instead of the more verbose setWithIndex() but then I have to keep track of the type of all expressions and I haven't implemented anything like that. The current version just knows whether it is a method name or not from the syntax tree and boldly and blindly replaces it if it matches. The "Smart" prefix of this thread just became a big fat lie Smiley

I don't know how difficult it is to perfom type analysis, but I'll give it a try! This would really make the whole thing better and unlock possibilties.

Edit: You have to gather a lot of information from all imported classes, both from source and jar-files, to do type analysis in a general way. I might just apply the KISS principle, even if it is ugly.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-08-26 18:18:00 »

Maybe use:

   map.$put(key, val)
   list.$add(item)


or

   map._put(key, val)
   list._add(item)


I know it looks a lot like PHP / C, but at least it's short, and unique...? persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline krasse
« Reply #8 - Posted 2010-08-26 18:43:56 »

Good idea Riven!

I think I will change it to put$(key, val) and get$(key) for Arrays, Objects and Dictionaries. For Vectors I will use set$(index, val) and get$(index).

BTW, I have also added the possibility for statically importing TopLevel and iteration over keys (in Object/Array/Dictionary) with a keySet$() function as a tribute to our beloved Collection framework Smiley

I am also testing all this for real since I am writing a very simple ActionScript Midi synth to use for my generative music stuff.


Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!