TranslateRotateScale_demo

A demo to hopefully aid in the understanding of how these calls work. Controller source code will need to be placed under a separate tab.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

JFrame frame;

float rotX;
float rotY;
float rotZ;

float transX;
float transY;
float transZ;

float myScale = 1;

int XOffset = 0;
int hGap = 0;
int vGap = 0;

void setup() {
  size(800, 800, P3D);
  surface.setTitle("P3D TranslateRotateScale");
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      buildControlWnd(); // Builds controls on EventDispatchThread
    }
  }
  );
}

void draw() {
  background(209);
  translate(transX, transY, transZ);
  rotateX(radians(rotX));
  rotateY(radians(rotY));
  rotateZ(radians(rotZ));
  scale(myScale);
  boxGrid(12, 40, 15, 10, 15, 5, 5);
}

void boxGrid(int col, int row, int l, int t, int w, int h, int d) {
  fill(0, 255, 0);
  for (int i = 0; i < col; i++) {
    for (int j = 0; j < row; j++) {
      if (j % 2 == 0) {XOffset = 5;} else {XOffset = 0;}
      int left = l + i*(w + vGap) + XOffset;
      int top = t + j*(h + hGap);
      pushMatrix();
      translate(left, top);
      box(w, h, d);
      popMatrix();     
    }
  }
}

Controller: (separate tab)

void buildControlWnd() {
  frame = new JFrame("Controller Wnd");
  frame.setLayout(null);
  frame.setBounds(100, 100, 300, 440);

  // *** translate X **** //
  JSlider transXSlider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
  // **** Action **** //
  transXSlider.addChangeListener(new ChangeListener() {
    void stateChanged(ChangeEvent changeEvent) {
      transX = transXSlider.getValue();
    }
  }
  );
  transXSlider.setBounds(30, 30, 250, 24);

  // **** translate X label **** //
  JLabel transXLabel = new JLabel();
  transXLabel.setHorizontalAlignment(JLabel.LEFT);
  transXLabel.setBounds(60, 10, 150, 30);
  transXLabel.setText("translate X");

  frame.add(transXSlider);
  frame.add(transXLabel);
  transXLabel.repaint();

  // *** translate Y **** //
  JSlider transYSlider = new JSlider(JSlider.HORIZONTAL, 0, 500, 0);
  // **** Action **** //
  transYSlider.addChangeListener(new ChangeListener() {
    void stateChanged(ChangeEvent changeEvent) {
      transY = transYSlider.getValue();
    }
  }
  );
  transYSlider.setBounds(30, 80, 250, 24);

  // **** translate Y label **** //
  JLabel transYLabel = new JLabel();
  transYLabel.setHorizontalAlignment(JLabel.LEFT);
  transYLabel.setBounds(60, 60, 150, 30);
  transYLabel.setText("translate Y");

  frame.add(transYSlider);
  frame.add(transYLabel);
  transYLabel.repaint();

  // *** translate Z **** //
  JSlider transZSlider = new JSlider(JSlider.HORIZONTAL, -400, 400, 0);
  // **** Action **** //
  transZSlider.addChangeListener(new ChangeListener() {
    void stateChanged(ChangeEvent changeEvent) {
      transZ = transZSlider.getValue();
    }
  }
  );
  transZSlider.setBounds(30, 130, 250, 24);

  // **** translate Z label **** //
  JLabel transZLabel = new JLabel();
  transZLabel.setHorizontalAlignment(JLabel.LEFT);
  transZLabel.setBounds(60, 110, 150, 30);
  transZLabel.setText("translate Z");

  frame.add(transZSlider);
  frame.add(transZLabel);
  transZLabel.repaint();

  // *** rotateX **** //
  JSlider rotXSlider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
  // **** Action **** //
  rotXSlider.addChangeListener(new ChangeListener() {
    void stateChanged(ChangeEvent changeEvent) {
      rotX = rotXSlider.getValue();
    }
  }
  );
  rotXSlider.setBounds(30, 180, 250, 24);

  // **** rotateX label **** //
  JLabel rotXLabel = new JLabel();
  rotXLabel.setHorizontalAlignment(JLabel.LEFT);
  rotXLabel.setBounds(60, 160, 150, 30);
  rotXLabel.setText("rotate X");

  frame.add(rotXSlider);
  frame.add(rotXLabel);
  rotXLabel.repaint();

  // *** rotateY **** //
  JSlider rotYSlider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
  // **** Action **** //
  rotYSlider.addChangeListener(new ChangeListener() {
    void stateChanged(ChangeEvent changeEvent) {
      rotY = rotYSlider.getValue();
    }
  }
  );
  rotYSlider.setBounds(30, 230, 250, 24);

  // **** rotateY Label **** //
  JLabel rotYLabel = new JLabel();
  rotYLabel.setHorizontalAlignment(JLabel.LEFT);
  rotYLabel.setBounds(60, 210, 150, 30);
  rotYLabel.setText("rotate Y");

  frame.add(rotYSlider);
  frame.add(rotYLabel);
  rotYLabel.repaint();

  // *** rotateZ **** //
  JSlider rotZSlider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
  // **** Action **** //
  rotZSlider.addChangeListener(new ChangeListener() {
    void stateChanged(ChangeEvent changeEvent) {
      rotZ = rotZSlider.getValue();
    }
  }
  );
  rotZSlider.setBounds(30, 280, 250, 24);

  // **** rotateZ label **** //
  JLabel rotZLabel = new JLabel();
  rotZLabel.setHorizontalAlignment(JLabel.LEFT);
  rotZLabel.setBounds(60, 260, 150, 30);
  rotZLabel.setText("rotate Z");

  frame.add(rotZSlider);
  frame.add(rotZLabel);
  rotZLabel.repaint();

  // **** scale **** //
  JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL, 1, 5, 1);
  // **** Action **** //
  scaleSlider.addChangeListener(new ChangeListener() {
    void stateChanged(ChangeEvent changeEvent) {
      myScale = scaleSlider.getValue();
    }
  }
  );
  scaleSlider.setBounds(30, 330, 250, 24);

  // **** scale label **** //
  JLabel scaleLabel = new JLabel();
  scaleLabel.setHorizontalAlignment(JLabel.LEFT);
  scaleLabel.setBounds(60, 310, 150, 30);
  scaleLabel.setText("scale");

  frame.add(scaleSlider);
  frame.add(scaleLabel);
  scaleLabel.repaint();

  // **** Quit Button **** //
  JButton quitBtn = new JButton("Q");
  quitBtn.setBounds(250, 360, 40, 30);
  // **** Action **** //
  quitBtn.addActionListener( new ActionListener() {
    void actionPerformed(ActionEvent actionEvent) {
      exit();
    }
  }
  );

  frame.add(quitBtn);
  quitBtn.repaint();

  frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  frame.setVisible(true);
}

1 Like