Вы находитесь на странице: 1из 39

MaxJava API

MaxJava API

1. .....................................................................................................................................3
.................................................................................................................................3
Java-external ................................................................................................3
Java-external Max? ......................................................................................3
....................................................................................................................4
mxj (CLASSPATH) ...............................................................................5
mxj quickie Java IDE Cycling74........................................................................................5
2. Java-external ........................................................................9
............................................................................................................................9
..............................................................................11
(inlets) (outlets) ....................................................................11
Info- (Info Outlet) ...........................................................................................................12
Java-external ..................................................................12
......................................................................13
............................................................13
...........................................................................................14
getInlet() , .......14
int float ...................................................................................14
..................................................................................................................15
...................................................................................16
.....................................................................18
anything ....................................................................................................20
viewsource.............................................................................................................21
............................................................................................................21
3. Max...................................................................................................23
outlet.........................................................................................................................23
info- .................................................................................25
.......................................................................................................25
........................................................................................27
4. . ......................................................................29
Max...................................................................................................29
..........................................................................29
MaxClock Callback .............................................................30
.........................................................32
Java- ..................................33
5. ....................................................................................35
.....................................................................................................................35
pac ................................................................................................................................38

MaxJava API

1.

MaxMSP 4.5, ,
Max/MSP/Jitter, external Java.
External , ,
Max. external ,
Max, .

Java-external
external Java?
Java-external -.
-. , Max Windows, MacOS X.
Java , .
Java : (
external, ),
..
Java - ( ) -
().
Java s : ,
, , . Java
( , , XML .),
.
Cycling74 Java-IDE (integrated development environment)
mxj quickie, Java-external
Max. , -
Max (external
, Max)

Java-external Max?
Java-external external .
-external , Java- (
external ,
Java-external Java- ) Max
- mxj.

MaxJava API
, Java- myextern,
- mxj ( ):
[mxj myextern], object box ,
-externala.
mxj ( MSP- mxj~)
Java- (JVM), Max.
Max ( ), Java- mxj
.
Java- Max:


, Max IDE
mxj quickie, Max.
Java IDE. :
NetBeans IDE, Sun. (http://www.netbeans.org)
JBuilder Foundation IDE Borland. (http://www.borland.com)
Eclipse IDE IBM (http://www.eclipse.org)
, IDE : ,
, .

MaxJava API
- max.jar,
Program Files\Common Files\Cycling74\java\lib (Windows)
Library/Application Support/Cycling74/java/lib (OSX), lib\ext,
Java.
max.jar API (application programming interface
), Max
Java-, - mxj.
MaxMSP 4.5 html-,
javadoc.
Java- Max.

mxj (CLASSPATH)
- mxj ,
- , mxj.
mxj ( IDE) ,
Java-.
: Program Files\Common Files\Cycling74\java\classes (Windows) Library/
Application/Support/ Cycling74/ java/classes (OSX),
mxj.
mxj - max.java.config.txt,
Program Files\Common Files\Cycling74\java (Windows)
Library/Application Support/Cycling74/java (OSX). , mxj
d:/javaclass - : max.system.class.dir d/javaclass

mxj quickie Java IDE Cycling74


Java-external mxj quickie IDE Cycling74. ,
. mxj quickie ,
Max ,
, Java-external.
, mxj quickie Java-externala,
mxj.
,
MaxJava-:
externala , .
mxj quickie ,
,
external. , mxj quickie Javaexternal MaxJava API.
mxj quickie ,
Java-external. external Max Hello,
world!, bang ( ) .
,
externala helloworld mxj
quickie.
5

MaxJava API
Max/MSP.
.
object box : mxj quickie helloworld
object box mxj quickie helloworld.

.
Java external.
- , .. JVM
Swing. object box mxj quickie
helloworld , ,
.
, helloworld.java.
Java- ,
, , File->Save
( .java, , Java).
, mxj quickie heloworld,
object box.
, heloworld.java, .

MaxJava API
bang :
public void bang()
{
}
:
post(Hello, World!);
:
Public void bang()
{
post(Hello, World!);
}
Java Open Compile Window ,
.
Compiler Command:. javac,
.
Browse
javac.
, Windows Java-, .
JVM, Java 2 Software Development Kit
(J2SDK). ,
Java- - Java 2 Runtime Environment (J2RE),
JVM.
Sun Microsystems (http://www.java.sun.com).
Mac OSX JVM .
mxj Java 2 1.4.
Java 2 1.5 max.java.config.txt :
max.java.jvm.version 1.5
Compiler Command: ,
.
Browse .
, , Compile, -
.class .
, , ,
.
- ,
. ,
, .
.
, ,
Command-k (OSX) Control-k (Windows).
, ,
Command-e (OSX) Control-e (Windows).
7

MaxJava API
external, .
Java-externala, object box
[mxj helloworld]. button. button ()
external. button, Max
Hello, World!.
, externala,
object box [mxj helloworld]. external,
, object box (
), [mxj quickie helloworld] .

( , mxj quickie
-). (helloworld.java) - (helloworld.class)
, mxj quickie,
MaxJava .
: -
mxj ( - mxj
, ).
mxj [mxj helloworld],
mxj, .
mxj, ,
.

MaxJava API

2. Java-external
MaxJava API.
html-,
Max.

Java-external mxj Java-


com.cycling74.max.MaxObject.
MaxObject, ,
Max.
MaxObject, .
public class scale extends com.cycling74.max.MaxObject
{
private float scale_factor; // scale
}
:
/* MaxJava API max.jar.

MaxJava API */
import com.cycling74.max.*;
public class scale extends MaxObject
{
private float scale_factor; // scale
}
Java-external,
, . , ,
scale_factor
Java-external . ,
object box - [mxj scale 1.2] .
:
public scale()
{
bail("(mxj scale) you must provide a default+
scale factor e.g. [mxj scale 0.5].");
}
public scale(float scale_arg)
{
scale_factor = scale_arg;
}

MaxJava API
object box mxj scale,
, .
bail Max ,
, .
mxj scale .
,
.
foo,
.
thename .
public class foo extends MaxObject
{
//
String name;
float velocity;
int factor;
public foo()
{
bail("(mxj foo) must provide a name for+
foo e.g. [mxj foo cheesecake]");
}
public foo(String thename)
{
this(thename, 1.2, 40);
}
public foo(String thename, float thevelocity)
{
this(thename,thevelocity,40);
}
public foo(String thename, float thevelocity,
int thefactor)
{
name = thename;
velocity = thevelocity;
factor = thefactor;
}
}
object box
:
[mxj foo goober]
[mxj foo goober 0.7]
[mxj foo goober 0.7 42]
[mxj foo]
, thename
.
10

MaxJava API


, Max, ,
. :
public foo (float x, float y, float z)
[mxj foo 1 0 1] object box - 1 0 1
float ( 1.0 0.0 1.0),
.
Java,
:
public
public
public
public
public

foo(int i, float f, String s)


foo(short s)
foo(boolean b1, boolean b2, byte b)
foo(int[] intarray)
foo(float[] floatarray)

..
, ,
. :
public foo(int[] ia1, int[] ia2)

public foo(float f, int[] ia)


Java-external.

(inlets) (outlets)
- mxj
.
declareInlets declareOutlets MaxObject.
declareInlets declareOutlets
int-, - .
, , static
final (, ) com.cycling74.max.DataTypes.
, :
DataTypes.INT

/ int

DataTypes.FLOAT / float
DataTypes.ALL

/ int, float String

11

MaxJava API
, , external 2 2 ,
:
import com.cycling74.max.*;
public class foo extends MaxObject
{
/* */
private float _x;
private float _y;
private float _z;
public foo()
{
bail((mxj foo) must provide x,y, and z arguments);
}
public foo(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
declareInlets(new int[]{ DataTypes.ALL, DataTypes.ALL});
declareOutlets(new int[]{ DataTypes.ALL, DataTypes.ALL});
}
}

Info- (Info Outlet)


Java-external ,
info- (info-outlet).
. info- ,
createInfoOutlet(false);. :
public foo(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
declareInlets(new int[]{ DataTypes.ALL, DataTypes.ALL});
declareOutlets(new int[]{ DataTypes.ALL, DataTypes.ALL});
createInfoOutlet(false);
}

Java-external
, Java-external ,
info-. , Java-external
, NO_INLETS
NO_OUTLETS MaxObject.
declareInlets(NO_INLETS); //
declareOutlets(NO_OUTLETS); //

12

MaxJava API
: MSP mxj~ Java-external .
Max.


external.
- Max Patcher.
, , ,
.
setInletAssist
setOutletAssist MaxObject.
String-, - .
public foo(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
declareInlets(new int[]{ DataTypes.ALL, DataTypes.ALL});
declareOutlets(new int[]{ DataTypes.ALL, DataTypes.ALL});
setInletAssist(new String[] { "bang to output",
"bang to reset"});
setOutletAssist(new String[] { "calc result 1",
"calc result 2"});
}


Java-external:
declareIO declareTypedIO. , 3 2
. declareIO
declareInlets declareOutlets. declareIO
DataTypes.ALL.
public foo(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
declareIO(3,2);
}
declareTypedIO declareIO,
, .
declareTypedIO html- MaxObject.

13

MaxJava API


Max .
, number box (box1),
int, number box
(box2), box1 int box2.
int 22 :
[box1]------- int 22 -------[box2]
external, , (
addmess),
.
MaxJava API, MaxObject,
, ,
Max. , , Java-external
bang, bang
MaxObject.
public void bang()
{
post("I was just banged pretty well.");
}
, bang -
Max "I was just banged pretty well.".

getInlet() ,

, ,
, getInlet.
public void bang()
{
int inlet_num = getInlet();
post("I was just banged pretty well in inlet "+inlet_num);
}
getInlet , .
0, . , bang
, : "I was just banged pretty well on inlet 0",
bang , : "I was just banged pretty well on
inlet 1".

int float
int float bang,
, . -
Java-external number box ,
int float.
14

MaxJava API
,
inlet MaxObject. int
float.
public void inlet(int i)
{
int intlet_no;
inlet_no = getInlet();
post("I got an integer in inlet "+inlet_no);
}
public void inlet(float f)
{
int intlet_no;
inlet_no = getInlet();
post("I got a floating point number in inlet "+inlet_no);
}
, ,
. \ . Java-
, (.. ).
: float
(inlet (float f)) - mxj int
float. , inlet (int i) - mxj
float int,
.


Max, , ,
(list),
(message).
message box (box1), [1.2 4 killa 8.6 bass 3 88 mahler]
message box (box2), box1
, box1, :
' '-->[1.2 4 killa 8.6 bass 3 88 mahler ]-->
list 1.2 4 killa 8.6 bass 3 88 mahler-->[box2]
, ,
, .
Atom. Atom ,
(union), .
, Max mxj
Atom. ,
externela Max,
Atom. , Max Java.
Max : ,
, - mxj
String, Java.

15

MaxJava API
,
, Atom,
.
public void list(Atom[] args)
{
post("Received list message at inlet "+getInlet());
// Atom
Atom a;
for(int i = 0; i < args.length; i++)
{
a = args[i];
if(a.isFloat())
post("List element "+i+" is a floating point atom with
a value of "+a.getFloat());
else if(a.isInt())
post("List element "+i+" is an integer atom with a
value of "+a.getInt());
else if(a.isString())
post("List element "+i+" is a String atom with a value
of "+a.getString());
}
}
:
Max
. , 1.2 killa 8.6 bass mahler
, :
List element 0 is a floating point atom with a value of 1.2
List element 1 is an integer atom with a value of 4
List element 2 is a String atom with a value of killa
List element 3 is a floating point atom with a value of 8.6
List element 4 is a String atom with a value of bass
List element 5 is an integer atom with a value of 3
List element 6 is an integer atom with a value of 88
List element 7 is a String atom with a value of mahler


external , Max
addmess.
addmess,
,
.
16

MaxJava API
Java-external ,
, MaxJava API
, .
, ,
++ , Smalltalk .
Java-
public, private protected. ,
. private,
, .
() .
protected, ,
, () ,
(package).
(friendly) ++, ,
A B
B.
, , public,
, JVM . public ,
.
,
public, private protected (
). ,
(package), .
, external,
,
. public,
void ( ). , ,
stop,
:
public void stop()
{
post("I received the stop message.");
}
, - stop object box
Java-externala, message box
.
, int float
inlet, int float? , int float
Java .

17

MaxJava API


resize 100.
,
, Atom .
.
, resize :
public void resize(Atom[] newsize)
{
Atom a;
if(newsize.length >= 1)
{
a = newsize[0];
if(a.isInt())
_do_resize(a.getInt());
else if(a.isFloat())
_do_resize((int)a.getFloat());
else
post("resize message does not understand the string
argument "+a.getString());
}
}
, ,
resize:
public void resize(int newsize)
{
_do_resize(newsize);
}
, , mxj
.
. , - Java-external resize c
float, resize 200.4, mxj
float 200.4 int 200, .
: - Java-external resize
( resize goforit) - mxj Max
resize 0.
, ,
, Max ,
0.
, ,
( ) -
.

18

MaxJava API
public void doit(int i1, int i2, float f1, float f2)
{
post("doit was called with "+i1+":"
+i2+":"
+f1+":"
+f2);
}
,
mxj, ,
.
boolean

mxj true, false.


, false .

byte

mxj byte (8 )
-128 127. 0.

char

mxj
Unicode. 1 mxj
.

short

mxj short (16 )


-32768 32767.
0.

int

mxj int (32 )


-2147483648 2147483647.
0.

long

mxj long (64 )


Max 32 ,
Max 32 .
long .
0.

float

mxj float (32 )


+/-1.4E-45 +/-3.4028235E+38.
0.

double

mxj double (64 )


+/-1.4E-45 +/-3.4028235E+38.
Max 32 , Max
32 .
double .
0.

String

mxj
String Max .
String.

19

MaxJava API

.
. :
public foo(int[] ia1, int[] ia2)

public foo(float f, int[] ia)


, :
public foo(boolean[] ba)
.
,
.
boolean[]
byte[]
char[]
short[]
int[]
long[]
float[]
double[]
String[]
,
Atom[]: .

anything
MaxJava API : anything.
:
public void anything(String msg, Atom[] args)
external ,
, anything
msg ( String), args
( Atom), . anything
,
.

20

MaxJava API
,
:
public void anything(String msg, Atom[] args)
{
post("I received a " + msg + " message.");
if (args.length > 0)
{
post("It has the following arguments:");
for (int i=0;i<args.length;i++)
post(args[i].toString());
}
}

viewsource
MaxObject, Java-external (..
MaxObject)
viewsource. viewsource Java-external,

- MXJEditor. - .
, ,
, , :
public void viewsource()
{
}
viewsource
, .. mxj quickie IDE.


Javaexternal , Max,
. , - mxj
, Max, , 3 ( Java
9).
9 Java mxj
Java-external Java-,
Java- Max .
:
public void doit(int i);
public void doit(short s);
public void doit(long l);
Max ,
doit 24, mxj ,
.
21

MaxJava API
, :
public void doit(int i);
public void doit(float f);
, :
public void doit(float f);
- mxj .
, ,
Java-external:
1. :
public void doit(Atom[] args);
public void doit(int[] args);
public void doit(int x);
doit(Atom[] args),
.
public void doit(int[] args);
public void doit(int x);
doit(int[] args),
.
2. :
public void doit(int x, int y);
public void doit(int x);
doit
public void doit(int x, int y). -
public void doit(int x);
Max, mxj
, ,
.

22

MaxJava API

3. Max.
Max Java-external .
Max external.
, external Max.

outlet
MaxObject, ,
Max. ,
, bang
2 bang :
public void bang()
{
outletBang(0);
outletBang(0);
}
outletBang ,
bang. - ,
0. , 3 , :
declareOutlets(new int[]{ DataTypes.INT, DataTypes.FLOAT,
DataTypes.ALL});
INT - 0, FLOAT -
1, ALL 2. ,
.
, outlet(int index, Atom[] args), index
. Atom API Atom
Java. , int
float, , bang
:
public void bang()
{
int a = 42;
int b = 38;
float c = 0.005F;
outlet(1,new Atom[]{Atom.newAtom(a),Atom.newAtom(b),
Atom.newAtom(c)});
}
- MaxObject
outlet. ,
, int
String. Java.

23

MaxJava API
, int,
:
public void bang()
{
outlet(0,42);
}
:
public void bang()
{
outlet(0,new Atom[]{Atom.newAtom(42)});
}

public void bang()


{
outlet(0,Atom.newAtom(42));
}
, .
Java, mxj.
public
public
public
public
public
public

void
void
void
void
void
void

outlet(int
outlet(int
outlet(int
outlet(int
outlet(int
outlet(int

index,
index,
index,
index,
index,
index,

boolean b);
char c);
short s);
String msg);
int[] i_array);
String[] str_array); .

,
, . ,
- [setdim 64 32] :
outlet(int idx,String msg,Atom[] args).
:
public void bang()
{
outlet(0,"setdim", new Atom[]{ Atom.newAtom(64),
Atom.newAtom(32)});
}
, , :
public void bang()
{
outlet(0, new Atom[]{Atom.newAtom("setdim"),
Atom.newAtom(64), Atom.newAtom(32)});
}
24

MaxJava API
,
(list),
:
public void firstway()
{
outlet(0,new Atom[]{Atom.newAtom(11.5), Atom.newAtom(23))
}
public void secondway()
{
outlet(0,"list", new Atom[]{Atom.newAtom(11.5),
Atom.newAtom(23)});
}

info-
MaxJava API
info-. , info-
Java-external. info-,
getInfoIdx
. , ,
dump info-:
public void dump()
{
//_frequency, _load, _stepsize private//
int info_idx = getInfoIdx();
outlet(info_idx, new Atom[]{Atom.newAtom("frequency"),
Atom.newAtom(_frequency));
outlet(info_idx, new Atom[]{Atom.newAtom("load"),
Atom.newAtom(_load)});
outlet(info_idx, new Atom[]{Atom.newAtom("stepsize"),
Atom.newAtom(_stepsize)});
}


MaxJava API,
. , Cycling 74 Jitter
.
externala.
, ,
. -
. -
mxj. MaxObject.

25

MaxJava API
Java-externala,
bang
bang:
import com.cycling74.max.*;
public class banger extends MaxObject
{
private int repeat; // bang
public banger()
// bang
{
repeat = 2; // , 2 bang
declareIO(1,1); // ,
declareAttribute("repeat"); // repeat
}
public void bang()
{
for(int i = 0; i < repeat; i++)
outletBang(0);
}
}
repeat , Java-external
- repeat int getrepeat.
bang 10 external
repeat 10,
getrepeat. info-,
repeat, (, repeat 20).
info- ( createInfoOutlet(false)),
.
, (
):
public void repeat(int val)
{
repeat = val;
}
public void getrepeat()
{
int info_idx = getInfoIdx();
if(info_idx == -1)
post("repeat "+repeat);
else
outlet(info_idx,repeat);
}
,
.
@, object box.
repeat object
box - [mxj banger @repeat 33] repeat
33. , repeat int
, bang.
26

MaxJava API
Java :
boolean, byte, char, short, int, long, float, double,
String,boolean[], byte[], char[], short[], int[], long[],
float[], double[], String[]
,
, ,
, . ,
, .
frequencies int[] ,
:
frequencies 1 33 4 55 66 77 2 1 66 78 99


.
,
,
.
, /
(attribute getter/setter), , mxj .
, ,
, , .
, :
import com.cycling74.max.*;
public class foo extends MaxObject
{
private static final double PI_OVER_180 = Math.PI / 180.0;
private static final double 180_OVER_PI = 180.0 / Math.PI;
private double angle;//
public foo()
{
angle = 0.0;
declareIO(1,1);
declareAttribute("angle","getangle","setangle");
}
private void setangle(double angle)
{
angle = angle * PI_OVER_180; //

}
private double getangle()
{
return angle * 180_OVER_PI; //
}
}
, ,
, - mxj.
27

MaxJava API
, java.awt.Point , x y,
.
import com.cycling74.max.*;
import java.awt.Point;
public class foo extends MaxObject
{
private Point coord;
public foo()
{
coord = new Point(0,0);
declareIO(1,1);
declareAttribute("coord","getcoord","setcoord");
}
private void setcoord(int x, int y)
{
coord.x = x;
coord.y = y;
}
private int[] coord_ret = new int[2];
private int[] getcoord()
{
coord_ret[0] = coord.x;
coord_ret[1] = coord.y;
return coord_ret;
}
}
, (getter/setter)
private. , public,
, Max.
, ,
mxj ?
- @-,
mxj pattr.
, (getter/setter)
.
declareAttribute, null
(getter/setter), .
declareAttribute("foo",null,"setfoo");// ,
//
declareAttribute("foo","getfoo",null);// ,
//
( ) html MaxObject.

28

MaxJava API

4. .
Max
, Max (main thread)
. MIDI-, ,
( metro), ,
. , , ,
. , -
bang - ,
, , .
, - bang,
external -
. , Max
, ( ) - .
, , Max
. , (
) : ,
, , . ,
,
,
.
Overdrive (Options->Overdrive),
Max , .
, Max
. (scheduler)
, ,
, .
, Max
,
( ).
,
,
. ,
( ) ,

. Java-external
, .
Java-
Max, MaxSystem. ,
MaxQelem, ,
.


Java- ,
, ,
, .

29

MaxJava API
, bang metro ,
bang, metro
. , ,
message box,
.
.

outletHigh,
.
, ,
Overdrive. , outletHigh
outlet.

MaxClock Callback
, , ,
, MaxClock API
. ,
com.cycling74.max.Executable.
, start
500 bang, stop
bang.
importcom.cycling74.max.*;
public class simplemetro extends MaxObject
{
MaxClock c;
public simplemetro()
{
declareIO(1,1); // ,
c = new MaxClock(new Callback(this, "dobang"));
// MaxClock,
// dobang , ""
}
private void dobang()
{
outletBang(0); // bang
c.delay(500.0);// 500
}
public void start()
{
c.delay(0.0); //
}
public void stop()
{
c.unset(); //
}

30

MaxJava API
// notifyDeleted Max,
//
// , .
public void notifyDeleted()
{
c.release(); //
//Max, MaxClock.
// . // .
}
}
Callback,
Executable , , .
Callback - ,
, ( this,
.. , Callback).
Callback, :
public simplemetro()
{
declareIO(1,1); //one inlet, one outlet
c = new MaxClock(new Executable(){
public void execute()
{
outletBang(0);
c.delay(500.0);
}
});
}
Callback .
Callback ,
Executable, Callback
Executable. , Callback ,
,
Callback.
, MaxClock ,
, .
MaxClock :
MaxClock c;
public simplemetro()
{
declareIO(1,1); // ,
// MaxClock
// dobang ""
c = new MaxClock(this, "dobang");
}
31

MaxJava API


MaxQelem ,
.
, . ,
( ,
),

. MaxQelem,
,
.
, , Java-external
, . ,
:
import com.cycling74.max.*;
public class qelemtest extends MaxObject
{
private MaxQelem q;
private double[] darray;
private double someParameter;
public qelemtest()
{
declareIO(1,1);
q = new MaxQelem(new Callback(this,"fillarray"));
}
private void fillarray()
{
double thisParameter = someParameter;
darray = new double[10000000];// 10
for(int i = 0; i < darray.length;i++)
{
darray[i] = Math.sin(System.currentTimeMillis()+ thisParameter);
}
}
public void inlet(double d)
{
someParameter = d;
q.set();
// fillarray
// . fillarray
// qelem-,
// MaxQelem q.
}
public void notifyDeleted()
{
q.release();//
//Max, MaxQelem.
// .
}
}
32

MaxJava API
, -
fillarray .

Java-
external Java,
Max
JVM. , Java-,
.
, bang
, , info- bang.
, Java-
MaxQelem, Max
bang,
.
import com.cycling74.max.*;
public class threadtest extends MaxObject
{
private double[] darray;
public threadtest()
{
declareIO(1,1);
darray = new double[1000000];
}
public void bang()
{
//
Thread t = new Thread(){
public void run()
{
for(int i = 0; i < darray.length;i++)
{
darray[i] = Math.sin(System.currentTimeMillis());
}
outletBang(getInfoIdx());// bang
// info-
}
};
t.start(); //
}
}

33

MaxJava API
Java- external,
Max,
,
outlet,
outletHigh. , Max
: . Java-
Max. , Java- ,
outletHigh,
.

34

MaxJava API

5. .

Java-external jit.cellblock,
( ).
rand
. ,
. : up,
down, right left. keyup.
cheat,
( , ).
Java-external
. MaxMSP 4.5.3.
import com.cycling74.max.*;
import java.util.Random;// Random
public class fifth_game extends MaxObject //Programmed by I.Tikhonov
{
private Random rnd = new Random();// Random
private int counter; //
private int zerox; // ,
private int zeroy; // ,
private int[][] matrix=new int[4][4];//
private final int[][] refer={{1,5,9,13},{2,6,10,14},{3,7,11,15},{4,8,12,0}};
// ( )
private Atom[] out=new Atom[3];// Atom[]
public fifth_game (){ //
declareInlets(new int[]{DataTypes.ALL}); //
declareOutlets(new int[]{DataTypes.ALL, DataTypes.ALL});//
createInfoOutlet(false);// info-
setInletAssist(new String[] {"input commands"});//
setOutletAssist(new String[] {"connect to jit.cellblock", "move number"});
//
}
public void rand(){//
counter=0;//
int init=0;//
for(int a=0; a<matrix.length; a++)//
for(int b=0; b<matrix.length; b++){
matrix[a][b]=init;// 0 15
init++;// 1
}
for(int i = 0;i<100;i++){// 0 100
int a = rnd.nextInt(4);// 0 3
int b = rnd.nextInt(4);// nextInt,
int a2 = rnd.nextInt(4);// ()
int b2 = rnd.nextInt(4);// ( )
int c = matrix[a][b];// 1
//
matrix[a][b] = matrix[a2][b2];// 2
// 1
matrix[a2][b2] = c; // 1 2
}
this.print();// print ( )
outlet(1, counter);//
}

35

MaxJava API
private void print(){// print jit.cellblock
outlet(0,"clear","all");// jit.cellblock
for(int i=0; i<matrix.length; i++)//
for(int j=0; j<matrix.length; j++){
if (matrix[i][j]==0) continue;//
// , jit.cellblock
out[0]=out[0].newAtom(i);//
// 0 out
out[1]=out[1].newAtom(j);// 1 out
out[2]=out[2].newAtom(matrix[i][j]);// 2 out
//
outlet(0, "set", out);// set,
// jit.cellblock
outlet(1, counter);//
}
//
//zerox zeroy
for(int d=0; d<matrix.length; d++)
for(int e=0; e<matrix.length; e++){
if(matrix[d][e]==0) {zerox=d;zeroy=e;}}
}
private void refer(){//
int error=0;//
for(int i=0; i<matrix.length; i++)//
for(int j=0; j<matrix.length; j++){
if(matrix[i][j]!=refer[i][j]) error++;//
// ,
//
}
if (error==0) {//
ouch("Congratulations, You Win!");//
this.rand();// !
}
}
public void up(){//
if(zeroy!=3){//
matrix[zerox][zeroy]=matrix[zerox][zeroy+1];//
matrix[zerox][zeroy+1]=0;//
zeroy++;//
counter++;//
this.print();// jit.cellblock
this.refer();//
}
}
public void down(){//
if(zeroy!=0){//
matrix[zerox][zeroy]=matrix[zerox][zeroy-1];//
matrix[zerox][zeroy-1]=0;//
zeroy--;//
counter++;//
this.print();// jit.cellblock
this.refer();//
}
}

36

MaxJava API
public void right(){//
if(zerox!=0){ //
matrix[zerox][zeroy]=matrix[zerox-1][zeroy]; //
matrix[zerox-1][zeroy]=0; //
zerox--;//
counter++;//
this.print();// jit.cellblock
this.refer();//
}
}
public void left(){//
if(zerox!=3){ //
matrix[zerox][zeroy]=matrix[zerox+1][zeroy]; //
matrix[zerox+1][zeroy]=0; //
zerox++;//
counter++;//
this.print();// jit.cellblock
this.refer();//
}
}
public void cheat(){//
int[][] cheater = {{1,5,9,13},{2,6,10,14},{3,7,11,0},{4,8,12,15}};
// ( )
matrix=cheater;//
this.print();// jit.cellblock
}
}

37

MaxJava API

pac
Java- pac Max pak pack.
( )
. pak pack (
) , pac ,
.
import com.cycling74.max.*;
public class pac extends MaxObject//Programmed by I.Tikhonov
{
private Atom[] outlet;// Atom,
private boolean[] reference;// outlet
private boolean flag = false;//, , outlet
//
public pac(int arg) // int,
//
{
int[] inlets_type=new int[arg];// -
for(int i=0; i<arg; i++) inlets_type[i]=DataTypes.ALL;//
//
String[] inlets_assist=new String [arg];// String
//
for(int j=0; j<arg; j++) inlets_assist[j]="inlet "+j;//
// +
declareInlets(inlets_type);//
declareOutlets(new int[]{DataTypes.ALL});//
setInletAssist(inlets_assist);//
//
setOutletAssist(new String[]{"combined list"});//
createInfoOutlet(false);// info-
outlet=new Atom[arg];// , -
// -
reference=new boolean[arg];// , -
// -
this.referfill();// referfill (. )
// false, .. outlet
// .
}
public pac()// .
//- , .
{
bail("wrong args");//
}
private void referfill()// false,
//.. outlet .
{
for(int i=0; i<reference.length;i++) reference[i]=false;//
// false
}

38

MaxJava API

private void print()//


{
int error=0;//
for(int i=0; i<reference.length; i++)// reference
if(reference[i]==false) error++;// reference
// false (.. - outlet )
//- 1
if(error==0) {outlet(0,outlet); this.referfill();flag=true;}//
//( outlet ), outlet
}
public void inlet(int i)//
{
int inlet=getInlet();//
outlet[inlet]=outlet[inlet].newAtom(i);//
// outlet. , ,
// ,
reference[inlet]=true;//, outlet
//
this.print();//
}
public void inlet(float f) //
{
int inlet=getInlet();//
outlet[inlet]=outlet[inlet].newAtom(f);//
//outlet. , ,
// ,
reference[inlet]=true; //, outlet
//
this.print();//
}
public void anything(String msg, Atom[] args)//
{
int inlet=getInlet();//
outlet[inlet]=outlet[inlet].newAtom(msg); //
// outlet. , ,
// ,
reference[inlet]=true; //, outlet
//
this.print();//
}
public void bang()// bang,
// (.. outlet).
// , outlet .
// flag, true,
// outlet
{
if(flag==true)// flag true(.. outlet )-
try{outlet(0, outlet);}// outlet.
//( ) NullPointerException (..
//outlet ),
catch(NullPointerException e)//
{post("error");}// Max
}
}

39

Оценить