Package utils

Source Code of utils.JavaWindow

package utils;

import graphics.common.Size;
import graphics.common.Size2D;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class JavaWindow extends JFrame {
   
  private static final long serialVersionUID = 97762672127776908L;
  private static Graphics2D mGraphics;
  private Size mWindowSize;
  private static Canvas mCanvas;
  private BufferStrategy mStrategy;
  private Color mBackground;
 
  private static final int NUM_BUFFERS = 2;
 
  public JavaWindow( String windowName, Size windowSize ) {
    makeWindow( windowName, windowSize );
  }
 
  public JavaWindow( String windowName, int width, int height)
  {
    makeWindow( windowName, new Size2D( width, height ) );
  }
 
  /**
   * Instantiates the window with the passed parameters.
   * @param windowName The name the window will display.
   * @param windowSize An instance of Size describing the width and height of
   * the window.
   */
  private void makeWindow(String windowName, Size windowSize )
  {
    mWindowSize = windowSize;
    mBackground = Color.white;
   
    setName( windowName );
    setIgnoreRepaint( true );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   
    mCanvas = new Canvas();
    mCanvas.setIgnoreRepaint( true );
    mCanvas.setSize( mWindowSize.getWidth(), mWindowSize.getHeight() );
   
    add( mCanvas );
    pack();
    setVisible( true );
   
    mCanvas.createBufferStrategy( NUM_BUFFERS );
    mStrategy = mCanvas.getBufferStrategy();
   
    //Check for hardware acceleration (I think)
    //System.out.println( mCanvas.getGraphicsConfiguration().getBufferCapabilities().isPageFlipping() );
  }
 
  /**
   * Clears the screen for drawing more stuff.<br/>
   *
   * As a side effect of how Window works, this MUST be called before using pretty
   * much any other method, as this is the only method that gets the current
   * Graphics2D instance. Otherwise, the other method will attempt to return/use
   * a null pointer.
   */
  public void clear() {
    mGraphics = (Graphics2D)mStrategy.getDrawGraphics();
    mGraphics.setColor( mBackground );
    mGraphics.fillRect( 0, 0, getWidth(), getHeight() );
  }
 
  /**
   * Updates the canvas with anything that has been drawn to the screen using
   * the current Graphics instance since the canvas was last cleared. Disposes
   * of the graphics instance and set it to null, so clear must be called again
   * before using more methods.
   */
  public void update() {
    mGraphics.dispose();
    mGraphics = null;
    mStrategy.show();
  }
 
  /**
   * Gets the current Graphics instance as a Graphics2D instance.
   * @return The current Graphics2D isntance.
   */
  public static Graphics2D getGraphics2D()
  {
    return mGraphics;
  }
 
  public static GraphicsConfiguration getGraphicsConfig() {
    return mCanvas.getGraphicsConfiguration();
  }
 
  /**
   * Simulates closing the window down via pressing the close window button
   * on your operating system. Will shut down the program entirely.
   */
  public void shutdown() {
    processWindowEvent( new WindowEvent( this, WindowEvent.WINDOW_CLOSING ) );
  }
 
  /**
   * Sets the color which the screen will be cleared with.
   * @param color The color the screen will be cleared with.
   */
  public void setClearColor( Color color ) {
    mBackground = color;
  }
  public void setWidth( int width ) {
    mWindowSize.setWidth( width );
    mCanvas.setSize( width, mWindowSize.getHeight() );
    pack();
  }
  public void setHeight( int height ) {
    mWindowSize.setHeight( height );
    mCanvas.setSize( height, mWindowSize.getWidth() );
    pack();
  }

        /* Apparently these are override and not needed.
  public int getWidth() {
    return mWindowSize.getWidth();
  }
  public int getHeight() {
    return mWindowSize.getHeight();
  }*/
 
  public void setSize( Size size ) {
    mWindowSize = size;
    mCanvas.setSize( size.getWidth(), size.getHeight() );
    pack();
  }

        @Override
        public void addKeyListener( KeyListener kl ) {
            super.addKeyListener( kl );
            mCanvas.addKeyListener( kl );
        }

        @Override
        public void addMouseListener( MouseListener ml ) {
            super.addMouseListener( ml );
            mCanvas.addMouseListener( ml );
        }

        @Override
        public void addMouseMotionListener( MouseMotionListener mml ) {
            super.addMouseMotionListener( mml );
            mCanvas.addMouseMotionListener( mml );
        }
}
TOP

Related Classes of utils.JavaWindow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.