Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JavaFX: Draw Polygon on Canvas  (Read 2494 times)
0 Members and 1 Guest are viewing this topic.
Offline Half_NO_oB

Senior Newbie





« Posted 2017-04-24 07:15:09 »

Hi guys

I'm still developing a platformer, but I don't get to draw a polygon on my screen. Im using a Group-Node as the root, that is used as a parent for the Scene-Object. Then canvas is then added. I'm not using a Layout/Stack-Pane/Grid-Pane...

I'm having a player object that is containing an Object of class HUD(displaying stats and stuff). I can draw Images without problems, but how can i draw a Rectangle/Polygon onto a canvas?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
@Override
   public void start(Stage primaryStage) throws Exception
   {
      primaryStage.setTitle("Platformer");

      Group root = new Group();
     
      Scene scene = new Scene(root, WIDTH, HEIGHT);
     
      Canvas canvas = new Canvas(WIDTH,HEIGHT);
      root.getChildren().add(canvas);

      GraphicsContext gc = canvas.getGraphicsContext2D();
     
      MapManager.getInstance().loadAllMaps("maps");
     
      tileMap = MapManager.getInstance().getCurrentTileMap();
     
      player = new Player(tileMap);
      player.setX(64);
      player.setY(64);
     
      scene.setOnKeyPressed(
            event -> {
               String code = event.getCode().toString();
               
               if(code == "W")
               {
                  player.setJumping(true);
                }
               if(code == "A")
               {
                  player.setLeft(true);
               }
               if(code == "D")
               {
                  player.setRight(true);
               }
            });
     
      scene.setOnKeyReleased(
            event -> {
               String code = event.getCode().toString();
               
               if(code == "A")
               {
                  player.setLeft(false);
               }
               if(code == "D")
               {
                  player.setRight(false);
               }
            });
     
      AnimationTimer at = new AnimationTimer()
      {
         @Override
         public void handle(long now)
         {
            update();
            render(gc);
         }
         
      };
      at.start();      
     
      primaryStage.setScene(scene);
      primaryStage.show();      
   }


Any ideas how to?

Greetz HalfNoob
Offline philfrei
« Reply #1 - Posted 2017-04-25 01:38:06 »

In the JavaFX tutorial on our tutorials page, they draw and animate a Circle.

http://www.java-gaming.org/topics/getting-started-with-javafx-game-programming-for-java-programmers/37201/view.html

Just swap in Rectangle for Circle in the code examples and it should work out okay.

1  
2  
3  
4  
5  
6  
7  
              Circle circle = new Circle();
              circle.setCenterX(100);
              circle.setCenterY(200);
              circle.setRadius(40);
              circle.setFill(Color.BLUE);

              root.getChildren().add(circle);



music and music apps: http://adonax.com
Offline Half_NO_oB

Senior Newbie





« Reply #2 - Posted 2017-05-02 13:12:36 »

Thx this fixed it Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4585 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4016 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!