Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JFrame

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Birmingham, United Kingdom
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JFrame

    EDIT: Sent the project to a friend, who compiled it and says it appears fine on his display, this had led me to believe that it's either my OS, GPU or Monitor, either way I guess this means it is independent of my code. Please check over if you will and insure I have done everything correctly, in case the error can be resolved this way! Thanks

    Ok, so I took a first try at making a simple window using a JFrame, ready to be drawn on with images. My CanvasHolder class extends Canvas, and is the component I'm using with a BufferStrategy of 2 to draw to the screen. For the moment, I am not drawing anything other than a clearRect to clear the screen of the previous data (when some will be present).

    As this code stands, -sometimes- it works (3/5 times), and I get a red background as I should (I'm using red for the sake of ease of knowing that what I'm writing is working). The other 2/5 times I just get a white screen that when clicked outside of closes.

    Also, the times where it does work, the boundary seems to start a certain amount of pixels to the right, giving me a black band across the left which is not accessible by the mouse. This occurs no matter what resolution I set it to perform at.

    Like I said, this is my first attempt at something in Java using a JFrame, I might have blatently done something very wrong or that is very bad practice, any advice on the following code would be great.

    Thanks guys.

    Main Class:
    package mainPackage;
     
    import java.awt.DisplayMode;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
     
    import javax.swing.JFrame;
     
    import canvasPackage.CanvasHolder;
     
    public class MainMethod 
    {
    	//fields to hold screen resolution
    	private static int xResolution;
    	private static int yResolution;
     
    	//JFrame variable to hold JFrame that holds the canvas
    	private static JFrame jFrame;
     
     
    	//main
    	public static void main(String[] args) 
    	{
    		//apply wanted resolution
    		setXResolution(800);
    		setYResolution(600);
     
    		//see 'initialiseGraphics()' below
    		jFrame = initialiseGraphics(xResolution, yResolution, 32, true);
     
    		//creates an instance of the canvas holder, applying a dimension of the given resolution
    		CanvasHolder canvas = new CanvasHolder(xResolution, yResolution);
     
    		//apply canvas and set characteristics
    		jFrame.add(canvas);
    		jFrame.setVisible(true);
    		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//modified method which creates bufferstrategy of required amount
    		canvas.addNotify(2);
     
    		//main 'loop', see CanvasHolder class
    		canvas.mainRenderer(xResolution, yResolution);
     
     
    	}
     
    	public static JFrame initialiseGraphics(int xResolution, int yResolution, int bitDepth, boolean isFullscreen)
    	{
    		//detect graphics environment and deduce graphics config
    		GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		GraphicsDevice device = environment.getDefaultScreenDevice();
    		GraphicsConfiguration config = device.getDefaultConfiguration();
     
    		//create new JFrame with necessary config
    		JFrame jFrame = new JFrame(config);
     
    		//if fullscreen is specified, check it is supported and change resolution to given values
    		if ( isFullscreen == true )
    		{
    			if ( device.isFullScreenSupported() ) 
    			{ 
    				device.setFullScreenWindow(jFrame);
     
    				if ( device.isDisplayChangeSupported() )
    				{
    					device.setDisplayMode( new DisplayMode( xResolution, yResolution, bitDepth, DisplayMode.REFRESH_RATE_UNKNOWN ) );
    				}
    				else
    				{
    					System.err.println("Change display mode not supported");
    				}
    			}
    			else
    			{
    				System.err.println("Full screen not supported");
    			}
    		}
     
    		return jFrame;
     
    	}
     
     
     
    	public static int getXResolution()
    	{
    		return xResolution;
    	}
     
    	public static void setXResolution(int x)
    	{
    		xResolution = x;
    	}
     
    	public static int getYResolution()
    	{
    		return yResolution;
    	}
     
    	public static void setYResolution(int y)
    	{
    		yResolution = y;
    	}
     
     
    }

    CanvasHolder class:
    package canvasPackage;
     
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
     
    public class CanvasHolder extends Canvas
    {	
    	//holds bufferstrategy
    	BufferStrategy bufStrat;
     
    	//constructor initiates canvas with given resolution and has focus. Background colour set here
    	public CanvasHolder(int xRes, int yRes)
    	{
    		setBackground(Color.red);
    		this.setPreferredSize(new Dimension(xRes, yRes));
    		setFocusable(true);
    		requestFocus();
     
    	}
     
    	//custom addNotify() to create buffer strategy
    	public void addNotify(int buffer)
    	{
    		super.addNotify();
    		this.createBufferStrategy(buffer);
    		this.bufStrat = this.getBufferStrategy();
    	}
     
    	public void mainRenderer(int xRes, int yRes)
    	{
    		//create graphics object to 'represent' current off-screen image
    		Graphics g = bufStrat.getDrawGraphics();
     
    		//wipe last iteration's pixels from screen ready for re-drawing
    		g.setColor(Color.red);
    		g.clearRect(0, 0, xRes, yRes);
     
    		/*
    		 * Image drawing goes here
    		 */
     
    		//switch off-screen and on-screen images and release unneeded graphics representation
    		bufStrat.show();
    		g.dispose();
     
    		//apply a sleep time to control tickrate
    		try 
    		{
    			Thread.sleep(19);
    		} 
    		catch (InterruptedException e) 
    		{
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
     
    	//draw image method goes here
    	public void drawImage(Graphics g, BufferedImage bufImage, int xLocation, int yLocation)
    	{
     
    	}
    }
    Last edited by M3ss1ah; February 21st, 2011 at 11:34 AM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Location
    Birmingham, United Kingdom
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JFrame

    Actually offended that not only does this thread have no replies, but no views either. Having watched every thread before and after this one be viewed and replied on while sitting hoping someone just comments on my problem, I'm led to believe you guys aren't bothered.

    Being a new member here shouldn't effect the help received, although I presume it won't really matter to you guys, but I'll have to join some other Java community instead.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: JFrame

    It full screens, then I seem to be getting a red flicker for about a second, then it stops at white, clicking does nothing for me, it just waits for me to hit alt+f4. An explanation of the black band could be from your monitor not being adjusted correctly for the 800x600 resolution. I know when i change resolution on my lcd, i have to hit the auto button for it to fill the screen.
    Last edited by JJeng; February 23rd, 2011 at 04:54 PM.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: JFrame

    Moved to - AWT / Java Swing
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. JFrame not repainting
    By dumb_terminal in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2010, 08:51 AM
  2. [SOLVED] Drawing on JFrame
    By kbarrett1989 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 31st, 2010, 03:41 AM
  3. Print out a JFrame
    By ellias2007 in forum AWT / Java Swing
    Replies: 8
    Last Post: June 17th, 2010, 06:15 AM
  4. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 04:12 AM
  5. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM