~ubuntu-branches/ubuntu/gutsy/cairo-java/gutsy

« back to all changes in this revision

Viewing changes to src/java/org/freedesktop/cairo/Matrix.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-04-16 16:15:30 UTC
  • Revision ID: james.westby@ubuntu.com-20060416161530-avdw6l5vg6n2kosy
Tags: upstream-1.0.3
ImportĀ upstreamĀ versionĀ 1.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Java-Gnome Bindings Library
 
3
 *
 
4
 * Copyright 1998-2005 the Java-Gnome Team, all rights reserved.
 
5
 *
 
6
 * The Java-Gnome bindings library is free software distributed under
 
7
 * the terms of the GNU Library General Public License version 2.
 
8
 */
 
9
package org.freedesktop.cairo;
 
10
 
 
11
import org.gnu.glib.Struct;
 
12
import org.gnu.glib.Handle;
 
13
 
 
14
/**
 
15
 * TODO: error handling
 
16
 */
 
17
public class Matrix extends CairoObject {
 
18
 
 
19
    Matrix(Handle hndl) {
 
20
        super(hndl);
 
21
    }
 
22
 
 
23
    /**
 
24
     * Creates a new matrix initialized with a noop transform.
 
25
     */
 
26
        public Matrix() {
 
27
                super(new_identity_matrix());
 
28
        }
 
29
 
 
30
    /**
 
31
     * Disposes all the native resources used by the matrix.
 
32
     */
 
33
    protected void finalize() throws Throwable {
 
34
        free_matrix(getHandle());
 
35
        super.finalize();
 
36
    }
 
37
 
 
38
    /**
 
39
     * Sets the matrix to be the affine transformation given by xx, yx, xy, yy,
 
40
     * x0, y0. The transformation is given by: <code> 
 
41
     *  x_new = xx * x + xy * y + x0;
 
42
     *  y_new = yx * x + yy * y + y0;
 
43
     *  </code>
 
44
     */
 
45
    public void init(double xx, double yx, double xy, double yy, double x0,
 
46
            double y0) {
 
47
        cairo_matrix_init(getHandle(), xx, yx, xy, yy, x0, y0);
 
48
    }
 
49
 
 
50
    /**
 
51
     * Modifies the matrix to be an identity transformation.
 
52
     */
 
53
    public void initIdentity() {
 
54
        cairo_matrix_init_identity(getHandle());
 
55
    }
 
56
 
 
57
    /**
 
58
     * Initializes the matrix to a transformation that translates by tx and ty
 
59
     * in the X and Y dimensions, respectively.
 
60
     * 
 
61
     * @param tx
 
62
     *            amount to translate in the X direction.
 
63
     * @param ty
 
64
     *            amount to translate in the Y direction.
 
65
     */
 
66
    public void initTranslate(double tx, double ty) {
 
67
        cairo_matrix_init_translate(getHandle(), tx, ty);
 
68
    }
 
69
 
 
70
    /**
 
71
     * Initializes the matrix to a transformation that scales by sx and sy in
 
72
     * the X and Y dimensions, respectively.
 
73
     * 
 
74
     * @param sx
 
75
     *            scale factor in the X direction.
 
76
     * @param sy
 
77
     *            scale factor in the Y direction.
 
78
     */
 
79
    public void initScale(double sx, double sy) {
 
80
        cairo_matrix_init_scale(getHandle(), sx, sy);
 
81
    }
 
82
 
 
83
    /**
 
84
     * Initialized the matrix to a transformation that rotates by
 
85
     * 
 
86
     * @radians.
 
87
     * 
 
88
     * @param radians
 
89
     *            angle of rotation, in radians. The direction of rotation is
 
90
     *            defined such that positive angles rotate in the direction from
 
91
     *            the positive X axis toward the positive Y axis. With the
 
92
     *            default axis orientation of cairo, positive angles rotate in a
 
93
     *            clockwise direction.
 
94
     */
 
95
    public void initRotate(double radians) {
 
96
        cairo_matrix_init_rotate(getHandle(), radians);
 
97
    }
 
98
 
 
99
    /**
 
100
     * Appends a transaltion transformation to this matrix.
 
101
     * 
 
102
     * @param tx
 
103
     *            X axis translation
 
104
     * @param ty
 
105
     *            Y axis translation
 
106
     */
 
107
    public void translate(double tx, double ty) {
 
108
        cairo_matrix_translate(getHandle(), tx, ty);
 
109
    }
 
110
 
 
111
    /**
 
112
     * Appends non-uniform scaling to this matrix.
 
113
     * 
 
114
     * @param sx
 
115
     *            X axis scaling factor
 
116
     * @param sy
 
117
     *            Y axis scaling factor
 
118
     */
 
119
    public void scale(double sx, double sy) {
 
120
        cairo_matrix_scale(getHandle(), sx, sy);
 
121
    }
 
122
 
 
123
    /**
 
124
     * Appends rotation transformation to this matrix.
 
125
     * 
 
126
     * @param radians
 
127
     *            The rotation angle in radians.
 
128
     */
 
129
    public void rotate(double radians) {
 
130
        cairo_matrix_rotate(getHandle(), radians);
 
131
    }
 
132
 
 
133
    /**
 
134
     * Inverts this matrix.
 
135
     */
 
136
    public void invert() {
 
137
        cairo_matrix_invert(getHandle());
 
138
    }
 
139
 
 
140
    /**
 
141
     * Multiplies 2 matrices and returns the result.
 
142
     * 
 
143
     * @param a
 
144
     *            first matrix
 
145
     * @param b
 
146
     *            second matrix
 
147
     * @return The product
 
148
     */
 
149
    static public Matrix multiply(Matrix a, Matrix b) {
 
150
        Handle hndl = cairo_matrix_multiply(a.getHandle(), b.getHandle());
 
151
        return new Matrix(hndl);
 
152
    }
 
153
 
 
154
    /**
 
155
     * Transforms the given distance and returns transformed co-ordinates
 
156
     */
 
157
    public Point transformDistance(Point distance) {
 
158
        double[] dx = new double[] { distance.getX() };
 
159
        double[] dy = new double[] { distance.getY() };
 
160
        cairo_matrix_transform_distance(getHandle(), dx, dy);
 
161
        return new Point(dx[0], dy[0]);
 
162
    }
 
163
 
 
164
    /**
 
165
     * Transforms the given point and returns transformed co-ordinates
 
166
     */
 
167
    public Point transformPoint(Point point) {
 
168
        double[] dx = new double[] { point.getX() };
 
169
        double[] dy = new double[] { point.getY() };
 
170
        cairo_matrix_transform_distance(getHandle(), dx, dy);
 
171
        return new Point(dx[0], dy[0]);
 
172
    }
 
173
 
 
174
    public double getXX() {
 
175
        return getXX(getHandle());
 
176
    }
 
177
 
 
178
    public void setXX(double xx) {
 
179
        setXX(getHandle(), xx);
 
180
    }
 
181
 
 
182
    public double getYX() {
 
183
        return getYX(getHandle());
 
184
    }
 
185
 
 
186
    public void setYX(double yx) {
 
187
        setYX(getHandle(), yx);
 
188
    }
 
189
 
 
190
    public double getXY() {
 
191
        return getXY(getHandle());
 
192
    }
 
193
 
 
194
    public void setXY(double xy) {
 
195
        setXY(getHandle(), xy);
 
196
    }
 
197
 
 
198
    public double getYY() {
 
199
        return getYY(getHandle());
 
200
    }
 
201
 
 
202
    public void setYY(double yy) {
 
203
        setYY(getHandle(), yy);
 
204
    }
 
205
 
 
206
    public double getX0() {
 
207
        return getX0(getHandle());
 
208
    }
 
209
 
 
210
    public void setX0(double x0) {
 
211
        setX0(getHandle(), x0);
 
212
    }
 
213
 
 
214
    public double getY0() {
 
215
        return getY0(getHandle());
 
216
    }
 
217
 
 
218
    public void setY0(double y0) {
 
219
        setY0(getHandle(), y0);
 
220
    }
 
221
 
 
222
    /*
 
223
     * Native calls
 
224
     */
 
225
    native static final private double getXX(Handle matrix);
 
226
 
 
227
    native static final private double getYX(Handle matrix);
 
228
 
 
229
    native static final private double getXY(Handle matrix);
 
230
 
 
231
    native static final private double getYY(Handle matrix);
 
232
 
 
233
    native static final private double getX0(Handle matrix);
 
234
 
 
235
    native static final private double getY0(Handle matrix);
 
236
 
 
237
    native static final private void setXX(Handle matrix, double xx);
 
238
 
 
239
    native static final private void setYX(Handle matrix, double yx);
 
240
 
 
241
    native static final private void setXY(Handle matrix, double xy);
 
242
 
 
243
    native static final private void setYY(Handle matrix, double yy);
 
244
 
 
245
    native static final private void setX0(Handle matrix, double x0);
 
246
 
 
247
    native static final private void setY0(Handle matrix, double y0);
 
248
 
 
249
    native static final private Handle new_identity_matrix();
 
250
 
 
251
    native static final private void free_matrix(Handle matrix);
 
252
 
 
253
    native static final private void cairo_matrix_init(Handle matrix,
 
254
            double xx, double yx, double xy, double yy, double x0, double y0);
 
255
 
 
256
    native static final private void cairo_matrix_init_identity(Handle matrix);
 
257
 
 
258
    native static final private void cairo_matrix_init_translate(Handle matrix,
 
259
            double tx, double ty);
 
260
 
 
261
    native static final private void cairo_matrix_init_scale(Handle matrix,
 
262
            double sx, double sy);
 
263
 
 
264
    native static final private void cairo_matrix_init_rotate(Handle matrix,
 
265
            double radians);
 
266
 
 
267
    native static final private void cairo_matrix_translate(Handle matrix,
 
268
            double tx, double ty);
 
269
 
 
270
    native static final private void cairo_matrix_scale(Handle matrix,
 
271
            double sx, double sy);
 
272
 
 
273
    native static final private void cairo_matrix_rotate(Handle matrix,
 
274
            double radians);
 
275
 
 
276
    native static final private void cairo_matrix_invert(Handle matrix);
 
277
 
 
278
    native static final private Handle cairo_matrix_multiply(Handle a,
 
279
            Handle b);
 
280
 
 
281
    native static final private void cairo_matrix_transform_distance(
 
282
            Handle handle, double[] dx, double[] dy);
 
283
 
 
284
    native static final private void cairo_matrix_transform_point(
 
285
            Handle handle, double[] x, double[] y);
 
286
 
 
287
}