# How to convert Java class to Python

**URL:** <https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729>\
**Category:** Ignition\
**Created:** [January 25, 2018, 11:50am UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729 "2018-01-25T11:50:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [January 25, 2018, 11:50am UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/1 "2018-01-25T11:50:03Z")

</div>

Can someone tell me how to convert this java classes to python:  
Java:

```auto
class Vertex {
    double x;
    double y;
    double z;
    Vertex(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

class Triangle {
    Vertex v1;
    Vertex v2;
    Vertex v3;
    Color color;
    Triangle(Vertex v1, Vertex v2, Vertex v3, Color color) {
        this.v1 = v1;
        this.v2 = v2;
        this.v3 = v3;
        this.color = color;
    }
}

class Matrix3 {
    double[] values;
    Matrix3(double[] values) {
        this.values = values;
    }
    Matrix3 multiply(Matrix3 other) {
        double[] result = new double[9];
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                for (int i = 0; i < 3; i++) {
                    result[row * 3 + col] +=
                        this.values[row * 3 + i] * other.values[i * 3 + col];
                }
            }
        }
        return new Matrix3(result);
    }
    Vertex transform(Vertex in) {
        return new Vertex(
            in.x * values[0] + in.y * values[3] + in.z * values[6],
            in.x * values[1] + in.y * values[4] + in.z * values[7],
            in.x * values[2] + in.y * values[5] + in.z * values[8]
        );
    }
}

```

I’ve done this, but there is something wrong with transform func.

```auto
class Vertex():
	def __init__ (self, x=0, y=0, z=0):
		self.x = x
		self.y = y
		self.z = z
		print "Vertex created"

class Triangle(Vertex):
	def __init__ (self, v1, v2, v3, color=0):
		self.v1 = v1
		self.v2 = v2
		self.v3 = v3
		self.color = color

class Matrix3(object):
	def __init__ (self, values):
		self._values = values

	def multiply(self, other):
		result = Array.CreateInstance(Double, 9)
		row = 0
		while row < 3:
			col = 0
			while col < 3:
				i = 0
				while i < 3:
					result[row * 3 + col] += self._values[row * 3 + i] * other.values[i * 3 + col]
					i += 1
				col += 1
			row += 1
		return Matrix3(result)

	def transform(self, Vertex):
		return Vertex(Vertex.x * self._values[0] + Vertex.y * self._values[3] + Vertex.z * self._values[6], 
		Vertex.x * self._values[1] + Vertex.y * self._values[4] + Vertex.z * self._values[7], 
		Vertex.x * self._values[2] + Vertex.y * self._values[5] + Vertex.z * self._values[8])

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 25, 2018, 1:54pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/2 "2018-01-25T13:54:44Z")

</div>

I haven’t looked closely, but why is the python `Triangle` _inheriting_ `Vertex`? I would expect all of the python classes to inherit from `object`.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 25, 2018, 2:20pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/3 "2018-01-25T14:20:19Z")

</div>

Your transform function declares Vertex as a parameter, but also tries to use it as a class name. Try it with `def transform(self, in):`.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [January 25, 2018, 2:43pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/4 "2018-01-25T14:43:45Z")

</div>

When I try your suggestion I get this:

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/3a8ba84c2b83c7cb3dc5941ecc9d594d2742a33b.PNG)  
This is my code inside Paintable Canvas paint.repaint:

```auto
from java.awt import Color
from java.awt import GradientPaint
from java.awt.geom import GeneralPath
from java.awt.geom import Rectangle2D
from java.awt.geom import Ellipse2D
import math

class Vertex(object):
	def __init__ (self, x=0, y=0, z=0):
		self.x = x
		self.y = y
		self.z = z
		print "Vertex created"

class Triangle(object):
	def __init__ (self, v1, v2, v3, color=0):
		self.v1 = v1
		self.v2 = v2
		self.v3 = v3
		self.color = color

class Matrix3(object):
	def __init__ (self, values):
		self._values = values

	def multiply(self, other):
		result = Array.CreateInstance(Double, 9)
		row = 0
		while row < 3:
			col = 0
			while col < 3:
				i = 0
				while i < 3:
					result[row * 3 + col] += self._values[row * 3 + i] * other.values[i * 3 + col]
					i += 1
				col += 1
			row += 1
		return Matrix3(result)

	def transform(self, in):
		result = Vertex(self, in.x * self._values[0] + in.y * self._values[3] + in.z * self._values[6], 
				in.x * self._values[1] + in.y * self._values[4] + in.z * self._values[7], 
				in.x * self._values[2] + in.y * self._values[5] + in.z * self._values[8])
		return result

#=============================================================================
tris = []
tris.append(Triangle(Vertex(100, 100, 100), Vertex(-100, -100, 100), Vertex(-100, 100, -100), Color.WHITE))
tris.append(Triangle(Vertex(100, 100, 100), Vertex(-100, -100, 100), Vertex(100, -100, -100), Color.RED))
tris.append(Triangle(Vertex(-100, 100, -100), Vertex(100, -100, -100), Vertex(100, 100, 100), Color.GREEN))
tris.append(Triangle(Vertex(-100, 100, -100), Vertex(100, -100, -100), Vertex(-100, -100, 100), Color.BLUE))		
print tris

heading = math.radians(event.source.parent.getComponent('SliderH').value);
transform = Matrix3([math.cos(heading),0,-math.sin(heading),
					0,1,0,
					math.sin(heading),0,math.cos(heading)])
print "transform=",transform
g = event.graphics

g.translate(event.width / 2, event.width / 2)
g.setColor(Color.WHITE)
for t in tris:
	print t.v1,t.v2,t.v3
	v1 = transform.transform(t.v1)
	v2 = transform.transform(t.v2)
	v3 = transform.transform(t.v3)
	path = GeneralPath()
	path.moveTo(v1.x, v1.y)
	path.lineTo(v2.x, v2.y)
	path.lineTo(v3.x, v3.y)
	path.closePath()
	g.draw(path)

```

Can you make it work?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 25, 2018, 2:54pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/5 "2018-01-25T14:54:55Z")

</div>

Oops. "in’ is a reserved word in python. Use a different parameter name.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [January 25, 2018, 2:56pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/6 "2018-01-25T14:56:09Z")

</div>

I tried that too, but then I get:

```auto
Caused by: org.python.core.PyException: Traceback (most recent call last):
  File "<event:repaint>", line 65, in <module>
  File "<event:repaint>", line 41, in transform
NameError: global name 'Vertex' is not defined

```

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [January 25, 2018, 3:29pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/7 "2018-01-25T15:29:24Z")

</div>

[CanvasSample.proj](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/3a77b49cd597807efa11100c24588228af443501.proj) (23.4 KB)

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [January 25, 2018, 5:10pm UTC](https://forum.inductiveautomation.com/t/how-to-convert-java-class-to-python/16729/8 "2018-01-25T17:10:14Z")

</div>

Thank you very much! 👏👍
