KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > object > Square


1 /*
2  * Copyright 2005 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.ensmer.object;
18
19 import javax.media.j3d.*;
20
21 /**
22  * A shape3D with a geometry modelling a square of a particular
23  * width and height.
24  *
25  * @author Dusty Phillips [dusty@buchuki.com]
26  */

27 public class Square extends Shape3D {
28     
29     /**
30      * Creates a new instance of Square with default width and height
31      */

32     public Square() {
33         this(1f, 1f);
34     }
35     
36     /**
37      * Create a new instance of Square with a specific width and height
38      *
39      * @param width the width of the square
40      * @param height the height of the square
41      */

42     public Square(float width, float height) {
43         QuadArray quad = new QuadArray(4, QuadArray.COORDINATES | QuadArray.NORMALS | QuadArray.TEXTURE_COORDINATE_2);
44         quad.setCapability(quad.ALLOW_COORDINATE_WRITE);
45         setCapability(ALLOW_GEOMETRY_READ);
46         setGeometry(quad);
47         setSize(width, height);
48         quad.setNormals(0, new float[] {
49             0, 0, 1,
50             0, 0, 1,
51             0, 0, 1,
52             0, 0, 1
53         });
54         quad.setTextureCoordinates(0, 0, new float[] {
55             0, 1,
56             0, 0,
57             1, 0,
58             1, 1
59         });
60         Appearance app = new Appearance();
61         PolygonAttributes polyAtt = new PolygonAttributes();
62         polyAtt.setCullFace(polyAtt.CULL_NONE);
63         polyAtt.setBackFaceNormalFlip(true);
64         app.setPolygonAttributes(polyAtt);
65         setAppearance(app);
66     }
67     
68     /**
69      * Set the size of the square to new values
70      *
71      * @param width the width of the square
72      * @param height the height of the square
73      */

74     public void setSize(float width, float height) {
75         QuadArray quad = (QuadArray) getGeometry();
76         float w = width / 2;
77         float h = height / 2;
78         quad.setCoordinates(0, new float[] {
79             -w, h, 0,
80             -w, -h, 0,
81              w, -h, 0,
82              w, h, 0
83         });
84     }
85 }
86
Popular Tags