Class ViewPanel

All Implemented Interfaces:
ComponentListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class ViewPanel extends JPanel implements ComponentListener
Java Swing panel that provides a 3D rendering canvas with built-in camera navigation.

ViewPanel is the primary entry point for embedding the Sixth 3D engine into a Java Swing application. It manages the render loop, maintains a scene graph (ShapeCollection), and handles user input for camera navigation.

Quick start - creating a 3D view in a window:


 // Option 1: Use ViewFrame (creates a maximized JFrame for you)
 ViewFrame frame = new ViewFrame();
 ViewPanel viewPanel = frame.getViewPanel();

 // Option 2: Embed ViewPanel in your own Swing layout
 JFrame frame = new JFrame("My 3D App");
 ViewPanel viewPanel = new ViewPanel();
 frame.add(viewPanel);
 frame.setSize(800, 600);
 frame.setVisible(true);

 // Add shapes to the scene
 ShapeCollection scene = viewPanel.getRootShapeCollection();
 scene.addShape(new WireframeCube(
     new Point3D(0, 0, 200), 50,
     new LineAppearance(5, Color.GREEN)
 ));

 // Position the camera
 viewPanel.getCamera().setLocation(new Point3D(0, 0, -100));

 // Listen for frame updates (e.g., for animations)
 viewPanel.addFrameListener((panel, deltaMs) -> {
     // Called before each frame. Return true to force repaint.
     return false;
 });
 

Architecture:

  • A background render thread continuously generates frames at the target FPS
  • The engine intelligently skips rendering when no visual changes are detected
  • FrameListeners are notified before each potential frame, enabling animations
  • Mouse/keyboard input is managed by InputManager
  • Keyboard focus is managed by KeyboardFocusStack
See Also:
  • Field Details

    • backgroundColor

      public Color backgroundColor
  • Constructor Details

    • ViewPanel

      public ViewPanel()
  • Method Details

    • getCamera

      public Camera getCamera()
      Returns the camera that represents the viewer's position and orientation in the 3D world. Use this to programmatically move the camera.
      Returns:
      the camera for this view
    • getKeyboardFocusStack

      public KeyboardFocusStack getKeyboardFocusStack()
      Returns the keyboard focus stack, which manages which component receives keyboard input.
      Returns:
      the keyboard focus stack
    • getRootShapeCollection

      public ShapeCollection getRootShapeCollection()
      Returns the root shape collection (scene graph). Add your 3D shapes here to make them visible in the view.
      
       viewPanel.getRootShapeCollection().addShape(myShape);
       
      Returns:
      the root shape collection
    • getInputManager

      public InputManager getInputManager()
      Returns the input manager handling mouse and keyboard events for this view.
      Returns:
      the input manager
    • addFrameListener

      public void addFrameListener(FrameListener listener)
      Registers a listener that will be notified before each frame render. Listeners can trigger repaints by returning true from FrameListener.onFrame(eu.svjatoslav.sixth.e3d.gui.ViewPanel, int).
      Parameters:
      listener - the listener to add
      See Also:
    • componentHidden

      public void componentHidden(ComponentEvent e)
      Specified by:
      componentHidden in interface ComponentListener
    • componentMoved

      public void componentMoved(ComponentEvent e)
      Specified by:
      componentMoved in interface ComponentListener
    • componentResized

      public void componentResized(ComponentEvent e)
      Specified by:
      componentResized in interface ComponentListener
    • componentShown

      public void componentShown(ComponentEvent e)
      Specified by:
      componentShown in interface ComponentListener
    • getMaximumSize

      public Dimension getMaximumSize()
      Overrides:
      getMaximumSize in class JComponent
    • getMinimumSize

      public Dimension getMinimumSize()
      Overrides:
      getMinimumSize in class JComponent
    • getPreferredSize

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent
    • getRenderingContext

      public RenderingContext getRenderingContext()
    • repaintDuringNextViewUpdate

      public void repaintDuringNextViewUpdate()
      Calling these methods tells 3D engine that current 3D view needs to be repainted on first opportunity.
    • setFrameRate

      public void setFrameRate(int frameRate)
      Set target frames per second rate for this view. Target FPS can be changed at runtime. Use 0 or negative value for unlimited FPS (max performance mode for benchmarking).
      Parameters:
      frameRate - target frames per second rate for this view.
    • stop

      public void stop()
      Stops rendering of this view.
    • removeFrameListener

      public void removeFrameListener(FrameListener frameListener)
      Removes a previously registered frame listener.
      Parameters:
      frameListener - the listener to remove