Error in code by classes

-1

Good. I have now started to study programming in java that includes several classes in which one calls another. But I'm facing a mistake that I can not understand. I looked for similarly-coded codes and I can not figure out a clear error difference. The goal is to create a class that does a drawing in the form of a tablet (Fulfilled), and another drawing a grid of these tablets.

In the drawing code I have this:

import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.scene.shape.*;
import javafx.embed.swing.*;
import javafx.application.*;
import javafx.scene.text.*;
import java.util.*;
import javafx.scene.paint.Color;

public class Pill  {  
private Pane pane;
public void pill(int x, int y, int width, int height, Color leftColor, Color rightColor)
{       
    Rectangle leftRect= new Rectangle(x,y,width/4,height);
    leftRect.setStrokeWidth(0);
    leftRect.setFill(leftColor);
    pane.getChildren().add(leftRect);

    Rectangle rightRect = new Rectangle(x+width/4,y,width/4,height);
    rightRect.setStrokeWidth(0);
    rightRect.setFill(rightColor);
    pane.getChildren().add(rightRect);

    Ellipse leftEllipse = new Ellipse(x,y+height/2,width/4,height/2);
    leftEllipse.setStrokeWidth(0);
    leftEllipse.setFill(leftColor);
    pane.getChildren().add(leftEllipse);

    Ellipse rightEllipse = new Ellipse(x+width/2,y+height/2,width/4,height/2);
    rightEllipse.setStrokeWidth(0);
    rightEllipse.setFill(rightColor);
    pane.getChildren().add(rightEllipse);

}
} // END class Pill

That compiles and has already tried to put in a class apart and runs without problem. Where is the error is in this:

import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.scene.shape.*;
import javafx.embed.swing.*;
import javafx.application.*;
import javafx.scene.text.*;
import java.util.*;
import javafx.scene.paint.Color;

public class Drawing  {  
private Pane pane;
private void start(Stage primaryStage) 
{
    primaryStage.setOnCloseRequest(
        e -> Platform.runLater( () -> {Platform.exit(); System.exit(0);} ));

    // WRITE YOUR CODE HERE
    // TODO    
        // 
  https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Pane.html
    this.pane = new Pane();
    this.pane.setPrefSize(900, 600);
    primaryStage.setScene(new Scene(this.pane, Color.WHITE));
    primaryStage.show();

    Pill pill = new Pill (150,100,300,200,Color.BLACK,Color.GRAY);   //Aqui dá um erro(Constructor Pill cannot be applied to class Pill cannot be applied to given types).
    this.pane.getChildren().add(pill);//Aqui da outro erro(No suitable method for add(pill).

    this.drawGrid();
} // END start

public void addShape(Shape shape)
{
    Platform.runLater(() -> this.pane.getChildren().add(shape));
}

/** execute this method to start the program
 * executing the code in method start(Stage primaryStage) 
 */
public static void start()
{
    Drawing drawingApp = new Drawing();
    drawingApp.launch();
}

public void launch()
{
    // Initialises JavaFX:
    new JFXPanel();
    // Makes sure JavaFX doesn't exit when first window is closed:
    Platform.setImplicitExit(false);
    // Runs initialisation on the JavaFX thread:
    Platform.runLater(() -> start(new Stage()));
}

public Drawing() 
{
    super();
}

public void drawGrid(int x, int y, int nLines, int nColumns, int width, int height, Color leftColor, Color rightColor)
{  
    nLines=3;
    nColumns = 4;
    for(int i = 300;i<=nLines*350;i+=350)
    {
        for(int j = 200; j <=nColumns*250;i+=250)
        {
            Pill p = new Pill (i,j,width,height,Color.BLACK,Color.GRAY);  // Dá um erro igual ao primeiro.
        }
    }
}

} // END class Drawing
    
asked by anonymous 30.12.2017 / 23:46

1 answer

0

The problem with your code is in the constructor call of the Pill class, since there is no constructor that matches what was called in the Drawing class. Since a constructor with the parameters of the call that was made in the latter was not defined, the code can not compile

A possible solution would be to rename the public void pill(...) method to public Pill(...) , being as follows:

public class Pill  {  
    private Pane pane;
    public Pill(int x, int y, int width, int height, Color leftColor, Color rightColor){
        ...
    }
}

Note that there are still two conceptual issues in your code:

1 - Constructor has no return type;

2 - Constructor must have the same class name (do not forget that Java is case sensitive!).

    
02.01.2018 / 01:22