A simple frame that allows quick and easy visualisation of something : Debug « Development Class « Java






A simple frame that allows quick and easy visualisation of something

        

//package com.ryanm.util.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 * A simple frame that allows quick and easy visualisation of
 * something
 * 
 * @author ryanm
 */
public class DebugFrame extends JFrame
{
  private DebugPanel panel;

  /**
   * Creates a new Debug frame
   * 
   * @param name
   *           The name for the frame
   * @param x
   *           The width
   * @param y
   *           The height
   */
  public DebugFrame( String name, int x, int y )
  {
    setTitle( "Debug Frame : " + name );

    panel = new DebugPanel( x, y );

    getContentPane().add( panel );

    clear();

    pack();
    setVisible( true );
  }

  /**
   * Creates a new Debug frame
   * 
   * @param x
   *           The width
   * @param y
   *           The height
   */
  public DebugFrame( int x, int y )
  {
    setTitle( "Debug Frame" );

    panel = new DebugPanel( x, y );

    getContentPane().add( panel );

    clear();

    pack();
    setVisible( true );
  }

  /**
   * Gets the graphics for the backing image. Stuff drawn on this
   * context can only be cleared with a call to clear()
   * 
   * @return A {@link Graphics2D} object
   */
  public Graphics2D getDebugGraphics()
  {
    Graphics2D g = panel.buff.createGraphics();
    return g;
  }

  /**
   * Gets the graphics for the frame itself. Stuff drawn to this
   * context will be cleared when refresh() is called
   * 
   * @return A {@link Graphics2D} object
   */
  public Graphics2D getTempDebugGraphics()
  {
    Graphics2D g = ( Graphics2D ) panel.getGraphics();
    return g;
  }

  /**
   * Refreshes the frame by drawing the contents of the back buffer
   */
  public void refresh()
  {
    panel.paint( panel.getGraphics() );
  }

  /**
   * Clears the back buffer
   */
  public void clear()
  {
    Graphics g = panel.buff.getGraphics();

    g.setColor( Color.white );
    g.fillRect( 0, 0, panel.getWidth(), panel.getHeight() );
  }

  private class DebugPanel extends JComponent
  {
    private int x, y;

    private BufferedImage buff;

    private DebugPanel( int x, int y )
    {
      this.x = x;
      this.y = y;
      buff = new BufferedImage( x, y, BufferedImage.TYPE_INT_ARGB );
    }

    @Override
    public void paint( Graphics g )
    {
      g.drawImage( buff, 0, 0, this );
    }

    @Override
    public int getWidth()
    {
      return x;
    }

    @Override
    public int getHeight()
    {
      return y;
    }

    @Override
    public Dimension getPreferredSize()
    {
      return new Dimension( x, y );
    }
  }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.A simple logging facility.
2.Debug Utilities
3.Debug InputStream
4.Methods for printing Debug messages
5.Trace InputStream
6.Trace OutputStream
7.Debug Utility
8.Debugging utility that reports, in a brute force manner, any internal data of a class instance
9.Swing Console
10.How to do Benchmark
11.Methods for logging events
12.Printing indented text
13.Prints messages formatted for a specific line width.
14.Class providing static methods to log diagnostics
15.A bean that can be used to keep track of a counter
16.An integer synchronized counter class.
17.Counts down from a specified value the number of bytes actually read from the wrapped InputStream.
18.A long integer counter class
19.Logging class to record errors or unexpected behavior to a file
20.Handle obtaining string timestamps
21.Scans java source files in cvs tree and validates the license header
22.Debug Util
23.Array debug util