Topic: Open Source fullscreen stub for Java (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=28405" title="Pages that link to Topic: Open Source fullscreen stub for Java (Page 1 of 1)" rel="nofollow" >Topic: Open Source fullscreen stub for Java <span class="small">(Page 1 of 1)</span>\

 
_Mauro
Maniac (V) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 09-09-2006 17:23 Edit Quote

Hello world,

This is the most simple example of a fullscreen application starter for Java I could think of.
As an applet, it has restrictions, unless ran from appletviewer.

As a webstart app or normal app, it is decent, and very small in size (4 to 2kb if in a jar).

Enjoy.

code:
package ake;

/*

	Applet code 
	by Mauro Colella
	(c) 2006
	================
	Feel free to use for personal needs. For commercial uses, 
	include the source of this applet.

	** Tips
	-- It is subject to security restructions when run from a browser
	-- works well when run from appletViewer
	-- Useful for inclusion in Java applications or webstarted applications
	-- JOGL friendly


*/


import java.applet.Applet;
import java.awt.HeadlessException;
import java.awt.GraphicsDevice;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class stub extends Applet implements Runnable, KeyListener {
	GraphicsDevice context = null;
	
	private static Thread runner = null;
	
	private static Frame window = null;
	
	private static BufferStrategy strategy = null;
	
	private static BufferCapabilities capabilities = null;
	
	GraphicsConfiguration gc = null;
	
	private int i = 0;

	
	DisplayMode newDisplayMode = null, oldDisplayMode = null; // =
	// myDevice.getDisplayMode();
	
	public stub() throws HeadlessException {
		super();
		
		// TODO Auto-generated constructor stub
	}
	
	public void init() {
		
	}
	
	public void start() {
		if (runner == null) {
			context = GraphicsEnvironment.getLocalGraphicsEnvironment()
			.getDefaultScreenDevice();
			
	        gc = context.getDefaultConfiguration();
			oldDisplayMode = context.getDisplayMode();
			newDisplayMode = new DisplayMode(800,600,32,85);
			window = new Frame(gc);
			window.setName("Fullscreen applet");
			//myWindow.setSize(800, 600);
			window.addKeyListener(this);
			window.setUndecorated(true);
			window.setIgnoreRepaint(true);
			//window.setAlwaysOnTop(true);
			window.setVisible(true);
			runner = new Thread(this);
			strategy = window.getBufferStrategy();
			capabilities = strategy.getCapabilities();
			
			try {
				context.setFullScreenWindow(window);
				if(context.isDisplayChangeSupported()){
					context.setDisplayMode(newDisplayMode);
				}
			} catch(Exception e) {
				context.setFullScreenWindow(null);
				if(context.isDisplayChangeSupported()){
					context.setDisplayMode(oldDisplayMode);
				}
			}
			System.out.println(capabilities.isMultiBufferAvailable());
			runner.start();
			
		}
	}
	
	public void run() {
		BufferStrategy bs;
		while (true) {
			// repaint();
			//g = myWindow.getGraphics();
			//if (context.isFullScreenSupported() /*
			/* &&
			* context.isDisplayChangeSupported()
			*///) {
				
				i++;
				i %= window.getHeight();
				try{
					//window.createBufferStrategy(1,capabilities);
					bs = window.getBufferStrategy();
					if (!bs.contentsLost()) {
						Graphics g = bs.getDrawGraphics();
						render(g);
						bs.show();
						g.dispose();
					}
				}
				catch(Exception e){
					e.printStackTrace();
				}
				
			//}
		}
	}
	
	private final void render(Graphics g){
		
		g.setColor(new Color(0, 0, 255));
		g.drawLine(0, i, window.getWidth(), i);
		
		try{
			Thread.sleep(1);
		}
		catch(InterruptedException e){
			//e.printStackTrace();
		}
		
		g.setColor(new Color(255, 255, 255));
		g.drawLine(0, i, window.getWidth(), i);
		
	}
	
	public void paint(Graphics g) {
		//myWindow.paintComponents(myFrame.getGraphics());
	}
	
	public void update() {
		
	}
	
	public void keyPressed(KeyEvent e) {
		runner.interrupt();
		window.setVisible(false);
		System.exit(0);
	}
	
	public void keyTyped(KeyEvent e) {
		
	}
	
	public void keyReleased(KeyEvent e) {
		
	}
}

_Mauro
Maniac (V) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 09-09-2006 18:20 Edit Quote

And for the same price, you get the java application version of the code, which I'll use later on to demonstrate:

code:
package ake;

/*

	Applet code 
	by Mauro Colella
	(c) 2006
	================
	Feel free to use for personal needs. For commercial uses, 
	include the source of this applet.

	** Tips
	-- It is subject to security restructions when run from a browser
	-- works well when run from appletViewer
	-- Useful for inclusion in Java applications or webstarted applications
	-- JOGL friendly


*/

import java.awt.GraphicsDevice;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class appStub extends Object implements Runnable, KeyListener {
	GraphicsDevice context = null;
	
	private static Thread runner = null;
	
	private static Frame window = null;
	
	private static BufferStrategy strategy = null;
	
	private static BufferCapabilities capabilities = null;
	
	GraphicsConfiguration gc = null;
	
	private int i = 0;

	
	DisplayMode newDisplayMode = null, oldDisplayMode = null; // =
	// myDevice.getDisplayMode();
	
	public appStub(){
		super();
		
		// TODO Auto-generated constructor stub
	}
	
	public void main() {
		appStub st = new appStub();
		st.start();
	}
	
	public void start() {
		if (runner == null) {
			context = GraphicsEnvironment.getLocalGraphicsEnvironment()
			.getDefaultScreenDevice();
			
	        gc = context.getDefaultConfiguration();
			oldDisplayMode = context.getDisplayMode();
			newDisplayMode = new DisplayMode(800,600,32,85);
			window = new Frame(gc);
			window.setName("Fullscreen applet");
			//myWindow.setSize(800, 600);
			window.addKeyListener(this);
			window.setUndecorated(true);
			window.setIgnoreRepaint(true);
			//window.setAlwaysOnTop(true);
			window.setVisible(true);
			runner = new Thread(this);
			strategy = window.getBufferStrategy();
			capabilities = strategy.getCapabilities();
			
			try {
				context.setFullScreenWindow(window);
				if(context.isDisplayChangeSupported()){
					context.setDisplayMode(newDisplayMode);
				}
			} catch(Exception e) {
				context.setFullScreenWindow(null);
				if(context.isDisplayChangeSupported()){
					context.setDisplayMode(oldDisplayMode);
				}
			}
			System.out.println(capabilities.isMultiBufferAvailable());
			runner.start();
			
		}
	}
	
	public void run() {
		BufferStrategy bs;
		while (true) {
			// repaint();
			//g = myWindow.getGraphics();
			//if (context.isFullScreenSupported() /*
			/* &&
			* context.isDisplayChangeSupported()
			*///) {
				
				i++;
				i %= window.getHeight();
				try{
					//window.createBufferStrategy(1,capabilities);
					bs = window.getBufferStrategy();
					if (!bs.contentsLost()) {
						Graphics g = bs.getDrawGraphics();
						render(g);
						bs.show();
						g.dispose();
					}
				}
				catch(Exception e){
					e.printStackTrace();
				}
				
			//}
		}
	}
	
	private final void render(Graphics g){
		
		g.setColor(new Color(0, 0, 255));
		g.drawLine(0, i, window.getWidth(), i);
		
		try{
			Thread.sleep(1);
		}
		catch(InterruptedException e){
			//e.printStackTrace();
		}
		
		g.setColor(new Color(255, 255, 255));
		g.drawLine(0, i, window.getWidth(), i);
		
	}
	
	public void paint(Graphics g) {
		//myWindow.paintComponents(myFrame.getGraphics());
	}
	
	public void update() {
		
	}
	
	public void keyPressed(KeyEvent e) {
		runner.interrupt();
		window.setVisible(false);
		System.exit(0);
	}
	
	public void keyTyped(KeyEvent e) {
		
	}
	
	public void keyReleased(KeyEvent e) {
		
	}
}



edit TP: Looks much nicer with [ code] tags

(Edited by Tyberius Prime on 09-09-2006 18:50)



Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu