How to hide Taskbar in Fullscreen Mode JavaFx - java

I am currently writing a Game with JavaFx. I now want to make the game run in fullscreen mode. I have written the following code:
public class Game extends Application {
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage gameStage) {
gameStage.setResizable(false);
gameStage.setMaximized(Settings.fullscreen);
gameStage.setFullScreen(Settings.fullscreen);
Settings.changeRoot(StartMenu.getInstance()); //this line just sets my root element
Scene scene = new Scene(Settings.root,Settings.width,Settings.height);
gameStage.setScene(scene);
SoundLoader.getInstance(); //loads sound
ImageLoader.getInstance(); //loads all ingame images
gameStage.show();
}
}
The proplem was that it allways showed a popup that said press esc to leave fullscreen mode. I added the following line to remove that text:
gameStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
The problem now was, that it no longer goes completly into fullscreen mode it allways showes the Toolbar screenshot that shows how the game is displayed.How can I hide the taskbar completly?

I figured out that the game application wasnt in the front that was the reason why the toolbar was infront of it. i added the following line to fix it:
gameStage.toFront();

You can remove the text/popup of Press ESC to leave FullScreen this way :
gameStage.setFullScreenExitHint("");
PS : Remove the line you added (gameStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH); Because I guess this way you won't have to hide any Taskbar.

Related

netbeans setDefaultCloseOperation

I am trying to set the default close operation in NetBeans 8.0.2 (in Ubuntu 14.04 on an older Asus gaming laptop.) My program is very large but uses no JFrame or java.swing components.
I merely need to save some values when the "x" in the lower right corner is clicked (this is one usual way to stop execution of a java program in NetBeans.)
I found suggestions that involved swing & JFrame, but it wasn't clear just where to insert the code:
DefaultApplicationView view = new DefaultApplicationView(this);
javax.swing.JFrame frame = view.getFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.out.println("CLOSING");
}
}
show(view);
I also found a set of instructions that I think I would prefer to use, but the post is old enough that my NetBeans doesn't have the tabs/menu-items referred to:
Set Window to Design Mode by clicking the 'Design' Tab
In the Navigator: Right click the 'JFrame' -> 'Properties'
In the Properties Tab: Set 'defaultCloseOperation' (top of the list) to 'DO_NOTHING'
Select 'Events' Tab
Scroll down to 'windowClosing'
Click on the "..." button on the right of the event to bring up the custom editor
Click 'Add...' and name the handler (i.e. custom function that you want to have execute on click of the 'X', or window close event).
Click 'Ok'
Netbeans now automatically creates the function and takes to you the function body in the source view
Now simply add what you want to do here: eg. dispose(), or system.exit or pintln(), or whatever your heart desires, as long as its JAVA and makes sense to the app.
Then there are a few other possibly relevant posts, but they all explicitly involve JFrame and/or swing. (Am I ignorant of some fact such as "All NetBeans java applications use JFrame", or some such?)
A pared down example of code for what I'm trying to do would be:
public class MyApp{
public static void main(String[] args){
loadMyVariables();
// do some work that changes variables' values
// during this work user clicks the 'x' box to halt execution
// I need then automatically to save the variables' new values
}
// needs to be called by the OS or GUI when execution is halted by user
public static void saveMyVariables{
// here the usual printStream stuff saves some values to a file
System.exit(0);
}
public static void loadMyVariables{
// here the usual Scanner stuff reads some values from a file
}
}
(I need help setting the tags for this, so I'm doing as instructed and asking the community.)
THANKS

JavaFX - Music On/Off Toggle Button (Not Working)

Below is the code sample, with other features left out. The code below encompasses the media player only. It's used on a menu screen for a project I'm working on. My issue is getting the musicButton (which is a toggle button- On/Off) to work properly. Using the following code, when I interact with the music toggle button, the playing music stops. When I click it again to resume playing, it does not resume. It stops once and stops altogether.
You can see I've tried simply using the boolean values of the toggle button in two if statements... If it's off and pressed, pause the music. If its on and pressed, resume the music. The problem is, as stated earlier, pausing the music works but it cannot be resumed. I've tried some combinations with loops, but nothing worked either.
I think if statements are too simple for this. I've scoured the JavaDocs and various online articles but I cannot find anything definitive. I've read a little about listeners, but they seem overly-complex for an on/off switch.
My question:
How do I get the musicButton to pause/play the music, whenver the user clicks it?
Any help is appreciated, thanks.
-Bagger
/* A simple game, the mechanics not yet implemented.
This is simply working on the title screen. */
import java.io.File;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class MenuFX extends Application {
#Override
public void start (Stage primaryStage) {
// Make the window a set size...
primaryStage.setResizable(false);
// Create media player
// Rather than inputting the entire absolute URI, which would confine the program
// to the creator's device, we create a new file, grab the URI on whatever machine
// the program is running on and convert it to a string... portability.
Media menuMusic = new Media(new File("music/menu.mp3").toURI().toString());
MediaPlayer menuPlayer = new MediaPlayer(menuMusic);
// Want to see the absolute URI? Uncomment the next line
//System.out.println(new File("music/menu.mp3").toURI().toString());
// Adjust the cycles and volume then start playing menu music
// Lazy, but it will suffice
menuPlayer.setCycleCount(999999999);
menuPlayer.setVolume(0.1);
menuPlayer.setAutoPlay(true);
/*
Need assistance here
*/
// Create music toggle button
ToggleButton musicButton = new ToggleButton("Music On/Off");
if (musicButton.isSelected() == false) {
musicButton.setOnAction(e -> menuPlayer.pause());
}
if (musicButton.isSelected() == true) {
musicButton.setOnAction(e -> menuPlayer.play());
}
// Add all nodes to the vbox pane and center it all
// Must be in order from top to bottom
menuVBox.getChildren().add(musicButton);
menuVBox.setAlignment(Pos.CENTER);
// New scene, place pane in it
Scene scene = new Scene(menuVBox, 630, 730);
// Place scene in stage
primaryStage.setTitle("-tiles-");
primaryStage.setScene(scene);
primaryStage.show();
}
// Needed to run JavaFX w/o the use of the command line
public static void main(String[] args) {
launch(args);
}
}
I think you are over thinking it. You should have an EventListener on your ToggleButton to Pause and Play the music.
musicButton.setOnAction(event -> {
if (musicButton.isSelected()) {
menuPlayer.pause();
}else {
menuPlayer.play();
}
});
This should give you the desired effect.
The reason your code was not working is because the ToggleButton is not selected by default, so the only EventListener that gets associated with it is the menuPlayer.pause();. So when you click on it, it only ever pauses. I have moved your code into one EventListener, and used the approriate if-else.

Disable Command in the Codename toolbar

My application uses one form, the navigation consists of replace a main container.
I have a toolbar with sidemenu, logo , back and exit command.
I want to disable the back command in some specific pages.
command.setEnabled(false) does not work, removing and adding commands does not work properly.
Is there any way to disable command after adding it to the toolbar.
Thanks
If you want the command to be visible and clickable but do nothing, you can use if condition on the code inside the actionPerformed of the back command.
Command back = new Command("back") {
#Override
public void actionPerformed(ActionEvent evt) {
if (some coditions is true) {
backForm.showBack();
}
}
};
OR disable it before adding it to your Toolbar and calling f.setBackCommand(back);, if you want it to be visible but not clickable
back.setEnabled(false);
f.setBackCommand(back);
toolbar.addCommandToLeftBar(back);
If you don't want it there, you can either remove it or do the following if removing it will ruin your design and your TitleCommand Uiid doesn't have a background color different from the TitleArea:
Command back = new Command(" ");
back.putClientProperty("TitleCommand", true);

Slick2d fullscreen adds black bar

I am trying to make my java slick2d game fullscreen capable. It launches into fullscreen fine initially with this code:
Container app = new Container(this);
DisplayMode current = Display.getDesktopDisplayMode();
System.out.println(current.getWidth() + "x" + current.getHeight());
Start.setFullscreen(current.getWidth(), current.getHeight());
app.setDisplayMode(Start.FULLSCREEN_WIDTH, Start.FULLSCREEN_HEIGHT, true);
app.setResizable(true);
app.setTargetFrameRate(60);
app.setClearEachFrame(true);
app.setAlwaysRender(false);
app.start();
(The Start class holds a few constant variables that the rest of the program references)
I can also turn off fullscreen in my update method with this code:
if (gc.getInput().isKeyPressed(Input.KEY_ESCAPE)){
if (gc.isFullscreen()){
gc.setFullscreen(false);
} else {
AppGameContainer app = (AppGameContainer)gc;
app.setDisplayMode(Start.FULLSCREEN_WIDTH, Start.FULLSCREEN_HEIGHT, true);
}
}
When I run the program and press escape, it becomes a window and looks fine but when I try to put it back into fullscreen mode, it occupies the whole screen but there is a black bar at the top of the window and the displaying area is squished to whatever size the window was. Here is a screenshot of this effect:
Also, when I switch to fullscreen the console prints INFO:Starting display 1440x900 which is the correct screen size of my display.
I am using a MacBook Air 13-inch running 10.9.2 (Mavericks).

Animated Splash Screen on Netbeans Platform app

Our maven/Netbeans platform application uses a custom image on startup, by replacing
Nbm-branding > core.jar > org.netbeans.core.startup > splash.gif
I tried making it an animated .gif, but only the first frame is displayed.
How would one possibly go about implementing an animated splash screen, maybe by running some JavaFX window animations?
I've seen another other SO question, but it wasn't really answered - please notice I'm asking about how to integrate a custom splash screen with my Netbeans Platform application, and not how to actually build it.
Surprisingly enough, I found out how to plug in a custom splash screen based on this post about user authentication and authorization.
Basically, one needs to write another start-up class, instead of the platform's default:
import java.lang.reflect.Method;
public class CustomStartup {
private static final String NB_MAIN_CLASS = "org.netbeans.core.startup.Main";
public static void main(String[] args) throws Exception {
// do whatever you need here (e.g. show a custom login form)
System.out.println("Hello world! I am a custom startup class");
JWindow splash = initSplash();
// once you're done with that, hand control back to NetBeans
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
Class<?> mainClass = Class.forName(NB_MAIN_CLASS, true, classloader);
Object mainObject = mainClass.newInstance();
Method mainMethod = mainClass.getDeclaredMethod("main", new Class[]{String[].class});
mainMethod.invoke(mainObject, (Object) args);
splash.setVisible(false);
}
}
In that class, one can create a JavaFX stage, embed it into a JWindow, and show it:
public JWindow initSplash(){
JWindow window = new JWindow();
final JFXPanel fxPanel = new JFXPanel();
window.add(fxPanel);
window.setVisible(true);
window.setLocationRelativeTo(null);
Platform.runLater(new Runnable() {
#Override
public void run() {
Scene scene = new Scene(new CustomFxSplash(), 475, 300, true);
fxPanel.setScene(scene);
}
}
return window;
}
Other things to remember are:
Suppress the original NetBeans splash screen by running your app with the --nosplash parameter.
Call your custom initialization class by running your app with the -J-Dnetbeans.mainclass=com.package.splash.CustomStartup parameter
As the link suggests this custom class has to be on the platform's initialization classpath, meaning inside the platform/core folder.
The current version of the NetBeans class that is responsible for rendering the splash screen can be viewed online here: org.netbeans.core.startup.
The culprit code that prevents the gif from animating is this line (line 546)
graphics.drawImage(image, 0, 0, null);
In order for the gif to animate the ImageObserver will have to be specified instead of being set to null and then repaint must be called when imageUpdate() is called on the ImageObserver.
An example of displaying an animated gif can be viewed here: Relationship Between Animated Gif and Image Observer
So as far as I can see you will either have to change the above NetBeans platform code and rebuild it for your application or you will have to create your own splash screen from scratch to use instead of the NetBeans one.
Hope you find this useful!

Categories

Resources