Results 1 to 18 of 18
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default JFrame locate the window to the screen that the application was launched in?

    I'm not sure if this automatically happens, but I've tried moving eclipse from one two another screen, and it only launches in my main screen. I have it set to get the width and height of the screen the window is in from Toolkit. But, if it does actually just pop your window onto the main screen all the time, how can I change this so wherever the application is launched, on that screen the window is going to be displayed. I don't think some workaround with getting which screen the mouse is in is useful, because you can also press ENTER with a file selected to launch it. So anyone have any ideas?

    Regards
    Augustas656

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JFrame locate the window to the screen that the application was launched in?

    I think you're looking for java.awt.GraphicsDevice. Note that the example code in the API for that class unnecessarily mixes javax.swing.JFrame and java.awt.Canvas -- not advisable in any application you write. They should definitely have used a JPanel instead of a Canvas; being a lightweight component, JPanel doesn't require a reference to a GraphicsConfiguration. The example also uses the deprecated JFrame#show() which should be replaced by setVisible(true), and is poorly indented.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    From what I know, I use canvas because I want triple buffering for my game, if there's a way to make JFrame and its components have triple buffering, I'll most likely use that. My class extends Canvas, and this is how I initiate JFrame, it's an abstract class, I use it like a GameFrame.

    Java Code:
    		Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
    		int screenWidth = (int) screenDimension.getWidth();
    		int screenHeight = (int) screenDimension.getHeight();
    		
    		setMinimumSize(screenDimension);
    		setPreferredSize(screenDimension);
    		setMaximumSize(screenDimension);
    		
    		frame = new JFrame(gameName);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLayout(new BorderLayout());
    		frame.add(this, BorderLayout.CENTER);
    		frame.setSize(screenWidth, screenHeight);
    		frame.setResizable(true);
    		frame.setLocationRelativeTo(null);
    		frame.setUndecorated(true);
    		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    		frame.setVisible(true);
    		frame.pack();

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Why do you want triple buffering? Is this based on some detailed analysis? I have written some pretty interactive applications using Swing and never have had any flicker or other performance issues. And my laptop is ancient by todays standards.

    N.B. Of course, I am not writing semi-realistic games which one would normally play on a state of the art gamebox either.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Well, I might use it later, not too sure, and this was the code that I saw in my first tutorial to make a Java RPG Game, and I just followed it, never had any problems, and I would improve my code, until now. For some reason, I have flickering, black and white constant when nothing is drawn. I can draw over and it will stop the flickering, however. Eventhough I don't intend to have my window be resizable, because it's full screen anyway, when I resize it, it flickers but really wierdly, like grayish white and, I won't describe it, just flickering basically. I used the same code before and it didn't use to flicker... strange.

    Regards
    augustas656

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Black and white flickering sounds like you have competing background colors. Do you do a super.paintComponent(g) in your paintComponent() method? I can make other guesses but it would be much more helpful if you would submit a small piece of code, fully compilable, that demonstrates the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    I don't use paintComponent, I have a game loop, and ticks are limited to 60 per second, and rendering is unlimited, render method is called rapidly, and render checks if a Buffer Strategy is created, if not, then one is created. Then I use bufferStrategy.getDrawGraphics() if I want to use Graphics to draw anything, and then graphics.dipose() and bufferStrategy.show().

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Unfortunately, without seeing an example that demonstrates the problem I can't help. Perhaps someone else can assist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Well what example do you need? My window flickers black and white

    Frame and Canvas Initialisation:
    Java Code:
    		Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
    		int screenWidth = (int) screenDimension.getWidth();
    		int screenHeight = (int) screenDimension.getHeight();
    		
    		setMinimumSize(screenDimension);
    		setPreferredSize(screenDimension);
    		setMaximumSize(screenDimension);
    		
    		frame = new JFrame(gameName);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLayout(new BorderLayout());
    		frame.add(this, BorderLayout.CENTER);
    		frame.setSize(screenWidth, screenHeight);
    		frame.setResizable(true);
    		frame.setLocationRelativeTo(null);
    		frame.setUndecorated(true);
    		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    		frame.setVisible(true);
    		frame.pack();
    My render method:
    Java Code:
    		BufferStrategy bufferStrategy = getBufferStrategy();
    		if (bufferStrategy == null) { createBufferStrategy(3);
    			return; }
    		
    		Graphics graphics = bufferStrategy.getDrawGraphics();
    		graphics.clearRect(0, 0, getWidth(), getHeight());
    		
    		GameColor gc = new GameColor(255, 0, 0);
    		gc.changeDarkness(75.0F);
    		gc.changeHue(180.0F);
    		graphics.setColor(gc.color);
    		graphics.fillRect(0, 0, frame.getWidth(), frame.getHeight());
    		graphics.dispose();
    		bufferStrategy.show();
    		render();
    I call my render method literally, without any delay in my infinite game loop.

    Regards
    Aqualight

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame locate the window to the screen that the application was launched in?

    The example I need is one which I can compile and recreate the problem. What you posted above does not tell me if you extend JFrame (bad) or JPanel (good). But in any event if that is your render method then you are calling render from your render method which could be related to the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Oh that, no it has a different name, I was thinking of having an asbtract method render(), the class that that code is in is an abstract class, it's like a GameFrame. Nvm
    Regards
    augustas656

  12. #12
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Here's an example, and the problem seems to be in the game loop :O
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.image.BufferStrategy;
    
    import javax.swing.JFrame;
    
    public class Test extends Canvas implements Runnable {
    	private static final long serialVersionUID = 1L;
    	private boolean running = false;
    	
    	public static void main(String args[]) {
    		new Test().start();
    	}
    	
    	public Test() {
    		Dimension test = new Dimension(400, 400);
    		setMinimumSize(test);
    		setPreferredSize(test);
    		setMaximumSize(test);
    		
    		JFrame frame = new JFrame("Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLayout(new BorderLayout());
    		frame.add(this, BorderLayout.CENTER);
    		frame.setSize(400, 400);
    		frame.setResizable(true);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		frame.pack();
    	}
    	
    	protected synchronized void start() {
    		running = true;
    		new Thread(this).start(); }
    	protected synchronized void stop() {
    		running = false; }
    	
    	private int gameTPSLimit = 60;
    	private double gameNanoSPT = 1000000000D / (double) gameTPSLimit;
    	
    	@Override
    	public void run() {
    		long nanoPrevious = System.nanoTime();
    		long nanoCurrent, nanoElapsed = 0;
    		
    		while (running) {
    			nanoCurrent = System.nanoTime();
    			nanoElapsed += (nanoCurrent - nanoPrevious);
    			nanoPrevious = nanoCurrent;
    			
    			while (nanoElapsed >= gameNanoSPT) {
    				update();
    				nanoElapsed -= gameNanoSPT;
    			} render();
    		}
    	}
    	
    	public void render() {
    		BufferStrategy bufferStrategy = getBufferStrategy();
    		if (bufferStrategy == null) { createBufferStrategy(3);
    			return; }
    		
    		Graphics graphics = bufferStrategy.getDrawGraphics();
    		graphics.dispose();
    		bufferStrategy.show();
    	}
    	
    	public void update() {
    		
    	}
    }
    Regards
    augustas656

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Not certain about your last message. See the effect of this if you put it right after you get your graphics context.

    Java Code:
    graphics.setColor(Color.red);
     graphics.fillRect(100,100,200,200);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Yeah I know that, I can cover it with anything, but why does it flash in the first place? It doesn't flash with createBufferStrategy(2), or createBufferStrategy(3) doesn't flash without the game loop, just in an infinite game loop. Wierd...
    Regards
    augustas656

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame locate the window to the screen that the application was launched in?

    The problem seems to be that you are not correctly defining how to handle page flipping. According to https://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html after a page flip, the contents can be unpredictable. So you need to define your handling when creating a bufferStrategy. Presently, your page flip is of unknown type. This would be setting to BACKGROUND so the color is copied after flipping. This will eliminate the flicker.

    Check out the two argument method for creating a bufferStrategy.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Can you provide me a link of how to handle buffer flipping in Canvas or explain please, because I've tried searching and can't find much help :P
    Regards
    augustas656

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame locate the window to the screen that the application was launched in?

    All I did was read the provided link and I also read the BufferStrategy API. I also used some print statements to print out your BufferCapabilities that were in use. That is how I found out the page flip capability was declared as unknown. That is all I know (since I never read this stuff until your thread).

    Edit: I also put in a 300 ms sleep between render calls to examine the flicker on a slower basis.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Re: JFrame locate the window to the screen that the application was launched in?

    Hmm, I can't find a way of making this work, I can't find any good documentation online and the Java Oracle Documentation doesn't help me, there's no videos or anything that I can find. I mean I'd imagine triple buffering for me to work by just drawing on the buffer that isn't deplayed or next to be desplayed, or am I wrong? But this is just the theory part, I can't find any way of making this work...

    My latest problem is that when I resize my game it flashes, faintly but irritatingly...
    Last edited by augustas656; 11-12-2014 at 11:22 PM.
    Regards
    augustas656

Similar Threads

  1. [JPanel] In window splash screen.
    By Jerba in forum AWT / Swing
    Replies: 1
    Last Post: 02-22-2013, 04:48 AM
  2. JFrame, Jdialog or Application Window
    By pamm in forum New To Java
    Replies: 8
    Last Post: 09-30-2011, 11:23 PM
  3. Replies: 0
    Last Post: 02-22-2011, 10:27 AM
  4. Replies: 1
    Last Post: 04-26-2010, 01:02 PM
  5. How to enable save as option in menu after the application has been launched
    By plenitude in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 12-26-2008, 09:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •