Implementing JavaFX shapes in different methods each

Implementing JavaFX shapes in different methods each



I have this working JavaFX code, that is used to draw four different shapes in a root frame. I would like each shape to have its own method that implements it, that is for instance if it is a circle, something like public void circle()//statements but I don't know how to do it. Please help me. Thank you.


public void circle()//statements


package shapes;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
*
* @author mach
*/
public class Shapes extends Application

@Override
public void start(Stage primaryStage)


Group root = new Group();
//Draw a line
Line line= new Line();
line.setStartX(150);
line.setStartY(20);
line.setEndX(270);
line.setEndY(20);
line.setStroke(Color.RED);

//Draw a circle radius (x,y,radius)
Circle circle = new Circle(70, 110, 30);
circle.setStroke(Color.RED);
circle.setFill(Color.AQUA);

//draw a Rectangle(x, y, width, height);
Rectangle rect = new Rectangle(200, 90, 70,50);
rect.setStroke(Color.WHITE);
rect.setFill(Color.BLUE);

//Draw a triange
Polygon triangle = new Polygon();
triangle.getPoints().addAll(50.0, 0.0, 50.0, 50.0,100.0, 50.0);


Scene scene = new Scene(root, 300, 300);
//add all the created objects to canvas
root.getChildren().addAll(line, circle, rect, triangle);

primaryStage.setTitle("PART 1!");
primaryStage.setScene(scene);
primaryStage.show();


/**
* @param args the command line arguments
*/
public static void main(String args)
launch(args);








Are you saying you want a method that returns a new circle that you can add or a method that actually adds the circle to the screen. Also do you want just one single default type or parameters to change the specifics with

– faris
Sep 17 '18 at 3:39






This is a quite vague question

– faris
Sep 17 '18 at 3:39






I'm sorry about that. Each shape should have its own method that draws it, but all the shapes are added to the screen using one method.

– Timothy Mach
Sep 17 '18 at 3:47






Here is a Java tutorial which explains how methods can be written and other related information: Defining Methods in Java.

– prasad_
Sep 17 '18 at 5:37




2 Answers
2



Something like this?


public Circle getCircle()
Circle circle = new Circle(70, 110, 30);
circle.setStroke(Color.RED);
circle.setFill(Color.AQUA);
return circle;



Without knowing what you are working one, i only want to add that I generally would not know a good reason for using a method without any parameters here, and would advise against it.






@milk This is nice. Thank you. the last question is, how do I call this method to show the Circle object?

– Timothy Mach
Sep 17 '18 at 12:42






@TimothyMach if this answered your original question, mark it as the correct answer. If you have another question, start a new thread.

– Sedrick
Sep 17 '18 at 12:45






its part of the question. just knowing how to call the display the circle using this method is the only remain this.

– Timothy Mach
Sep 17 '18 at 12:51






You would use Circle circle = getCircle(); Alternativley you could do somethign like root.getChildren().addAll(line, getCircle(), rect, triangle);

– mik
Sep 19 '18 at 7:55



Circle circle = getCircle();


root.getChildren().addAll(line, getCircle(), rect, triangle);






@TimothyMach As it looks like my code answered your question it would be nice to mark it as correct answer - thanks.

– mik
Oct 1 '18 at 12:35




This is an extension of the answer above.
After some research, I found out how to add the shapes to the root frame. Inside the public void start(Stage primaryStage) method, create an object of the class Shape and use the object to call the methods for the shapes. Assign the objects to a variable and add them to the frame in my case (root).


public void start(Stage primaryStage)


public void start(Stage primaryStage)
Shapes frame = new Shapes();

Circle circle = frame.getCircle();
Rectangle rectangle= frame.getRectangle();


root.getChildren().addAll(circle, rectangle);

public Rectangle getRectangle()
Rectangle rect = new Rectangle(200, 90, 70,50);
rect.setStroke(Color.WHITE);
rect.setFill(Color.BLUE);
return rect;



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)