Home > Blockchain >  Issue getting a shape from custom class to be added to driver class
Issue getting a shape from custom class to be added to driver class

Time:11-08

Hey everyone I'm writing a program the is a pizza builder, it has several check boxes and whenever you click on it will add 0.50 to the total and add a shape of whatever topping user chose. I created a class called Toppings that will create several different shapes. Everything seems to work but whenever I try to create it in the driver class it keeps throwing an error. Here is some code from the Toppings class:

public class Toppings {

private String shapeName;
private int radius, xCoor, yCoor;

// Constructor for Toppings-------------------------------------------------
public Toppings(String shapeName, int radius, int xCoor, int yCoor) {
     this.radius = radius;
     this.xCoor = xCoor;
     this.yCoor = yCoor;
     
}

public Toppings() {
    
}

public void createPepperoni() {
    
    // Create a circle for pepperoni----------------------------------------
    Circle pepperoni = new Circle();
    pepperoni.setRadius(radius);
    pepperoni.setCenterX(xCoor);
    pepperoni.setCenterY(yCoor);
    
}

And here is where I'm trying to create a pepperoni in the driver class:

    pepperoni = new Toppings("pepp1", 50, 550, 150);
    pepperoni.createPepperoni();
    
    list.addAll(pizzaDough, pepperoni);

When I try it like this it gives me a runtime error at list.addAll but it is not very specific as to what the issue is. I have also tried it like this:

Group pizza1 = new Group(pizzaDough, pepperoni);

Attempting it this way has an error that says "constructor Group in class Group cannot not be applied to given types." Can anyone help figure out what I'm doing wrong? I have imported the Circle from javafx just FYI.

CodePudding user response:

You can create a new instance of group, get the list of children and add the NodeObject to that list. Try this:

Group pizza1 = new Group();

//Retrieving the observable list object 
ObservableList list = root.getChildren(); 

//Adding node to the list
list.add(NodeObject); 

or alternatively you can create a collection of the nodes and create a new instance by passing the collection as Argument.

Group class documentation : Group

CodePudding user response:

pepperoni is a Toppings. If you want to put it in a Group, it would need to be a Node (which it is not).

You already have a method to make a node for a pepperoni. That method creates a circle. You could make the createPepporoni() method return the circle it created and add that to a group, e.g. group.add(pepperoni.createPepporoni()).

public Shape createPepperoni() {
    return new Circle(xCoor, yCoor, radius);
}

Where the arguments of the circle constructor are class field values or constants that you have previously established values for.

There are numerous other issues with your code and design, as well as the naming strategy for identifiers in your app, which I won't discuss here at this time, but hopefully the info I provided might help solve some of your immediate issues.

  • Related