Commit 83d7f999 authored by Daniel Vogel's avatar Daniel Vogel

improved SceneGraph demo

parent f61439cf
/*
* CS 349 Java Code Examples
*
* SceneGraph Demonstrate simple scene graph.
* SceneGraph Demonstrate simple scene graph that draws a house.
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.*;
public class SceneGraph extends JPanel {
......@@ -18,45 +20,93 @@ public class SceneGraph extends JPanel {
f.setSize(400, 400); // window size
f.setContentPane(new SceneGraph()); // add canvas to jframe
f.setVisible(true); // show the window
}
SceneGraph() {
setOpaque(true);
setBackground(Color.WHITE);
// make sure panel is focusable for getting key events
// (remember focus dispatch?)
setFocusable(true);
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_RIGHT:
rotateBy += 5;
break;
case KeyEvent.VK_LEFT:
rotateBy -= 5;
break;
case KeyEvent.VK_UP:
scaleBy += .1;
break;
case KeyEvent.VK_DOWN:
scaleBy -= .1;
break;
}
repaint();
}
});
}
// scaleBy and rotation
double scaleBy = 2;
double rotateBy = 0;
// custom graphics drawing
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.translate(65, 65);
g2.scale(0.5, 0.5);
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 6; i++) {
drawShape(g2);
g2.translate(110, 0);
}
g2.translate(-6 * 110, 110);
}
// draw the house in centre of screen
g2.translate(getWidth() / 2, getHeight()/ 2);
g2.rotate(Math.toRadians(rotateBy));
g2.scale(scaleBy, scaleBy);
g2.setStroke(new BasicStroke(3));
g2.drawPolygon(houseShape.xpoints, houseShape.ypoints, houseShape.npoints);
// save transform for later
AffineTransform save = g2.getTransform();
// remember: these are all in "house coordinates"
g2.translate(-25, 0); // window centred 25 px
g2.scale(0.4, 0.4); // window is 40% house width
drawWindow(g2);
// translate to right 50 px
g2.translate(50 / 0.4, 0);
drawWindow(g2);
// draw third window twice to demo different coordinate frames
// set transform to saved matrix to return to "House" coordinates
// since this window is drawn in house coordinates, it will be
// transformed with the house
g2.setTransform(save);
drawWindow(g2, 0, -50, 45, 0.25);
// set transform to identity to reset
// this means third window is drawn in World Coordinates
// and won't be transformed with the house
g2.setTransform(new AffineTransform());
// model to world
g2.translate(200, 210);
g2.rotate(Math.toRadians(45));
g2.scale(1.25, 1.25);
drawShape(g2);
// using function
drawShape(g2, 200, 300, 45, 0.9);
// using function which has a model-to-world transform built in
drawWindow(g2, getWidth()/2, 89, 45, 0.5);
}
// 100 x 100 house shape using Java Polygon structure
// (model position is centred at top left corner)
private Polygon houseShape = new Polygon(new int[] { -50, 50, 50, 0, -50},
new int[] { 75, 75, -25, -75, -25}, 5);
// draws 100 x 100 window shape centred at 0,0
void drawShape(Graphics2D g2) {
void drawWindow(Graphics2D g2) {
g2.setColor(Color.BLACK);
g2.fillRect(-50, -50, 100, 100);
g2.setColor(Color.WHITE);
......@@ -67,21 +117,19 @@ public class SceneGraph extends JPanel {
}
// draws 100 x 100 window shape centred at 0,0
void drawShape(Graphics2D g2, double x, double y, double theta, double s) {
void drawWindow(Graphics2D g2, double x, double y, double theta, double s) {
// save the current g2 transform matrix
AffineTransform M = g2.getTransform();
// set transform to identity to reset
g2.setTransform(new AffineTransform());
AffineTransform save = g2.getTransform();
// do the model to world transformation
g2.translate(x, y); // T
g2.rotate(Math.toRadians(theta)); // R
g2.scale(s, s); // S
drawShape(g2);
// draws 100 x 100 window centred at 0,0
drawWindow(g2);
// reset the transform to what it was before we drew the shape
g2.setTransform(M);
g2.setTransform(save);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment