Jump to content
  • Advertisement
Sign in to follow this  
RidiculousName

Java Modifying Panes With JavaFX

Recommended Posts

I want to be able to display an amount of gold at the top of the screen that changes based on how much the player buys. I can just remove and reload the Pane object in my screen controller class, but it seems like there must be a way for me to only update one part of a Pane without needing to reload the entire thing.

How can I modify a variable and change it's displayed value in a window, while not reloading the entire Pane object?

Main Class

package application;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
   // Fonts
   public static final String IMPER_FONT = "./res/fonts/imperator.ttf";

   // Global Images // used for screen titles
   public static final ImageView TITLE_IMG = makeImage("./res/images/title.png");
   public static final ImageView HIDEOUT_IMG = makeImage("./res/images/hideout.png");
   public static final ImageView TREASURY_IMG = makeImage("./res/images/treasury.png");
   
   // Global Game Stats
   public static int Gold = 1250;
   public static LinkList<Object> Equipment = new LinkList<>();
   
   // Panes
   private static Pane mainMenu = MainMenu.getPane();
   private static Pane hideout = Hideout.getPane();
   private static Pane treasury = Treasury.getPane();
   
   // Screen Controller
   private static Scene scene = new Scene(mainMenu, 1600, 900);
   private static ScreenController control = new ScreenController(scene);

   /**
    * Creates an ImageView object
    * 
    * @param path A String that represents the filepath to the image
    * @return ImageView An Imageview object with the appropriate image
    */
   private static ImageView makeImage(String path) {
         FileInputStream input;
         try {
            input = new FileInputStream(path);
            ImageView title = new ImageView(new Image(input));
            return title;
         } catch (FileNotFoundException e) {
            e.printStackTrace();
         }
         return null;
   }
   
   /**
    * Creates a Text object with specified font file-path, message, and size.
    * 
    * @param String path the file path of the font file
    * @param String message the text of the Text Object
    * @param double size the size of the font
    * @return Text The appropriate Text object.
    */
   public static Text makeText(String path, String message, double size) {
      FileInputStream input;
      try {
         input = new FileInputStream(new File(path));
         Font font = Font.loadFont(input, size);
         Text msg = new Text(message);
         msg.setFont(font);
         return msg;
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      return null;
   }

   @Override
   public void start (Stage stage) {

      // Add panes to ScreenController object
      control.addScreen("main menu", mainMenu);
      control.addScreen("hideout", hideout);
      control.addScreen("treasury", treasury);
   
      // create the scene and display it
      stage.setScene(scene);
      stage.show();
   }
   
   /**
    * Allows other classes to use the same ScreenController object.
    * 
    * @return ScreenController The object used to switch screens
    */
   public static ScreenController getControl() {
      return control;
   }
   
   public static void main(String[] args) {
      launch(args);
   }
}

ScreenController class

package application;

import java.util.HashMap;

import javafx.scene.Scene;
import javafx.scene.layout.Pane;

public class ScreenController {
   private HashMap<String, Pane> screenMap = new HashMap<>();
   private Scene main;
   
   public ScreenController(Scene main) {
      this.main = main;
   }
   
   protected void addScreen(String name, Pane pane) {
      screenMap.put(name,  pane);
   }
   
   protected void removeScreen(String name) {
      screenMap.remove(name);
   }
   
   protected void activate(String name) {
      main.setRoot(screenMap.get(name));
   }
}

 

Share this post


Link to post
Share on other sites
Advertisement

A Pane seems to only perform layout of its children according to the documentation, which is probably why it has no update or paint methods.

I messed around with JavaFx some months ago as a first experiment, and as far as I remember, I just painted on the canvas and that was sufficient to display the changes. Initialization code is
 

    public void start(Stage stage) {
        this.stage = stage;
        canvas = new Canvas(400, 200);
        gc = canvas.getGraphicsContext2D();

Where gc is a javafx.scene.canvas.GraphicsContext/ It has a wealth of draw calls to draw on the canvas.

Note however this is the most basic form of using JavaFX I seem to recall, officially you should create a tree of nodes as I understood it. where each node displays something. However, I just wanted a empty area to paint on, rather than a "normal" gui so a javafx.scene.canvas.Canvas was sufficient for me.

Code is still highly experimental, and probably not even working currently, so sharing it is a bit of a problem.

 

Hope it helps,

Albert

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!