Error in code by classes

Asked

Viewed 45 times

-1

Good.I have now started to study java programming that includes several classes in which one flame another. But I’m facing a mistake I can’t understand. I looked for codes with similar effect and I can’t find a clear difference in error. The goal is to create a class that makes a drawing in the form of a tablet (Fulfilled), and another that draws a grid of these tablets.

In the code of the drawing 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 I’ve tried to put in a class separately 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

1 answer

0

Your code problem is in the class constructor call Pill, since there is no constructor that meets what was called in the class Drawing. Since no constructor was defined with the parameters of the call that is made in the latter, the code cannot compile

A possible solution would be to rename the method public void pill(...) for 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 problems in your code:

1 - Builder has no return type;

2 - Constructor needs to have the same class name (don’t forget that Java is case sensitive!).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.