java - Swing paintComponents in JavaFX -


i apologising in advance upcoming silly question.

is there replacement paintcomponents method jpanel has javafx? or should use jfxpanel embed swing?

for example figured timeline same timer in swing, corresponding thing panel/paintcomponents?

edit: example how go animating circle x-y coordinate another? (without use of translatetransition, of course)

i tried painting things canvas couldn't figure out how update canvas, calling repaint() in swing?

here example of grid, resizes automatically using simple bindings.

import javafx.beans.property.doubleproperty; import javafx.beans.property.simpledoubleproperty; import javafx.scene.layout.pane; import javafx.scene.shape.line;  public class grid {     private final pane view = new pane();      private final int numcolumns ;     private final int numrows ;      // arbitrary defaults of 20:     private final doubleproperty prefcolumnwidth = new simpledoubleproperty(20);     private final doubleproperty prefrowheight = new simpledoubleproperty(20);      public grid(int numcolumns, int numrows) {         this.numcolumns = numcolumns ;         this.numrows = numrows ;          (int x = 0 ; x <= numcolumns ; x++) {             line line = new line();             line.startxproperty().bind(view.widthproperty().multiply(x).divide(numcolumns));             line.endxproperty().bind(line.startxproperty());             line.setstarty(0);             line.endyproperty().bind(view.heightproperty());             view.getchildren().add(line);         }          (int y = 0 ; y <= numrows ; y++) {             line line = new line();             line.startyproperty().bind(view.heightproperty().multiply(y).divide(numrows));             line.endyproperty().bind(line.startyproperty());             line.setstartx(0);             line.endxproperty().bind(view.widthproperty());             view.getchildren().add(line);         }          view.prefwidthproperty().bind(prefcolumnwidth.multiply(numcolumns));         view.prefheightproperty().bind(prefrowheight.multiply(numrows));     }      public final doubleproperty prefcolumnwidthproperty() {         return this.prefcolumnwidth;     }       public final double getprefcolumnwidth() {         return this.prefcolumnwidthproperty().get();     }       public final void setprefcolumnwidth(final double prefcolumnwidth) {         this.prefcolumnwidthproperty().set(prefcolumnwidth);     }       public final doubleproperty prefrowheightproperty() {         return this.prefrowheight;     }       public final double getprefrowheight() {         return this.prefrowheightproperty().get();     }       public final void setprefrowheight(final double prefrowheight) {         this.prefrowheightproperty().set(prefrowheight);     }      public pane getview() {         return view;     }      public int getnumcolumns() {         return numcolumns;     }      public int getnumrows() {         return numrows;     }  } 

here's simple test:

import javafx.application.application; import javafx.scene.scene; import javafx.stage.stage;  public class gridtest extends application {      @override     public void start(stage primarystage) {         grid grid = new grid(10,10);         scene scene = new scene(grid.getview());         primarystage.setscene(scene);         primarystage.show();     }      public static void main(string[] args) {         launch(args);     } } 

there bunch of different approaches use this. note approach above doesn't require subclassing @ all, write method created pane:

private pane creategrid(int numcolumns, int numrows) {      pane view = new pane();      (int x = 0 ; x <= numcolumns ; x++) {         line line = new line();         line.startxproperty().bind(view.widthproperty().multiply(x).divide(numcolumns));         line.endxproperty().bind(line.startxproperty());         line.setstarty(0);         line.endyproperty().bind(view.heightproperty());         view.getchildren().add(line);     }      (int y = 0 ; y <= numrows ; y++) {         line line = new line();         line.startyproperty().bind(view.heightproperty().multiply(y).divide(numrows));         line.endyproperty().bind(line.startyproperty());         line.setstartx(0);         line.endxproperty().bind(view.widthproperty());         view.getchildren().add(line);     }      view.setprefsize(20*numcolumns, 20*numrows);     return view ; } 

or, if wanted closer awt/spring way of doing things, subclass region, use canvas, , override region.layoutchildren(). layoutchildren() method called part of layout pass (which triggered if region changes size). in 1 added support padding:

import javafx.scene.canvas.canvas; import javafx.scene.canvas.graphicscontext; import javafx.scene.layout.region;  public class grid extends region {      private canvas canvas ;     private final int numcolumns ;     private final int numrows ;      public grid(int numcolumns, int numrows) {         this.numcolumns = numcolumns ;         this.numrows = numrows ;         canvas = new canvas();         getchildren().add(canvas);     }      @override     protected void layoutchildren() {         double w = getwidth() - getpadding().getleft() - getpadding().getright() ;         double h = getheight() - getpadding().gettop() - getpadding().getbottom() ;          canvas.setwidth(w+1);         canvas.setheight(h+1);          canvas.setlayoutx(getpadding().getleft());         canvas.setlayouty(getpadding().getright());          graphicscontext gc = canvas.getgraphicscontext2d();         gc.clearrect(0, 0, w, h);          (int = 0 ; <= numcolumns ; i++) {              // adding 0.5 here centers line in physical pixel,             // making appear crisper:             double x = w*i/numcolumns + 0.5;              gc.strokeline(x, 0, x, h);         }          (int j = 0 ; j <= numrows ; j++) {             double y = h*j/numrows + 0.5 ;             gc.strokeline(0, y, w, y);         }     }      @override     protected double computeprefwidth(double height) {         return 20 * numcolumns;     }      @override     protected double computeprefheight(double width) {         return 20 * numrows ;     }  } 

here's test this:

import javafx.application.application; import javafx.geometry.insets; import javafx.scene.scene; import javafx.stage.stage;  public class gridtest extends application {      @override     public void start(stage primarystage) {         grid grid = new grid(10,10);         grid.setpadding(new insets(20));         scene scene = new scene(grid, 400, 400);         primarystage.setscene(scene);         primarystage.show();     }      public static void main(string[] args) {         launch(args);     } } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -