Class ViewPanel

java.lang.Object
java.awt.Component
java.awt.Canvas
eu.svjatoslav.sixth.e3d.gui.ViewPanel
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class ViewPanel extends Canvas
AWT Canvas that provides a 3D rendering surface with built-in camera navigation.

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

Uses BufferStrategy for efficient page-flipping and tear-free rendering.

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 window
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
      The background color of the view.
  • Constructor Details

    • ViewPanel

      public ViewPanel()
      Creates a new view panel with default settings.
  • Method Details

    • getCamera

      public Camera getCamera()
      Returns the camera representing the viewer's position and orientation.
      Returns:
      the camera
    • 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(ViewPanel, int).
      Parameters:
      listener - the listener to add
      See Also:
    • getPreferredSize

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

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

      public Dimension getMaximumSize()
      Overrides:
      getMaximumSize in class Component
    • getRenderingContext

      public RenderingContext getRenderingContext()
      Returns the current rendering context for the active frame.
      Returns:
      the rendering context, or null if no frame is being rendered
    • getDeveloperTools

      public DeveloperTools getDeveloperTools()
      Returns the developer tools for this view panel.
      Returns:
      the developer tools
    • getDebugLogBuffer

      public DebugLogBuffer getDebugLogBuffer()
      Returns the debug log buffer for this view panel.
      Returns:
      the debug log buffer
    • getLightingManager

      public LightingManager getLightingManager()
      Returns the global lighting manager for the scene. Add light sources here to illuminate the world.
      Returns:
      the lighting manager
    • showDeveloperToolsPanel

      public void showDeveloperToolsPanel()
      Shows the developer tools panel, toggling it if already open. Called when F12 is pressed.
    • paint

      public void paint(Graphics g)
      Overrides:
      paint in class Canvas
    • update

      public void update(Graphics g)
      Overrides:
      update in class Canvas
    • 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.
    • getNumRenderThreads

      public int getNumRenderThreads()
      Returns the current number of render threads.
      Returns:
      the number of render threads
    • setNumRenderThreads

      public void setNumRenderThreads(int count)
      Sets the number of render threads. Takes effect on the next frame. The executor service is recreated lazily when the render loop detects the change.
      Parameters:
      count - number of render threads (must be at least 1)
    • 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